
正文
Java链表的代码 java链表的基本操作
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java实现链表求救
import java.util.Scanner;
public class Node {
private int data;
private Node next;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] numStr = s.split(",");
Node head = buildList(numStr);
printList(head);
Node node1 = new Node();
node1.setData(100);
node1.setNext(null);
Node node2 = new Node();
node2.setData(100);
node2.setNext(null);
Node temp = head;
node1.setNext(temp);
head = node1;
printList(head);
temp = head;
while(temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(node2);
printList(head);
}
//构建链表
public static Node buildList(String[] numStr) {
if(0 == numStr.length) {
return null;
}
Node head = new Node();
head.setData(Integer.parseInt(numStr[0]));
head.setNext(null);
Node temp = head;
for(int i = 1; i numStr.length; i++) {
Node node = new Node();
node.setData(Integer.parseInt(numStr[i]));
node.setNext(null);
temp.setNext(node);
temp = node;
}
return head;
}
//输出链表
public static void printList(Node head) {
Node temp = head;
while(temp != null) {
System.out.print(temp.getData() + "--");
temp = temp.getNext();
}
System.out.println();
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
相关问答
Q1: 用JAVA语言这以下程序代码。 链表类 1.能够从头部添加节点。 2.能够从尾部添加节点。 3.
package com.llist;
public class SingleLinkListT implements LListT {
protected NodeT head; // 头指针,指向单链表的头结点
// 默认构造方法,构造空单链表
public SingleLinkList(){
head = new NodeT();
}
// 由指定数组中的多个对象构造单链表,采用尾查构造单链表
public SingleLinkList(T[] element){
this();
NodeT rear = this.head;
for(int i=0;ielement.length;i++){
rear.next = new NodeT(element[i],null);
rear = rear.next;
}
}
// 判断链表是否为空
public boolean isEmpty() {
return this.head.next==null;
}
// 返回链表的长度
public int length() {
int count = 0;
NodeT p = this.head.next;
while(p!=null){
count++;
p = p.next;
}
return count;
}
// 返回第i(i0)个元素,若i的指定序号无效,则返回null
public T get(int i) {
if(i=0||ithis.length()){
return null;
}
else{
NodeT p = this.head;
for(int j=0;ji;j++){
p = p.next;
}
return p.data;
}
}
// 设置第i(i0)个元素的值为x,若i指定序号无效,则抛出序号越界异常
public void set(int i, T x) {
if(x==null)
return;
if(i=0||ithis.length()){
// 抛出序号越界异常
throw new IndexOutOfBoundsException("指定序号i="+i+"越界!");
}
else{
NodeT p = this.head;
for(int j=0;ji;j++){
p = p.next;
}
p.data = x;
}
}
// 链表的插入操作
public void insert(int i, T x) {
if(x==null) // 插入对象不能为空
return;
if(i==0){ // 插在头结点之后
head.next = new NodeT(x,this.head.next);
}
else if(i0i=this.length()){
NodeT p = this.head;
// 寻找插入位置
for(int j=0;ji;j++){
p = p.next;
}
// 插入x作为p结点的后继结点
p.next = new NodeT(x,p.next);
}
else{
// 抛出序号越界异常
throw new IndexOutOfBoundsException("指定序号i="+i+"越界!");
}
}
// 在单链表的最后添加对象
public void append(T x) {
insert(this.length(),x);
}
// 删除序号为i的结点,操作成功返回给对象,否则返回null
public T remove(int i) {
if(i=0||ithis.length()){
return null;
}
else{
NodeT p = this.head;
// 定位到待删除结点(i)的前驱结点(i-1)
for(int j=0;ji-1;j++){
p = p.next;
}
T old = p.next.data;
p.next = p.next.next;
return old;
}
}
// 删除单链表的所有元素
public void removeAll() {
this.head.next = null;
}
// 查找,返回首次出现关键子key的元素的序号
public int Search(T key) {
if(key==null)
return 0;
else{
int i = 0;
NodeT p = this.head;
while(p.next!=null){
i++;
p = p.next;
if(p.data.equals(key))
break;
}
if(p.next==null)
return 0;
else
return i;
}
}
//返回链表的所有元素的描述字符串,覆盖Object类的toString()方法
public String toString(){
String str = " ( ";
NodeT p = this.head.next;
while(p!=null){
str += p.data.toString();
p = p.next;
if(p!=null)
str += ",";
}
str += " ) ";
return str;
}
}
Q2: java中如何创建一个链表?望能给我详细的代码。加点注释。谢谢
//如果感兴趣的话,可以把下面的改成泛型的也就是这样的
//一个学生的类
public class Stu(){
String name;
int age;
public Stu(String name,int age){
this.name=name;
this.age=age;
}
}
//创建两个学生的对像
Stu stu1=new Stu("weiwie",24);
Stu stu2=new Stu("xiaoqiang",25);
//创建集合类,存放的是Stu对像,这样的声明只能存Stu对像
List Stu list=new ArrayListStu();
//存数据
list.add(stu1);
list.add(stu2);
//遍历
for(int i=0;ilist.size();i++){
//向下转型方便了,取出来的就是Stu对像
Stu stu=list.get(i);
}
List list=new ArrayList();
list.add("对像");
遍历
for(int i=0;ilist.size();i++){
//需要强转
String str=(String)list.get(i);
得到你存放的数据
}
Map map=new HashMap();
//存值
map.put("one","对像");
//取值
String str=(String)map.get("one");
Set set=new HashSet();
//存值
set.add("对像");
//需要用这个对像遍历
Iterator iter=set.iterator();
while(iter.hasNext()){
//取值
String Str=(String)iter.next();
}
Q3: 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类的代码稍加改动即可。
希望对你有帮助。
Q4: 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类的构造函数
链表是一种重要的数据结构Java链表的代码,在程序设计中占有很重要的地位。C语言和C++语
言中是用指针来实现链表结构的,由于Java语言不提供指针,所以有人认为在
Java语言中不能实现链表,其实不然,Java语言比C和C++更容易实现链表结构
。Java语言中的对象引用实际上是一个指针(本文中的指针均为概念上的意义,
而非语言提供的数据类型),所以Java链表的代码我们可以编写这样的类来实现链表中的结点。
class Node
{
Object data;
Node next;//指向下一个结点
}
将数据域定义成Object类是因为Object类是广义超类,任何类对象都可以给
其赋值,增加Java链表的代码了代码的通用性。为了使链表可以被访问还需要定义一个表头,表
头必须包含指向第一个结点的指针和指向当前结点的指针。为了便于在链表尾部
增加结点,还可以增加一指向链表尾部的指针,另外还可以用一个域来表示链表
的大小,当调用者想得到链表的大小时,不必遍历整个链表。下图是这种链表的
示意图:
链表的数据结构
Java链表的代码我们可以用类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链表的基本操作、Java链表的代码的信息别忘了在本站进行查找喔。








