java完成單鏈表、雙向鏈表。本站提示廣大學習愛好者:(java完成單鏈表、雙向鏈表)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成單鏈表、雙向鏈表正文
本文實例為年夜家分享了java完成單鏈表、雙向鏈表的相干代碼,供年夜家參考,詳細內容以下
java完成單鏈表:
package code;
class Node
{
Node next;
int data;
public Node(int data)
{
this.data=data;
}
}
class LinkList
{
Node first;
//頭部
public LinkList()
{
this.first=null;
}
public void addNode(Node no)
{
no.next=first;
first=no;//在頭部添加
}
public void delectNode()
{
Node n=first.next;
first=null;
first=n;//在頭部刪除
}
//刪除指定地位
public int Number()
{
int count=1;
//檢查有若干元素
Node nd=first;
while(nd.next!=null)
{
nd=nd.next;
count++;
}
return count;
}
public void delectExact(int n)
{
//刪除指定地位
if(n>1)
{
int count=1;
Node de=first;
while(count<n-1)
{
de=de.next;
count++;
}
de.next=de.next.next;
}
else
first=first.next;
}
public void addExact(int n,Node nd)
{
if(n>1)//添加指定地位
{
int count=1;
Node de=first;
while(count<n-1)
{
de=de.next;
count++;
}
nd.next=de.next;
de.next=nd;
}
else
first=first.next;
}
public int findNode(int n)
{
int count=1;//查找一個數對應的地位
Node de=first;
while(de.data!=n)
{
de=de.next;
count++;
if(de==null)
{
return -1;
}
}
return count;
}
public void print()
{
Node no=first;//打印一切
while(no!=null)
{
System.out.println(no.data);
no=no.next;
}
}
}
public class TextNode
{
public static void main(String[] args)
{
LinkList ll=new LinkList();
ll.addNode(new Node(12));
ll.addNode(new Node(15));
ll.addNode(new Node(18));
ll.addNode(new Node(19));
ll.addNode(new Node(20));
/*System.out.println(ll.first.data);
ll.delectNode();
System.out.println(ll.first.data);*/
System.out.println(ll.Number());
ll.delectExact(3);
ll.addExact(3, new Node(100));
System.out.println(ll.Number());
// ll.print();
System.out.println(ll.findNode(112));
}
}
java完成雙向鏈表:
public class DoubleLink
{
public static void main(String[]args)
{
Node2 no=new Node2(5);
no.addLeft(new Node2(6));
no.addRight(new Node2(7));
/*no.print();
no.print2();*/
no.addExact2(1, new Node2(8));
no.print();
System.out.println("--------------");
no.print2();
}
}
class Node2
{
public Node2 first;
public Node2 end;
public Node2 left;
public Node2 right;
int data=0;
public Node2(int n)
{
first=this;
end=this;
first.data=n;
}
//從頭部添加
public void addLeft(Node2 before)
{
first.left=before;
before.right=first;
first=before;
}
//從尾部添加
public void addRight(Node2 after)
{
end.right=after;
after.left=end;
end=after;
}
//拔出負數(第三聲)的第幾個
public void addExact(int n,Node2 no)
{
int count=0;
if(n==0)
{
addLeft(no);
}
else
{
Node2 f=first;
while(true)
{
f=f.right;
count++;
if(count==n)
{
//此處為四個指針的指向的變更
no.left=f.left;
f.left.right=no;
// first.left=no;
no.right=f;
f.left=no;
break;
}
}
}
}
//拔出倒數的第幾個
public void addExact2(int n,Node2 no)
{
int count=0;
if(n==0)
{
addRight(no);
}
else
{
Node2 f=end;
while(true)
{
f=f.left;
count++;
if(count==n)
{
no.left=f;
no.right=f.right;
f.right.left=no;
f.right=no;
break;
}
}
}
}
//正序遍歷
public void print()
{
System.out.println(first.data);
while(first.right!=null)
{
System.out.println(first.right.data);
first=first.right;
}
// System.out.println(end.data);
}
//倒序遍歷
public void print2()
{
System.out.println(end.data);
while(end.left!=null)
{
System.out.println(end.left.data);
end=end.left;
}
}
}
/*值得留意的是,每次拔出一個新的對象的時刻,須要留意指針指向的轉變。
起首是這個新的對象雙方的指向(左和右),其次是時右邊的對象向右的指向
和左邊對象向左的指向。
這四個指針的指向必需准確,不然能夠招致正序或許倒序遍歷沒法完成。
*/
/*比較單鏈表,單鏈表只能從一個偏向遍歷,由於只要一個頭,而雙向鏈表,有頭和尾,可以從
* 頭遍歷,也能夠從尾遍歷,並且個中一個對象由於有兩個偏向的指針,所以他可以取得右邊的
* 對象也能夠取得左邊的對象。
* 然則單鏈表的話,由於只要一個偏向,所以只能向左或右。添加對象的時刻,雙向也能夠從頭添加,也能夠從尾添加。
* 假如單鏈表要完成兩個偏向添加比擬可貴,或許說不可,由於他只要向左或向右的一個偏向的指針
* 而雙向鏈表每一個對象都有兩個偏向的指針沒如許更靈巧,然則這異樣出缺點,由於如許的話每一個對象
* 都邑包括兩個指針,這異樣內存會消費更多。
*
* */
以上就是本文的全體內容,願望對年夜家進修java法式設計有所贊助。