Java 實(shí)現(xiàn)鏈表結(jié)點(diǎn)插入
PS:鏈表是一種數(shù)據(jù)結(jié)構(gòu),而數(shù)據(jù)結(jié)構(gòu)就是一種存放數(shù)據(jù)的方式。
為什么需要鏈表?
我們知道,數(shù)組也可以存儲(chǔ)數(shù)據(jù),那么為什么還需要鏈表呢?接下來(lái),我們來(lái)看看數(shù)組 和鏈表的區(qū)別:
1、數(shù)組就像身上編了號(hào)站成一排的人,要找第10個(gè)人很容易,根據(jù)人身上的編號(hào)很快就能找到。但插入、刪除慢,要往某個(gè)位置插入或刪除一個(gè)人時(shí),后面的人身上的編號(hào)都要變。當(dāng)然,加入或刪除的人始終末尾的也快。
2、鏈表就像手牽著手站成一圈的人,要找第10個(gè)人不容易,必須從第一個(gè)人一個(gè)個(gè)數(shù)過(guò)去。但插入、刪除快。插入時(shí)只要解開兩個(gè)人的手,并重新牽上新加進(jìn)來(lái)的人的手就可以。刪除一樣的道理。
鏈表示意圖

鏈表的建立
class TestLink{//創(chuàng)建一個(gè)外部類
private Entry head;//指向頭結(jié)點(diǎn)的引用
public TestLink(){
head = new Entry();//用結(jié)點(diǎn)類 new 一個(gè)頭結(jié)點(diǎn)
}
class Entry{//Entry 創(chuàng)建一個(gè)結(jié)點(diǎn)內(nèi)部類
int data;//定義數(shù)據(jù)塊
Entry next;//定義地址塊
public Entry(){//構(gòu)造方法1
data = -1;//對(duì)結(jié)點(diǎn)數(shù)據(jù)塊初始化
next = null;//對(duì)地址初始化
}
public Entry(int val){//構(gòu)造方法2
data = val;//對(duì)數(shù)據(jù)塊賦值
next = null;
}
}
}
public class TestDemo2 {
public static void main(String[] args) {
TestLink testlink = new TestLink();
//創(chuàng)建一個(gè) 鏈表外部類 對(duì)象
}
}
頭插法:從頭插入
public void insertHead(int val){
//有這么一個(gè)結(jié)點(diǎn)
Entry cur = new Entry(val);
cur.next = head.next;
head.next = cur;
}
頭插法示意圖:

尾插法:從尾插入
public void insertTail(int val){
//找到尾巴
Entry cur = head;
while(cur.next != null){//遍歷結(jié)點(diǎn)
cur = cur.next;
}
Entry entry = new Entry(val);//得到的結(jié)點(diǎn)
cur.next = entry;
}
尾插法示意圖:

從任意結(jié)點(diǎn)插入
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的前一個(gè)
Entry entry = new Entry(val);
entry.next = cur.next;
cur.next = entry;
return true;
}
示意圖:

完整代碼:
package LianBiao;
class TestLink1{
private Entry head;//指向頭結(jié)點(diǎn)的引用
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){
//有這么一個(gè)結(jié)點(diǎn)
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);//得到的結(jié)點(diǎn)
cur.next = entry;
}
//得到單鏈表的長(zhǎng)度:
public int getLength(){
int len = 0;
Entry cur = head.next;
while(cur != null){
len++;
cur = cur.next;
}
return len;
}
//將數(shù)據(jù)插入到指定位置
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的前一個(gè)
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());
}
}
輸出結(jié)果:

補(bǔ)充:java中創(chuàng)建鏈表,實(shí)現(xiàn)鏈表的尾部插入
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
package test;
//目標(biāo):創(chuàng)建鏈表,實(shí)現(xiàn)鏈表結(jié)點(diǎn)的尾部插入
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+"結(jié)點(diǎn)");
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
使用純java config來(lái)配置spring mvc方式
這篇文章主要介紹了使用純java config來(lái)配置spring mvc方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
FreeMarker如何調(diào)用Java靜態(tài)方法及靜態(tài)變量方法
這篇文章主要介紹了FreeMarker如何調(diào)用Java靜態(tài)方法及靜態(tài)變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

