链表是一种复杂的数据结构,其数据之间的相互关系使链表分成三种:单链表、循环链表、双向链表,下面将逐一介绍。链表在数据结构中是基础,也是重要的知识点,这里讲下java 中链表的实现,
java 链表操作:单链表和双链表
主要讲述几点:
一、链表的简介
二、链表实现原理和必要性
三、单链表示例
四、双链表示例
一、链表的简介
链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都相应的应用,链表有多种类别,文章针对单链表和双链表进行分析。链表中数据就像被一个链条串联一起,轻易的可以实现数据的访问。
二、链表实现原理和必要性
这里只分析单链表和双链表。链表的实现过程是有些许复杂的,但是会带来许多好处。比如现在网购时代到来,商家发快递一般会将商品包装在盒子里并写上地址信息,快递公司就可以通过盒子上的信息找到买家,商品完整到达。如果没有盒子的保护,有可能在途中商品受损。而链表就好比那个写了地址信息的盒子,既保护了商品信息,同时又写好了物流信息。链表之中存在一个head节点,类似“火车头”,只要找到相应head节点,就可以对链表进行操作。此次分析中,head节点只是做数据头,不保存有效数据。
单链表的实现原理如图:
双链表实现原理:
三、单链表示例
icommoperate8742468051c85b06f0a0af9e3e506b5c 接口操作类:
package linklisttest;
import java.util.map;
public interface icommoperate<t> {
public boolean insertnode(t node) ;
public boolean insertposnode(int pos, t node) ;
public boolean deletenode(int pos) ;
public boolean updatenode(int pos, map<string, object> map) ;
public t getnode(int pos, map<string, object> map) ;
public void printlink() ;
}
单链表节点:
package linklisttest;
// 单连表节点
public class snode {
private string data;
private snode nextnode;
public snode() {
}
public snode(string data) {
this.data = data;
this.nextnode = new snode();
}
public string getdata() {
return data;
}
public void setdata(string data) {
this.data = data;
}
public snode getnextnode() {
return nextnode;
}
public void setnextnode(snode nextnode) {
this.nextnode = nextnode;
}
@override
public string tostring() {
return "snode [data=" + data + "]";
}
}
单链接操作类:
package linklisttest;
import java.util.hashmap;
import java.util.map;
public class singlelinklist implements icommoperate<snode>{
private snode head = new snode("head") ; // 公共头指针,声明之后不变
private int size = 0 ;
public int getsize() {
return this.size;
}
/*
* 链表插入,每次往末端插入
* */
@override
public boolean insertnode(snode node) {
boolean flag = false ;
snode current = this.head ;
if( this.size==0 ){ // 空链表
this.head.setnextnode(node) ;
node.setnextnode(null) ;
}else{ // 链表内节点
while( current.getnextnode()!=null ){
current = current.getnextnode() ;
}
current.setnextnode(node) ;
node.setnextnode(null) ;
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端
* */
@override
public boolean insertposnode(int pos, snode node){
boolean flag = true;
snode current = this.head.getnextnode() ;
if( this.size==0 ){ // 空链表
this.head.setnextnode(node) ;
node.setnextnode(null) ;
this.size++ ;
}else if( this.size<pos ){ // pos位置大于链表长度,插入末端
insertnode(node) ;
}else if( pos>0 && pos<=this.size) { // 链表内节点
// 1、找到要插入pos位置节点和前节点
int find = 0;
snode prenode = this.head; // 前节点
snode currentnode = current; // 当前节点
while( find<pos-1 && currentnode.getnextnode()!=null ){
prenode = current ; // 前节点后移
currentnode = currentnode.getnextnode() ; // 当前节点后移
find++ ;
}
// system.out.println(prenode);
// system.out.println(currentnode);
// 2、插入节点
prenode.setnextnode(node);
node.setnextnode(currentnode);
this.size++ ;
system.out.println("节点已经插入链表中");
}else{
system.out.println("位置信息错误");
flag = false ;
}
return flag;
}
/*
* 指定链表的节点pos,删除对应节点。方式:找到要删除节点的前后节点,进行删除。从1开始
* */
@override
public boolean deletenode(int pos) {
boolean flag = false;
snode current = this.head.getnextnode() ;
if( pos<=0 || pos>this.size || current==null ){
system.out.println("位置信息错误或链表无信息");
}else{
// 1、找到要删除节点的前后节点
int find = 0;
snode prenode = this.head; // 前节点
snode nextnode = current.getnextnode(); // 后节点
while( find<pos-1 && nextnode.getnextnode()!=null ){
prenode = current ; // 前节点后移
nextnode = nextnode.getnextnode() ; // 后节点后移
find++ ;
}
// system.out.println(prenode);
// system.out.println(nextnode);
// 2、删除节点
prenode.setnextnode(nextnode);
system.gc();
this.size-- ;
flag = true ;
}
return flag;
}
/*
* 指定链表的节点pos,修改对应节点。 从1开始
* */
@override
public boolean updatenode(int pos, map<string, object> map) {
boolean flag = false ;
snode node = getnode(pos, map); // 获得相应位置pos的节点
if( node!=null ){
string data = (string) map.get("data") ;
node.setdata(data);
flag = true ;
}
return flag;
}
/*
* 找到指定链表的节点pos,从1开始
* */
@override
public snode getnode(int pos, map<string, object> map) {
snode current = this.head.getnextnode() ;
if( pos<=0 || pos>this.size || current==null ){
system.out.println("位置信息错误或链表不存在");
return null;
}
int find = 0 ;
while( find<pos-1 && current!=null ){
current = current.getnextnode() ;
find++ ;
}
return current;
}
/*
* 打印链表
* */
@override
public void printlink() {
int length = this.size ;
if( length==0 ){
system.out.println("链表为空!");
return ;
}
snode current = this.head.getnextnode() ;
int find = 0 ;
system.out.println("总共有节点数: " + length +" 个");
while( current!=null ){
system.out.println("第 " + (++find) + " 个节点 :" + current);
current=current.getnextnode() ;
}
}
public static void main(string[] args) {
singlelinklist sll = new singlelinklist() ;
snode node1 = new snode("节点1");
snode node2 = new snode("节点2");
snode node3 = new snode("节点3");
snode node4 = new snode("节点4");
snode node5 = new snode("节点5");
snode node6 = new snode("插入指定位置");
sll.insertposnode(sll.getsize()+1, node1) ;
sll.insertposnode(sll.getsize()+1, node2) ;
sll.insertposnode(sll.getsize()+1, node3) ;
sll.insertposnode(sll.getsize()+1, node4) ;
sll.insertposnode(sll.getsize()+1, node5) ;
// sll.insertnode(node1);
// sll.insertnode(node2);
// sll.insertnode(node3);
// sll.insertnode(node4);
// sll.insertnode(node5);
system.out.println("*******************输出链表*******************");
sll.printlink();
system.out.println("*******************获得指定链表节点*******************");
int pos = 2 ;
system.out.println("获取链表第 "+pos+" 个位置数据 :"+sll.getnode(pos, null));
system.out.println("*******************向链表指定位置插入节点*******************");
int pos1 = 2 ;
system.out.println("将数据插入第 "+pos1+" 个节点:");
sll.insertposnode(pos1, node6) ;
sll.printlink();
system.out.println("*******************删除链表指定位置节点*******************");
int pos2 = 2 ;
system.out.println("删除第 "+pos2+" 个节点:");
sll.deletenode(pos2) ;
sll.printlink();
system.out.println("*******************修改链表指定位置节点*******************");
int pos3 = 2 ;
system.out.println("修改第 "+pos3+" 个节点:");
map<string, object> map = new hashmap<>() ;
map.put("data", "this is a test") ;
sll.updatenode(pos3, map) ;
sll.printlink();
}
}
四、双链表示例
icommoperate<t> 接口操作类:
package linklisttest;
import java.util.map;
public interface icommoperate<t> {
public boolean insertnode(t node) ;
public boolean insertposnode(int pos, t node) ;
public boolean deletenode(int pos) ;
public boolean updatenode(int pos, map<string, object> map) ;
public t getnode(int pos, map<string, object> map) ;
public void printlink() ;
}
双链表节点:
package linklisttest;
// 双连表节点
public class dnode {
private dnode priornode;
private string data;
private dnode nextnode;
public dnode(){
}
public dnode(string data) {
this.priornode = new dnode() ;
this.data = data ;
this.nextnode = new dnode() ;
}
public dnode getpriornode() {
return priornode;
}
public void setpriornode(dnode priornode) {
this.priornode = priornode;
}
public string getdata() {
return data;
}
public void setdata(string data) {
this.data = data;
}
public dnode getnextnode() {
return nextnode;
}
public void setnextnode(dnode nextnode) {
this.nextnode = nextnode;
}
@override
public string tostring() {
return "dnode [data=" + data + "]";
}
}
双链表实现类:
package linklisttest;
import java.util.hashmap;
import java.util.map;
public class doublelinklist implements icommoperate<dnode>{
private dnode head = new dnode("head");
private int size = 0 ;
public int getsize() {
return this.size;
}
/*
* 链表插入,每次往末端插入
* */
@override
public boolean insertnode(dnode node) {
boolean flag = false;
dnode current = this.head ;
if( this.size==0 ){ // 空链表
this.head.setnextnode(node) ;
node.setpriornode(this.head);
node.setnextnode(null) ;
}else{ // 链表内节点
while( current.getnextnode()!=null ){
current = current.getnextnode() ;
}
current.setnextnode(node);
node.setnextnode(null);
node.setpriornode(current);
}
this.size++ ;
flag = true ;
return flag;
}
/*
* 插入链表指定位置pos,从1开始,而pos大于size则插入链表末端
* */
@override
public boolean insertposnode(int pos, dnode node) {
boolean flag = true;
dnode current = this.head.getnextnode() ;
if( this.size==0){ // 链表为空
this.head.setnextnode(node) ;
node.setnextnode(null) ;
node.setpriornode(this.head);
this.size++ ;
}else if( pos>this.size ){ // pos位置大于链表长度,插入末端
insertnode(node) ;
}else if( pos>0 && pos<=this.size ){ // 链表内节点
// 1、找到要插入位置pos节点,插入pos节点当前位置
int find = 0;
while( find<pos-1 && current.getnextnode()!=null ){
current = current.getnextnode() ;
find++ ;
}
// 2、插入节点
if( current.getnextnode()==null ){ // 尾节点
node.setpriornode(current);
node.setnextnode(null);
current.setnextnode(node);
} else if( current.getnextnode()!=null ) { //中间节点
node.setpriornode(current.getpriornode());
node.setnextnode(current);
current.getpriornode().setnextnode(node);
current.setpriornode(node);
}
this.size++ ;
}else{
system.out.println("位置信息错误");
flag = false ;
}
return flag;
}
/*
* 指定链表的节点pos,删除对应节点,从1开始
* */
@override
public boolean deletenode(int pos) {
boolean flag = false;
dnode current = this.head.getnextnode() ;
if( pos<=0 || pos>this.size || current==null ){
system.out.println("位置信息错误或链表不存在");
}else{
// 1、找到要删除位置pos节点
int find = 0;
while( find<pos-1 && current.getnextnode()!=null ){
current = current.getnextnode() ;
find++ ;
}
// 2、删除节点
if( current.getnextnode()==null ){ // 尾节点
current.getpriornode().setnextnode(null) ;
} else if( current.getnextnode()!=null ) { //中间节点
current.getpriornode().setnextnode(current.getnextnode()) ;
current.getnextnode().setpriornode(current.getpriornode()) ;
}
system.gc();
this.size-- ;
flag = true ;
}
return flag;
}
/*
* 指定链表的节点pos,修改对应节点。 从1开始
* */
@override
public boolean updatenode(int pos, map<string, object> map) {
boolean flag = false ;
dnode node = getnode(pos, map);
if( node!=null ){
string data = (string) map.get("data") ;
node.setdata(data);
flag = true ;
}
return flag;
}
/*
* 找到指定链表的节点pos,从1开始
* */
@override
public dnode getnode(int pos, map<string, object> map) {
dnode current = this.head.getnextnode() ;
if( pos<=0 || pos>this.size || current==null ){
system.out.println("位置信息错误或链表不存在");
return null;
}
int find = 0 ;
while( find<pos-1 && current!=null ){
current = current.getnextnode() ;
find++ ;
}
return current;
}
/*
* 打印链表
* */
@override
public void printlink() {
int length = this.size ;
if( length==0 ){
system.out.println("链表为空!");
return ;
}
dnode current = this.head.getnextnode() ;
int find = 0 ;
system.out.println("总共有节点数: " + length +" 个");
while( current!=null ){
system.out.println("第 " + (++find) + " 个节点 :" + current);
current=current.getnextnode() ;
}
}
public static void main(string[] args) {
doublelinklist dll = new doublelinklist() ;
dnode node1 = new dnode("节点1");
dnode node2 = new dnode("节点2");
dnode node3 = new dnode("节点3");
dnode node4 = new dnode("节点4");
dnode node5 = new dnode("节点5");
dnode node6 = new dnode("插入指定位置");
dll.insertposnode(10, node1) ;
dll.insertposnode(10, node2) ;
dll.insertposnode(10, node3) ;
dll.insertposnode(10, node4) ;
dll.insertposnode(10, node5) ;
// dll.insertnode(node1);
// dll.insertnode(node2);
// dll.insertnode(node3);
// dll.insertnode(node4);
// dll.insertnode(node5);
system.out.println("*******************输出链表*******************");
dll.printlink();
system.out.println("*******************获得指定链表节点*******************");
int pos = 2 ;
system.out.println("获取链表第 "+pos+" 个位置数据 :"+dll.getnode(pos, null));
system.out.println("*******************向链表指定位置插入节点*******************");
int pos1 = 2 ;
system.out.println("将数据插入第"+pos1+"个节点:");
dll.insertposnode(pos1, node6) ;
dll.printlink();
system.out.println("*******************删除链表指定位置节点*******************");
int pos2 = 7 ;
system.out.println("删除第"+pos2+"个节点:");
dll.deletenode(pos2) ;
dll.printlink();
system.out.println("*******************修改链表指定位置节点*******************");
int pos3 = 2 ;
system.out.println("修改第"+pos3+"个节点:");
map<string, object> map = new hashmap<>() ;
map.put("data", "this is a test") ;
dll.updatenode(pos3, map) ;
dll.printlink();
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
更多java 数据结构链表操作实现代码。