當前位置:首頁 » 編程語言 » java實現鏈表

java實現鏈表

發布時間: 2022-06-13 00:41:14

java語言沒有指針,怎樣實現鏈表

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的源代碼如下:
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;
}
}
當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也可以用在堆棧和隊列的實現中

㈡ 用Java語言實現單向鏈表

1.先定義一個節點類

package com.buren;

public class IntNode {
//定義一個節點類

int
info;
//定義屬性,節點中的值
IntNode next;
//定義指向下一個節點的屬性

public IntNode(int
i){ //構造一個next為空的節點
this(i,null);
}

public IntNode(int i,IntNode
n){ //構造值為i指向n的節點
info=i;
next=n;
}

}

2.再定義一個鏈表類,這是主要部分

package com.buren;

public class IntSLList {

private IntNode head,tail;
//定義指向頭結點和尾結點的指針,
//如果大家看著這個不像指針的話,那就需要對指針有更深刻的了解

public
IntSLList(){
//定義一個空節點
head=tail=null;
}

public boolean
isEmpty(){
//判斷節點是否為空
return
head==null;
//這行代碼看起來似乎很神奇,其實真的很神奇,偶是服了
}

public void addToHead(int el){
//將el插入到頭結點前
head=new
IntNode(el,head);
//將節點插入到頭結點前,作為新的投節點
if(head==tail){
//給空鏈表插入節點時
tail=head;
//頭結點和尾結點指向同一個節點
}
}

public void addToTail(int
el){
//向鏈表的尾部增加結點
if(!isEmpty()){
//判斷鏈表是否為空
tail.next=new
IntNode(el);
//新建立一個值為el的節點,將鏈表的尾結點指向新節點
tail=tail.next;
//更新尾指針的指向
}else{
head=tail=new
IntNode(el);
//如果鏈表為空,新建立一個節點,將頭尾指針同時指向這個節點
}
}

public int
deleteFromHead(){
//刪除頭結點,將節點信息返回
int
el=head.info;
//取出節點信息
if(head==tail){
//如果鏈表中只有一個節點
head=tail=null;
//刪除這一個節點
}else{
head=head.next;
//如果鏈表中不止一個節點,將頭結點的下一個節點作為頭結點
}
return
el;
//返回原頭結點的值
}

public int
deleteFromTail(){
//刪除尾結點,返回尾結點的信息
int
el=tail.info;
//取出尾結點的值
if(head==tail){
// 如果鏈表中只有一個節點
head=tail=null;
//刪除這個節點
}else{
IntNode
temp;
//定義中間變數
for(temp=head;temp.next!=tail;temp=temp.next);
//找出尾結點的前一個節點,注意最後的分號,

//這個for循環是沒有循環體的,目的在於找出尾結點的前一個節點

//在整個程序中用了很多次這樣的寫法,相當經典啊
tail=temp;
//將找出來的節點作為尾結點,刪除原來的尾結點
tail.next=null;
//將新尾結點的指向設為空
}
return
el;
//返回原尾結點的信息
}

public void
printAll(){
//列印鏈表中所有節點的信息
if(isEmpty()){
//如果鏈表為空
System.out.println("This
list is
empty!");
//輸出提示信息
return;
//返回到調用的地方
}
if(head==tail){
//當鏈表中只有一個節點時
System.out.println(head.info);
//輸出這個節點的信息,就是頭結點的信息
return;
}
IntNode
temp;
//定義一個中間變數
for(temp=head;temp!=null;temp=temp.next){
//遍歷整個鏈表
System.out.print(temp.info+"
");
//輸出每個節點的信息
}
System.out.println();
//輸出一個換行,可以沒有這一行
}

public boolean isInList(int
el){
//判斷el是否存在於鏈表中
IntNode
temp;
//定義一個中間變數
for(temp=head;temp!=null
&&
temp.info!=el;temp=temp.next);
//將el找出來,注意最後的分
return
temp!=null;
// 如果存在返回true,否則返回flase,這兩行代碼很有思想
}

public void delete(int
el){
//刪除鏈表中值為el的節點
if(head.info==el
&&
head==tail){
//如果只有一個節點,並且節點的值為el
head=tail=null;
//刪除這個節點
}else
if(head.info==el){
// 不止一個節點,而頭結點的值就是el
head=head.next;
//刪除頭結點
}else{
IntNode
pred,temp;
//定義兩個中間變數
for(pred=head,temp=head.next;temp.info!=el
&&
temp.next!=null;pred=pred.next,temp=temp.next);
//跟上面的類似,自己琢磨吧,也是要注意最後的分號
pred.next=temp.next;
//將temp指向的節點刪除,最好畫一個鏈表的圖,有助於理解
if(temp==tail){
//如果temp指向的節點是尾結點
tail=pred;
//將pred指向的節點設為尾結點,
}
}
}

//下面這個方法是在鏈表中值為el1的節點前面插入一個值為el2的節點,
//用類似的思想可以再寫一個在鏈表中值為el1的節點後面插入一個值為el2的節點
public boolean insertToList(int el1,int
el2){
//定義一個插入節點的方法,插入成功返回true,否則返回false
IntNode
pred,temp; //定義兩個中間變數
if(isEmpty()){
//判斷鏈表是否為空
return
false;
//如果鏈表為空就直接返回false
}
if(head.info==el1
&&
head==tail){
//如果鏈表中只有一個節點,並且這個節點的值是el1
head=new
IntNode(el2,head);
//新建立一個節點
return
true;
}else if(head.info==el1){
IntNode t=new
IntNode(el2);
t.next=head;
head=t;
return
true;
}else{
for(pred=head,temp=head.next;temp!=null
&&
temp.info!=el1;pred=pred.next,temp=temp.next);
if(temp!=null){
IntNode
a=new IntNode(el2);
pred.next=a;
a.next=temp;
return
true;
}else{
System.out.println(el1+"
NOT EXEISTS!");
return
false;
}
}
}

3.下面是測試代碼
public static void main(String[] args){
IntSLList test=new
IntSLList();

//test.addToHead(7);
test.addToTail(7);

System.out.println(test.insertToList(7,5));
test.printAll();
System.out.println(test.isInList(123));
}
}

㈢ . java怎麼創建鏈表

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如何實現鏈表

鏈表是一種重要的數據結構,在程序設計中佔有很重要的地位。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中如何實現雙向鏈表

雙向鏈表:就是有雙向指針,即雙向的鏈域。
鏈結點的結構:
┌────┬────┬────────┐
│ data │ next │ previous │
└────┴────┴────────┘
雙向鏈表不必是雙端鏈表(持有對最後一個鏈結點的引用),雙端鏈表插入時是雙向的。
有兩條鏈:一條從頭到尾,一條從尾到頭,刪除遍歷時也是雙向的。
/**
* 雙向鏈表
*/
public class DoublyLinkedList<t> {
private Link<t> head; //首結點
private Link<t> rear; //尾部指針
public DoublyLinkedList() { }
public T peekHead() {
if (head != null) {
return head.data;
}
return null;
}
public boolean isEmpty() {
return head == null;
}
public void insertFirst(T data) {// 插入 到 鏈頭
Link<t> newLink = new Link<t>(data);
if (isEmpty()) {//為空時,第1次插入的新結點為尾結點
rear = newLink;
} else {
head.previous = newLink; //舊頭結點的上結點等於新結點
}
newLink.next = head; //新結點的下結點舊頭結點
head = newLink; //賦值後,頭結點的下結點是舊頭結點,上結點null
}
public void insertLast(T data) {//在鏈尾 插入
Link<t> newLink = new Link<t>(data);
if (isEmpty()) {
head = newLink;
} else {
rear.next = newLink;
}
newLink.previous = rear;
rear = newLink; //賦值後,尾結點的上結點是舊尾結點,下結點null
}
public T deleteHead() {//刪除 鏈頭
if (isEmpty()) return null;
Link<t> temp = head;
head = head.next; //變更首結點,為下一結點
if (head != null) {
head.previous = null;
} else {
rear = null;
}
return temp.data;
}
public T deleteRear() {//刪除 鏈尾
if (isEmpty()) return null;
Link<t> temp = rear;
rear = rear.previous; //變更尾結點,為上一結點
if (rear != null) {
rear.next = null;
} else {
head = null;
}
return temp.data;
}
public T find(T t) {//從頭到尾find
if (isEmpty()) {
return null;
}
Link<t> find = head;
while (find != null) {
if (!find.data.equals(t)) {
find = find.next;
} else {
break;
}
}
if (find == null) {
return null;
}
return find.data;
}
public T delete(T t) {
if (isEmpty()) {
return null;
}
Link<t> current = head;
while (!current.data.equals(t)) {
current = current.next;
if (current == null) {
return null;
}
}
if (current == head) {
head = head.next;
if (head != null) {
head.previous = null;
}
} else if (current == rear) {
rear = rear.previous;
if (rear != null) {
rear.next = null;
}
} else {
//中間的非兩端的結點,要移除current
current.next.previous = current.previous;
current.previous.next = current.next;
}
return current.data;
}
public boolean insertAfter(T key, T data) {//插入在key之後, key不存在return false
if (isEmpty()) {
return false;
}
Link<t> current = head;
while (!current.data.equals(key)) {
current = current.next;
if (current == null) {
return false;
}
}
Link<t> newLink = new Link<t>(data);
if (current == rear) {
rear = newLink;
} else {
newLink.next = current.next;
current.next.previous = newLink;
}
current.next = newLink;
newLink.previous = current;
return true;
}
public void displayList4Head() {//從頭開始遍歷
System.out.println("List (first-->last):");
Link<t> current = head;
while (current != null) {
current.displayLink();
current = current.next;
}
}
public void displayList4Rear() {//從尾開始遍歷
System.out.println("List (last-->first):");
Link<t> current = rear;
while (current != null) {
current.displayLink();
current = current.previous;
}
}

class Link<t> {//鏈結點
T data; //數據域
Link<t> next; //後繼指針,結點 鏈域
Link<t> previous; //前驅指針,結點 鏈域
Link(T data) {
this.data = data;
}
void displayLink() {
System.out.println("the data is " + data.toString());
}
}
public static void main(String[] args) {
DoublyLinkedList<integer> list = new DoublyLinkedList<integer>();
list.insertLast(1);
list.insertFirst(2);
list.insertLast(3);
list.insertFirst(4);
list.insertLast(5);
list.displayList4Head();
Integer deleteHead = list.deleteHead();
System.out.println("deleteHead:" + deleteHead);
list.displayList4Head();
Integer deleteRear = list.deleteRear();
System.out.println("deleteRear:" + deleteRear);
list.displayList4Rear();
System.out.println("find:" + list.find(6));
System.out.println("find:" + list.find(3));
System.out.println("delete find:" + list.delete(6));
System.out.println("delete find:" + list.delete(1));
list.displayList4Head();
System.out.println("----在指定key後插入----");
list.insertAfter(2, 8);
list.insertAfter(2, 9);
list.insertAfter(9, 10);
list.displayList4Head();
}
}

㈥ java 中如何實現鏈表操作

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基本鏈表

實現鏈表的思路:1)鏈表類,結點類(鏈表類的內部類),在main()方法創建一條鏈表類對象,通過方法逐步創建結點類,通過引用鏈接起來成為鏈表。2)結點類包含數據和對下個結點的引用,以及可以對數據賦值的構造函數。3)鏈表類的構造方法,只構造出不含數據的頭結點。(外部類可以直接對內部類的私有成員進行訪問,這樣就可以直接修改引用)

㈧ Java鏈表

鏈表是一種重要的數據結構,在程序設計中佔有很重要的地位。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類的代碼稍加改動即可。

如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!

vaela

㈨ java怎麼用鏈表實現

在數據結構中經常看見的一個基本概念-鏈表。
鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。鏈表由一系列結點(鏈表中每一個元素稱為結點)組成,結點可以在運行時動態生成。每個結點包括兩個部分:一個是存儲數據元素的數據域,另一個是存儲下一個結點地址的指針域。
在Java中,對於鏈表的實現都是基於引用數據類型操作的。實現大致如下:
定義節點類Node,節點的概念很重要,一個鏈表是由各各節點連接在一起組成的。在節點類Node中定義節點內容及指向下一節點的引用,再增加一個添加節點的方法即可完成鏈表實現。
鏈表有很多種不同的類型:單向鏈表,雙向鏈表以及循環鏈表。在執行效率上,相比數組而言,鏈表插入快查找慢,開發中得根據實際業務使用。

㈩ java 鏈表實現(測試是否有環)

package com.bb.bbs; import java.util.ArrayList; /** * 節點類:用於保存鏈表中的節點 */ class Node{ Node next; String data;//下一節點 public static int maxs = 0;//getSize() 最大數量 public static int maxg = 0;//getArray()最大數量 public static int maxp = 0;//printNode()列印節點最大數量 public static int maxc = 0;//contains()最大數量 /** * 帶參數的構造方法 */ public Node(String data){ this.data = data; } /** * 在當前節點上增加一個節點 */ public void addNode(Node node){ if(this.next==null){ this.next = node; }else{ this.next.addNode(node); } } /** * 從root開始尋找,目的是移除一個節點 */ public boolean removeNode(Node previous,String data){ if(this.data.equals(data)){ previous.next = this.next; return true; }else{ return this.next.removeNode(this, data); } } /** * 是否包含節點 */ public boolean contains(String data){ maxc++; if(maxc==10){ return false; } if(this.data.equals(data)){ return true; } if(this.next == null){ return false; }else{ return this.next.contains(data); } } /** * 列印一個節點 */ public void printNode(){ maxp++; if(maxp==10){ return; } if(this.next!=null){ System.out.println(this.next.data); this.next.printNode(); } } /** * 查找並返回一個節點 */ public Node findNode(String data){ if(this.data.equals(data)){ return this; }else{ return this.next.findNode(data); } } /** * 得到鏈表大小 */ public int getSize(int currentNum){ maxs++; if(maxs==10){ return 10; } if(this!=null){ currentNum++; } if(this.next!=null){ return this.next.getSize(currentNum); }else{ return currentNum; } } /** * 將節點里所有值封裝到一個ArrayList中 */ public void getArray(ArrayList tArrayList){ maxg++; if(maxg==10){ return; } tArrayList.add(this.data); if(this.next!=null){ this.next.getArray(tArrayList); } } } /** * 鏈表類 */ class Link{ Node root; public void add(String data){ if(data==null){ return; } if(root ==null){ root = new Node(data); }else{ root.addNode(new Node(data)); } } public boolean remove(String data){ if(root.data.equals(data)){ root = root.next; return true; }else{ return this.root.next.removeNode(root, data); } } public boolean contains(String data){ if(root.data.equals(data)){ return true; }else{ return root.contains(data); } } public void print(){ if(root!=null){ System.out.println(root.data); root.printNode(); } } public Node find(String data){ if(contains(data)){ if(this.root.data.equals(data)){ return root; }else{ return root.findNode(data); } } return null; } public int size(){ if(root.next ==null){ return 1; }else{ return root.next.getSize(1); } } public void getArray(ArrayList tArrayList){ if(root!=null){ tArrayList.add(root.data); } if(root.next!=null){ root.next.getArray(tArrayList); } } } /** * 測試入口 */ public class LinkList { public static void main(String args[]){ //1、增加鏈表節點 Link tLink = new Link(); tLink.add("A"); tLink.add("B"); tLink.add("C"); tLink.add("D"); tLink.print(); //2、形成環 A->B->C->A->B->C->A->B->C...... 無限循環 Node fnodeA = tLink.find("A"); Node fnodeC = tLink.find("C"); //如果有一個為空 if(fnodeA==null || fnodeC==null){ System.out.println("沒有找到元素!"); fnodeC.next = fnodeA;//出現環 } int linksize = tLink.size(); System.out.println("鏈表大小為:"+linksize); ArrayList tArrayList = new ArrayList(); tLink.getArray(tArrayList); for(Object o : tArrayList){ String str = o.toString(); System.out.println(str); } boolean flag = false; int circleLen = 0; //檢驗 "環" 是否存在 for(int i=0;i

熱點內容
android工程師筆試 發布:2024-05-05 00:10:52 瀏覽:948
python調試pycharm 發布:2024-05-05 00:10:51 瀏覽:707
索尼電腦vaio忘了密碼如何恢復出廠設置 發布:2024-05-05 00:09:56 瀏覽:895
安卓系統的用戶管理在哪裡 發布:2024-05-04 23:12:27 瀏覽:430
我的世界伺服器推薦電腦版免費 發布:2024-05-04 23:04:46 瀏覽:395
c程序如何編譯 發布:2024-05-04 22:58:05 瀏覽:932
蘋果手機怎麼查看id密碼 發布:2024-05-04 22:54:49 瀏覽:658
家有三相電如何配置音響設備 發布:2024-05-04 22:53:42 瀏覽:56
三星存儲器已幾乎滿 發布:2024-05-04 22:47:38 瀏覽:737
mf90pos機密碼是什麼 發布:2024-05-04 22:24:04 瀏覽:750