温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Java怎么插入链表结点

发布时间:2021-02-04 13:32:33 来源:亿速云 阅读:181 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用Java怎么插入链表结点,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

为什么需要链表?

我们知道,数组也可以存储数据,那么为什么还需要链表呢?接下来,我们来看看数组 和链表的区别:

1、数组就像身上编了号站成一排的人,要找第10个人很容易,根据人身上的编号很快就能找到。但插入、删除慢,要往某个位置插入或删除一个人时,后面的人身上的编号都要变。当然,加入或删除的人始终末尾的也快。

2、链表就像手牵着手站成一圈的人,要找第10个人不容易,必须从第一个人一个个数过去。但插入、删除快。插入时只要解开两个人的手,并重新牵上新加进来的人的手就可以。删除一样的道理。

链表示意图

使用Java怎么插入链表结点

链表的建立

class TestLink{//创建一个外部类	
	private Entry head;//指向头结点的引用
	public TestLink(){
		head = new Entry();//用结点类 new 一个头结点
	}
	
	class Entry{//Entry 创建一个结点内部类
		int data;//定义数据块
		Entry next;//定义地址块
		
		public Entry(){//构造方法1
			data = -1;//对结点数据块初始化
			next = null;//对地址初始化
		}
		public Entry(int val){//构造方法2
			data = val;//对数据块赋值
			next = null;
		}
	}
}
public class TestDemo2 {
			public static void main(String[] args) {
				TestLink testlink = new TestLink();
				//创建一个 链表外部类 对象
  }
}

头插法:从头插入

public void insertHead(int val){
  //有这么一个结点 
  Entry cur = new Entry(val);
  cur.next = head.next;
  head.next = cur;
  }

头插法示意图:

使用Java怎么插入链表结点

尾插法:从尾插入

public void insertTail(int val){
				//找到尾巴
				Entry cur = head;
				while(cur.next != null){//遍历结点
					cur = cur.next;
				}
				Entry entry = new Entry(val);//得到的结点
				cur.next = entry;
			}

尾插法示意图:

使用Java怎么插入链表结点

从任意结点插入

public boolean insertPos(int val,int pos){
  //1、判断pos的合法性
  if(pos < 0 || pos >= getLength()+1){
   return false;
  }
  Entry cur = head;
  for(int i = 0;i <= pos-1;i++){
   cur = cur.next;
  }
  //cur  pos的前一个
  Entry entry = new Entry(val);
  entry.next = cur.next;
  cur.next = entry;
  return true;
  }

示意图:

使用Java怎么插入链表结点

完整代码:

package LianBiao;
class TestLink1{	
	private Entry head;//指向头结点的引用	
	public TestLink1(){
		head = new Entry();
	}
	
	class Entry{//Entry Node 
		int data;
		Entry next;		
		public Entry(){
			data = -1;
			next = null;
		}
		
		public Entry(int val){
			data = val;
			next = null;
		}		
	}	
	
	public void insertHead(int val){
		//有这么一个结点 
		Entry cur = new Entry(val);
		cur.next = head.next;
		head.next = cur;
		/*head.next = cur;
		cur.next = head.next;*/
	}
	
	public void insertTail(int val){
		//找到尾巴
		Entry cur = head;
		while(cur.next != null){
			cur = cur.next;
		}
		Entry entry = new Entry(val);//得到的结点
		cur.next = entry;
	}
	//得到单链表的长度:
	public int getLength(){
		int len = 0;
		Entry cur = head.next;
		while(cur != null){
			len++;
			cur = cur.next;
		}
		return len;
	}
	//将数据插入到指定位置
	public boolean insertPos(int val,int pos){
		//1、判断pos的合法性
		if(pos < 0 || pos >= getLength()+1){
			return false;
		}
		Entry cur = head;
		for(int i = 0;i <= pos-1;i++){
			cur = cur.next;
		}
		//cur  pos的前一个
		Entry entry = new Entry(val);
		entry.next = cur.next;
		cur.next = entry;
		return true;
	}
	//
	
	//show()
	public void show(){
		/*Entry cur = head;
		while(cur.next != null){
			System.out.println("data:"+cur.next.data);
			cur = cur.next;
		}*/
		Entry cur = head.next;
		while(cur != null){
			System.out.println("data:"+cur.data);
			cur = cur.next;
		}
	}
	
}
public class LianBiao1 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
				TestLink1 testlink = new TestLink1();
				
				testlink.insertTail(1330);
				testlink.insertTail(110);
				//1330 110 
				testlink.insertPos(10,0);
				//10 1330 110
				
				if(testlink.insertPos(32,10000)){
					System.out.println("插入成功");
				}else{
					System.out.println("插入失败");
				}
				
				//10 32 1330 110
				
				testlink.show();
				System.out.println(testlink.getLength());
			}
		}

输出结果:

使用Java怎么插入链表结点

补充:java中创建链表,实现链表的尾部插入

我就废话不多说了,大家还是直接看代码吧~

package test;
//目标:创建链表,实现链表结点的尾部插入
class Node_5{
  private String data;
  public Node_5 nextNode;
  public void setData(String indata){
    this.data=indata;
  }
  public String getData(){
    return this.data;
  }
  public void setNextNode(Node_5 newNode){
    this.nextNode=newNode;
  }
  public Node_5 getNextNode(){
    return this.nextNode;
  }
  public void addData(String indata){
    setData(indata);
    Node_5 node_5=new Node_5();
    Node_5 head=node_5;
    if(node_5.getData()==null){
      node_5.setData(indata);
      System.out.println(node_5.getData());
    }
    else{
      node_5.setNextNode(node_5);  
      node_5.setData(indata);
      System.out.println(node_5.getData());
    }    
  }   
}

public class T_5 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Node_5 node_5=new Node_5();
    for(int i=1;i<=3;i++){
      node_5.addData("第"+i+"结点");
    }
  }
}

看完上述内容,你们对使用Java怎么插入链表结点有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI