導航:首頁 > 編程語言 > java鏈表代碼

java鏈表代碼

發布時間:2022-07-31 12:21:27

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實現鏈表求救

importjava.util.Scanner;

publicclassNode{
privateintdata;
privateNodenext;

publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
Strings=sc.nextLine();
String[]numStr=s.split(",");
Nodehead=buildList(numStr);
printList(head);

Nodenode1=newNode();
node1.setData(100);
node1.setNext(null);
Nodenode2=newNode();
node2.setData(100);
node2.setNext(null);

Nodetemp=head;
node1.setNext(temp);
head=node1;
printList(head);

temp=head;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(node2);
printList(head);
}

//構建鏈表
publicstaticNodebuildList(String[]numStr){
if(0==numStr.length){
returnnull;
}
Nodehead=newNode();
head.setData(Integer.parseInt(numStr[0]));
head.setNext(null);
Nodetemp=head;
for(inti=1;i<numStr.length;i++){
Nodenode=newNode();
node.setData(Integer.parseInt(numStr[i]));
node.setNext(null);
temp.setNext(node);
temp=node;
}
returnhead;
}

//輸出鏈表
publicstaticvoidprintList(Nodehead){
Nodetemp=head;
while(temp!=null){
System.out.print(temp.getData()+"--");
temp=temp.getNext();
}
System.out.println();
}

publicintgetData(){
returndata;
}

publicvoidsetData(intdata){
this.data=data;
}

publicNodegetNext(){
returnnext;
}

publicvoidsetNext(Nodenext){
this.next=next;
}
}

❸ Java單向鏈表代碼。

這是我寫的一個差不多,你看一下吧:
package com.test.list;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LinkedList {

public static void main(String[] args) {

MyList l = new MyList();
MyListNode node = l.createList();
l.printNode(node);
//l.searchNode(node, 4);
//node = l.insertNode(node, 3, "g");
//l.printNode(node);
node = l.deleteNode(node, "d");
l.printNode(node);
}
}

class MyListNode {

public String data;

public MyListNode nextNode;
}

class MyList {

public MyListNode createList() {

MyListNode node = new MyListNode();

MyListNode q ,p;
q = new MyListNode();
q = node;

while (true) {

String s = null;

try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("請輸入節點數據:");
s = br.readLine();

if (s.equals("0")) {
break;
} else {
p = new MyListNode();
p.data = s;
p.nextNode = null;
q.nextNode = p;
q = p;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return node;
}

public void printNode(MyListNode node) {

MyListNode p = node.nextNode;
while (p!= null) {
System.out.print(" "+p.data);
p = p.nextNode;
}
}

public void searchNode(MyListNode node, int i){

MyListNode p = node.nextNode;

int j = 1;
while (p != null && j<i) {
p = p.nextNode;
j++;
}
if( p == null || j>i) {
System.out.println("error");
}

System.out.println(" --"+p.data+"--");
}

public MyListNode insertNode(MyListNode node, int i ,String s) {

MyListNode p = node.nextNode;

int j = 1;
while (p != null && j<i-1) {
p = p.nextNode;
j++;
}
if( p == null || j>i-1) {
System.out.println("error");
}

MyListNode n = new MyListNode();
n.data = s;
n.nextNode = p.nextNode;
p.nextNode = n;

return node;

}

public MyListNode deleteNode(MyListNode node ,String s) {

MyListNode p = node;

while(p.nextNode != null && !p.nextNode.data.equals(s)) {
p = p.nextNode;
}

p.nextNode = p.nextNode.nextNode;

return node;
}
}

/*逆位序創建
public MyListNode createList() {

MyListNode node = new MyListNode();
node.nextNode = null;

while(true) {

String s = null;

try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("請輸入節點數據:");
s = br.readLine();

if(s.equals("0")) {
break;
}else {
MyListNode n = new MyListNode();
n.data = s;
n.nextNode = node.nextNode;
node.nextNode = n;
}
} catch (Exception e) {
e.printStackTrace();
}

}
return node;
}
*/

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

  1. Java語言中的對象引用實際上是一個指針(這里的指針均為概念上的意義,而非語言提供的數據類型),所以我們可以編寫這樣的類來實現鏈表中的結點。
    程序代碼:

  2. classNode
    {
    Objectdata;
    Nodenext;//指向下一個結點
    }
  3. 將數據域定義成Object類是因為Object類是廣義超類,任何類對象都可以給其賦值,增加了代碼的通用性。為了使鏈表可以被訪問還需要定義一個表頭,表頭必須包含指向第一個結點的指針和指向當前結點的指針。為了便於在鏈表尾部增加結點,還可以增加一指向鏈表尾部的指針,另外還可以用一個域來表示鏈表的大小,當調用者想得到鏈表的大小時,不必遍歷整個鏈表。
    鏈表的數據結構我們可以用類List來實現鏈表結構,用變數Head、Tail、Length、Pointer來實現表頭。存儲當前結點的指針時有一定的技巧,Pointer並非存儲指向當前結點的指針,而是存儲指向它的前趨結點的指針,當其值為null時表示當前結點是第一個結點,因為當刪除當前結點後仍需保證剩下的結點構成鏈表,如果Pointer指向當前結點,則會給操作帶來很大困難。如何得到當前結點呢?我們定義了一個方法cursor(),返回值是指向當前結點的指針。類List還定義了一些方法來實現對鏈表的基本操作,通過運用這些基本操作我們可以對鏈表進行各種操作。例如reset()方法使第一個結點成為當前結點。insert(Object d)方法在當前結點前插入一個結點,並使其成為當前結點。remove()方法刪除當前結點同時返回其內容,並使其後繼結點成為當前結點,如果刪除的是最後一個結點,則第一個結點變為當前結點。
    鏈表類List的源代碼如下:

  4. packagecn.javatx;importjava.io.IOException;/**
    *@authorljfan
    *
    */
    publicclassList{
    privateNodeHead=null;
    privateNodeTail=null;
    privateNodePointer=null;
    privateintLength=0;publicvoiddeleteAll(){
    Head=null;
    Tail=null;
    Pointer=null;
    Length=0;
    }publicvoidreset(){
    Pointer=null;
    }publicbooleanisEmpty(){
    return(Length==0);
    }publicbooleanisEnd(){
    if(Length==0)
    thrownewjava.lang.NullPointerException();
    elseif(Length==1)
    returntrue;
    else
    return(cursor()==Tail);
    }publicObjectnextNode(){
    if(Length==1)
    thrownewjava.util.NoSuchElementException();
    elseif(Length==0)
    thrownewjava.lang.NullPointerException();
    else{
    Nodetemp=cursor();
    Pointer=temp;
    if(temp!=Tail)
    return(temp.next.data);
    else
    thrownewjava.util.NoSuchElementException();
    }
    }publicObjectcurrentNode(){
    Nodetemp=cursor();
    returntemp.data;
    }publicvoidinsert(Objectd){
    Nodee=newNode(d);
    if(Length==0){
    Tail=e;
    Head=e;
    }else{
    Nodetemp=cursor();
    e.next=temp;
    if(Pointer==null)
    Head=e;
    else
    Pointer.next=e;
    }
    Length++;
    }publicintsize(){
    return(Length);
    }publicObjectremove(){
    Objecttemp;
    if(Length==0)
    thrownewjava.util.NoSuchElementException();
    elseif(Length==1){
    temp=Head.data;
    deleteAll();
    }else{
    Nodecur=cursor();
    temp=cur.data;
    if(cur==Head)
    Head=cur.next;
    elseif(cur==Tail){
    Pointer.next=null;
    Tail=Pointer;
    reset();
    }else
    Pointer.next=cur.next;
    Length--;
    }
    returntemp;
    }privateNodecursor(){
    if(Head==null)
    thrownewjava.lang.NullPointerException();
    elseif(Pointer==null)
    returnHead;
    else
    returnPointer.next;
    }publicstaticvoidmain(String[]args){
    Lista=newList();
    for(inti=1;i<=10;i++)
    a.insert(newInteger(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("ThereisnoNodeinListn");
    System.out.println("Youcanpressreturntoquitn");
    try{
    System.in.read();
    }catch(IOExceptione){
    }
    }
    }classNode{
    Objectdata;
    Nodenext;Node(Objectd){
    data=d;
    next=null;
    }
    }
  5. 當然,雙向鏈表基本操作的實現略有不同。鏈表和雙向鏈表的實現方法,也可以用在堆棧和隊列的現實中。

❺ 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語言實現單向鏈表

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代碼實現鏈表結構

List<T> list = new LinkedList<T>() 這個集合就是以鏈表結構存儲數據的

❽ 在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鏈表代碼相關的資料

熱點內容
php開發客戶端 瀏覽:998
theisle測試服怎麼搜伺服器 瀏覽:447
廣播PDF 瀏覽:218
單片機編程300例匯編百度 瀏覽:35
騰訊雲連接不上伺服器 瀏覽:223
不能用來表示演算法的是 瀏覽:861
6軸機器人演算法 瀏覽:890
手機主題照片在哪個文件夾 瀏覽:294
安卓手機後期用什麼軟體調色 瀏覽:628
cad修改快捷鍵的命令 瀏覽:242
好錢包app怎麼登錄不了 瀏覽:859
樹莓派都用python不用c 瀏覽:757
access文件夾樹的構造 瀏覽:662
安卓多指操作怎麼設置 瀏覽:658
linux樹形目錄 瀏覽:727
平方根的簡單演算法 瀏覽:898
千牛訂單頁面信息加密取消 瀏覽:558
單片機自製紅外遙控燈 瀏覽:719
伺服器最小配置怎麼弄 瀏覽:853
ibm伺服器硬體如何升級 瀏覽:923