java中創建鏈表的例子:
創新互聯公司專注于偃師企業網站建設,響應式網站,商城網站建設。偃師網站建設公司,為偃師等地區提供建站服務。全流程按需規劃網站,專業設計,全程項目跟蹤,創新互聯公司專業和態度為您提供的服務
package zx;
class Link{
private Node root;
class Node{
private String name;
private Node Next;
public Node(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void addNode(Node newNode){
if(this.Next==null){
this.Next = newNode;
}else{
this.Next.addNode(newNode);
}
}
public void printNode(){
System.out.print(this.name + "--");
if(this.Next!=null){
this.Next.printNode();
}
}
};
public void add(String name){
Node newNode = new Node(name);
if(this.root==null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
};
public class LinkDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Link link = new Link();
link.add("根節點");
link.add("第一節點");
link.add("第二節點");
link.add("第三節點");
link.add("第四節點");
link.print();
System.out.println("null");
}
}
Java語言中的對象引用實際上是一個指針(這里的指針均為概念上的意義,而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
程序代碼:
class?Node?
{?
Object?data;?
Node?next;//指向下一個結點?
}
將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便于在鏈表尾部增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。
鏈表的數據結構我們可以用類List來實現鏈表結構,用變量Head、Tail、Length、Pointer來實現表頭。存儲當前結點的指針時有一定的技巧,Pointer并非存儲指向當前結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是第一個結點,因為當刪除當前結點后仍需保證剩下的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。如何得到當前結點呢?我們定義了一個方法cursor(),返回值是指向當前結點的指針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作我們可以對鏈表進行各種操作。例如reset()方法使第一個結點成為當前結點。insert(Objectd)方法在當前結點前插入一個結點,并使其成為當前結點。remove()方法刪除當前結點同時返回其內容,并使其后繼結點成為當前結點,如果刪除的是最后一個結點,則第一個結點變為當前結點。
鏈表類List的源代碼如下:
package??cn.javatx;?import??java.io.IOException;/**
*??@author??ljfan
*??
*/
public??class??List??{
private??Node??Head??=??null;
private??Node??Tail??=??null;
private??Node??Pointer??=??null;
private??int??Length??=??0;public??void??deleteAll()??{
Head??=??null;
Tail??=??null;
Pointer??=??null;
Length??=??0;
}public??void??reset()??{
Pointer??=??null;
}public??boolean??isEmpty()??{
return??(Length??==??0);
}public??boolean??isEnd()??{
if??(Length??==??0)
throw??new??java.lang.NullPointerException();
else??if??(Length??==??1)
return??true;
else
return??(cursor()??==??Tail);
}public??Object??nextNode()??{
if??(Length??==??1)
throw??new??java.util.NoSuchElementException();
else??if??(Length??==??0)
throw??new??java.lang.NullPointerException();
else??{
Node??temp??=??cursor();
Pointer??=??temp;
if??(temp??!=??Tail)
return??(temp.next.data);
else
throw??new??java.util.NoSuchElementException();
}
}public??Object??currentNode()??{
Node??temp??=??cursor();
return??temp.data;
}public??void??insert(Object??d)??{
Node??e??=??new??Node(d);
if??(Length??==??0)??{
Tail??=??e;
Head??=??e;
}??else??{
Node??temp??=??cursor();
e.next??=??temp;
if??(Pointer??==??null)
Head??=??e;
else
Pointer.next??=??e;
}
Length++;
}public??int??size()??{
return??(Length);
}public??Object??remove()??{
Object??temp;
if??(Length??==??0)
throw??new??java.util.NoSuchElementException();
else??if??(Length??==??1)??{
temp??=??Head.data;
deleteAll();
}??else??{
Node??cur??=??cursor();
temp??=??cur.data;
if??(cur??==??Head)
Head??=??cur.next;
else??if??(cur??==??Tail)??{
Pointer.next??=??null;
Tail??=??Pointer;
reset();
}??else
Pointer.next??=??cur.next;
Length--;
}
return??temp;
}private??Node??cursor()??{
if??(Head??==??null)
throw??new??java.lang.NullPointerException();
else??if??(Pointer??==??null)
return??Head;
else
return??Pointer.next;
}public??static??void??main(String[]??args)??{
List??a??=??new??List();
for??(int??i??=??1;??i??=??10;??i++)
a.insert(new??Integer(i));
System.out.println(a.currentNode());
while??(!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while??(!a.isEnd())??{
a.remove();
}
a.remove();
a.reset();
if??(a.isEmpty())
System.out.println("There??is??no??Node??in??List??n");
System.out.println("You??can??press??return??to??quitn");
try??{
System.in.read();
}??catch??(IOException??e)??{
}
}
}class??Node??{
Object??data;
Node??next;Node(Object??d)??{
data??=??d;
next??=??null;
}
}
當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也可以用在堆棧和隊列的現實中。
class Node {
Object data;
Node next;//申明類Node類的對象叫Next
public Node(Object data) { //類Node的構造函數
setData(data);
}
public void setData(Object data) {
this.data = data;
}
public Object getData() {
return data;
}
}
class Link {
Node head;//申明一個Node類的一個對象 head
int size = 0;
public void add(Object data) {
Node n = new Node(data); //調用Node類的構造函數
鏈表是一種重要的數據結構,在程序設計中占有很重要的地位。C語言和C++語
言中是用指針來實現鏈表結構的,由于Java語言不提供指針,所以有人認為在
Java語言中不能實現鏈表,其實不然,Java語言比C和C++更容易實現鏈表結構
。Java語言中的對象引用實際上是一個指針(本文中的指針均為概念上的意義,
而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
class Node
{
Object data;
Node next;//指向下一個結點
}
將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給
其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表
頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便于在鏈表尾部
增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表
的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。下圖是這種鏈表的
示意圖:
鏈表的數據結構
我們可以用類List來實現鏈表結構,用變量Head、Tail、Length、Pointer
來實現表頭。存儲當前結點的指針時有一定的技巧, Pointer并非存儲指向當前
結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是
第一個結點。那么為什么要這樣做呢?這是因為當刪除當前結點后仍需保證剩下
的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。那么如
何得到當前結點呢,我們定義了一個方法cursor(),返回值是指向當前結點的指
針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作
我們可以對鏈表進行各種操作。例如reset()方法使第一個結點成為當前結點。
insert(Object d)方法在當前結點前插入一個結點,并使其成為當前結點。
remove()方法刪除當前結點同時返回其內容,并使其后繼結點成為當前結點,如
果刪除的是最 后一個結點,則第一個結點變為當前結點。
鏈表類List的源代碼如下:
import java.io.*;
public class List
{
/*用變量來實現表頭*/
private Node Head=null;
private Node Tail=null;
private Node Pointer=null;
private int Length=0;
public void deleteAll()
/*清空整個鏈表*/
{
Head=null;
Tail=null;
Pointer=null;
Length=0;
}
public void reset()
/*鏈表復位,使第一個結點成為當前結點*/
{
Pointer=null;
}
public boolean isEmpty()
/*判斷鏈表是否為空*/
{
return(Length==0);
}
public boolean isEnd()
/*判斷當前結點是否為最后一個結點*/
{
if(Length==0)
throw new java.lang.NullPointerException();
else if(Length==1)
return true;
else
return(cursor()==Tail);
}
public Object nextNode()
/*返回當前結點的下一個結點的值,并使其成為當前結點*/
{
if(Length==1)
throw new java.util.NoSuchElementException();
else if(Length==0)
throw new java.lang.NullPointerException();
else
{
Node temp=cursor();
Pointer=temp;
if(temp!=Tail)
return(temp.next.data);
else
throw new java.util.NoSuchElementException();
}
}
public Object currentNode()
/*返回當前結點的值*/
{
Node temp=cursor();
return temp.data;
}
public void insert(Object d)
/*在當前結點前插入一個結點,并使其成為當前結點*/
{
Node e=new Node(d);
if(Length==0)
{
Tail=e;
Head=e;
}
else
{
Node temp=cursor();
e.next=temp;
if(Pointer==null)
Head=e;
else
Pointer.next=e;
}
Length++;
}
public int size()
/*返回鏈表的大小*/
{
return (Length);
}
public Object remove()
/*將當前結點移出鏈表,下一個結點成為當前結點,如果移出的結點是最后
一個結點,則第一個結點成為當前結點*/
{
Object temp;
if(Length==0)
throw new java.util.NoSuchElementException();
else if(Length==1)
{
temp=Head.data;
deleteAll();
}
else
{
Node cur=cursor();
temp=cur.data;
if(cur==Head)
Head=cur.next;
else if(cur==Tail)
{
Pointer.next=null;
Tail=Pointer;
reset();
}
else
Pointer.next=cur.next;
Length--;
}
return temp;
}
private Node cursor()
/*返回當前結點的指針*/
{
if(Head==null)
throw new java.lang.NullPointerException();
else if(Pointer==null)
return Head;
else
return Pointer.next;
}
public static void main(String[] args)
/*鏈表的簡單應用舉例*/
{
List a=new List ();
for(int i=1;i=10;i++)
a.insert(new Integer(i));
System.out.println(a.currentNode());
while(!a.isEnd())
System.out.println(a.nextNode());
a.reset();
while(!a.isEnd())
{
a.remove();
}
a.remove();
a.reset();
if(a.isEmpty())
System.out.println("There is no Node in List \n");
System.in.println("You can press return to quit\n");
try
{
System.in.read();
//確保用戶看清程序運行結果
}
catch(IOException e)
{}
}
}
class Node
/*構成鏈表的結點定義*/
{
Object data;
Node next;
Node(Object d)
{
data=d;
next=null;
}
}
讀者還可以根據實際需要定義新的方法來對鏈表進行操作。雙向鏈表可以用
類似的方法實現只是結點的類增加了一個指向前趨結點的指針。
可以用這樣的代碼來實現:
class Node
{
Object data;
Node next;
Node previous;
Node(Object d)
{
data=d;
next=null;
previous=null;
}
}
當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也
可以用在堆棧和隊列的實現中,這里就不再多寫了,有興趣的讀者可以將List類
的代碼稍加改動即可。
分享文章:鏈表java代碼 java鏈表詳解
分享URL:http://m.kartarina.com/article32/hgjhpc.html
成都網站建設公司_創新互聯,為您提供網站策劃、網站改版、網站設計、云服務器、網頁設計公司、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯