导航:首页 > 编程语言 > 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链表代码相关的资料

热点内容
服务器地址和ip地址一样不 浏览:662
php中括号定义数组 浏览:600
php打印堆栈 浏览:514
华为adb命令行刷机 浏览:963
人像摄影pdf 浏览:755
解压文件密码怎样重新设置手机 浏览:999
高考指南pdf 浏览:693
爬虫python数据存储 浏览:240
u盘怎么取消加密 浏览:429
567除以98的简便算法 浏览:340
pdf手机如何解压 浏览:15
python描述器 浏览:60
战地联盟3解压密码 浏览:805
s型命令 浏览:25
php年薪5年 浏览:71
如何上网上设个人加密账户 浏览:44
linux打开ssh服务 浏览:78
微信位置可以加密吗 浏览:470
算法蛮力法 浏览:438
随机排练命令 浏览:147