当前位置:首页 » 编程语言 » java循环链表

java循环链表

发布时间: 2022-06-20 17:39:33

A. java怎么用链表实现

在数据结构中经常看见的一个基本概念-链表。
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
在Java中,对于链表的实现都是基于引用数据类型操作的。实现大致如下:
定义节点类Node,节点的概念很重要,一个链表是由各各节点连接在一起组成的。在节点类Node中定义节点内容及指向下一节点的引用,再增加一个添加节点的方法即可完成链表实现。
链表有很多种不同的类型:单向链表,双向链表以及循环链表。在执行效率上,相比数组而言,链表插入快查找慢,开发中得根据实际业务使用。

B. 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类的代码稍加改动即可。

希望对你有帮助。

C. java 有循环链表吗

网页链接

java可以实现链表的,不管是单链表、双链表还是循环链表。

D. 用java语言编写单循环链表约瑟夫生死游戏

LinkList p;; /link,int k;*循环地删除队列结点*,m为出列者喊到的数
{
/
}
p-;n最后被删除的元素是;
for(int i=0;data);link;
for(i=0;*使链表循环起来*,int m) /m-1,P-;
p=p-;;i++)
{
p=(LinkList)malloc(sizeof(LNode)); /link,p-;ilt;
}
r-;n为总人数;link;
p=p-;;
p=list;
p=r-;
}
,k为第一个开始报数的人;i++)
{
r=p;* p为当前结点 r为辅助结点;
⒊不断地从链表中删除链结点;
else
r-;link=p;
free(p),r;
p-;data);,无头结点的循环链表;*建立循环链表*:%4dquot;ilt;*使p指向头节点*!=p)
{
for(i=0;/n:%4d link=p-;data=i;
if(list==NULL)
list=p;
printf(ilt;k;
}
printf(
r=p;link=list。
void JOSEPHUS(int n;*把当前指针移动到第一个报数的人*/
/i++)
{
r=p,指向p的前驱结点 list为头节点*/link解决问题的核心步骤,list:(程序的基本算法
⒈建立一个具有n个链结点;
⒉确定第1个报数人的位置;
while(p-,直到链表为空;被删除的元素

E. Java循环双链表如何深拷贝子表

双链结构的话,在拷贝的过程中是直接可以用用复制粘贴就可以完成了的。

F. java循环单链表实现约瑟夫环

看了你的代码,不是很明白,给你提几个建议吧:

1、不需要tail节点

2、remove方法应该对删除节点前面的节点操作,而不是使用数字找

给你我修改的LinkList类,你参考一下:

publicclassLinkList{
privateNodehead;
intcurlen=0;

//创建链表
publicvoidcreatelist(intcode)throwsException{
insert(curlen,code);
}

publicvoidinsert(inti,intcode)throwsException{
Nodes=newNode(code);
if(i==0){
s.setNext(head);
head=s;
}
Nodep=head;
intj=0;
while(p!=null&&j<i-1){
p=p.getNext();
j++;
}
if(j>i||p==null){
thrownewException("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
// tail=s;
// tail.setNext(head);
curlen=curlen+1;
}

publicvoidremove(inti)throwsException{
Nodep=head,q=null;
intj=0;
i=i-1;
while(j<i){
q=p;
p=p.getNext();
j++;
}
if(j>i||p==null)
thrownewException("删除位置不合法");
if(q==null){
// tail.setNext(p.getNext());
head=head.getNext();
}else
q.setNext(p.getNext());
curlen=curlen-1;
}
/**
*按照节点删除
*@parami
*@throwsException
*/
publicvoidremove(Nodep)throwsException{
if(p.getNext()==p){
p=null;
head=null;
}
else{
Nodeq=p.getNext();
p.setNext(q.getNext());
}
curlen=curlen-1;
}

publicvoidout(intm)throwsException{
Nodep=head;
if(m==1){
System.out.print("按照顺序出列");
return;
}
intcount=1;
intn=m-1;
while(curlen>0){
if(count==n){
System.out.print(p.getNext().getData()+"");
remove(p);
count=1;
}else{
count++;
}
p=p.getNext();
}

}

publicvoiddisplay(){
Nodenode=head;
for(inti=0;i<2*curlen;i++){
System.out.print(node.getData()+"");
node=node.getNext();
}
System.out.println();
}
}

G. 一个关于Java链表循环遍历,排序的问题。高手上了!!!

package com.neusoft.main;
import java.util.ArrayList;import java.util.List;
public class TestCoordinate {
/**
* <p>Discription:[方法功能中文描述]</p>
* @param args
* @author:[滕一汉]
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/

public static void main(String[] args) {
List list = new ArrayList();
list.add( new CoordinateObj(10,10));
list.add( new CoordinateObj(20,20));
list.add( new CoordinateObj(30,30));
list.add( new CoordinateObj(40,40));
list.add( new CoordinateObj(50,50));
list.add( new CoordinateObj(60,60));
list.add( new CoordinateObj(70,70));
list.add( new CoordinateObj(80,80));
list.add( new CoordinateObj(90,90));
list.add( new CoordinateObj(100,100));
getNearestCoor(list, new CoordinateObj(0, 0));
}

public static CoordinateObj getNearestCoor(List list, CoordinateObj nowCoor) {
double dis = 0d;
CoordinateObj newcoo = null;
for (int i = 0; i < list.size(); i++) {
CoordinateObj coo = (CoordinateObj) list.get(i);
double x = coo.getX() - nowCoor.getX();
double y = coo.getY() - nowCoor.getY();
double newDis = Math.hypot(x, y);
if (dis == 0) {
dis = newDis;
newcoo = coo;
}
else {
if (newDis < dis) {
newcoo = coo;
dis = newDis;
}
}
}
list.remove(newcoo);
System.out.println("x:" + newcoo.getX() + "---y:" + newcoo.getY());
if(list.size()>0)
getNearestCoor(list,newcoo);

return newcoo;
}

}

package com.neusoft.main;
public class CoordinateObj {
public CoordinateObj(){}
CoordinateObj(double x,double y){
this.x = x;
this.y = y;
}

double x;//x 坐标

double y;//y 坐标
public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

}

H. java中的循环链表怎么写

设置 last.next = head
head.pre=last

I. Java约瑟夫循环单循环链表

importjava.util.List;
importjava.util.ArrayList;

publicclassProgram{
/**
*为参与约瑟夫循环的<code>人数</code>。
*/
publicfinalstaticintGAME_PEOPLE_NUMBER=10;

publicstaticvoidmain(String...args){
List<GameObject>gameObjects=newArrayList<>();
//初始化参与约瑟夫角色的信息,必要时可以更改为从键盘输入。但此处只为演示,so,省略...
for(inti=0;i<Program.GAME_PEOPLE_NUMBER;i++){
gameObjects.add(newGameObject(i+1,i+1,"nn-"+i));
}

inti=0,c=0,m=2;
while(gameObjects.size()>0){
c++;
if(c==m){
System.out.println(gameObjects.get(i));
m=gameObjects.get(i).getPassword();
gameObjects.remove(i);
c=0;
}

i++;
if(i>=gameObjects.size())i=0;
}
}
}

classGameObject{
privateintid;
privateintpassword;
privateStringnickName;

publicintgetId(){
returnid;
}

publicvoidsetId(intid){
this.id=id;
}

publicintgetPassword(){
returnpassword;
}

publicvoidsetPassword(intpassword){
this.password=password;
}

publicStringgetNickName(){
returnnickName;
}

publicvoidsetNickName(StringnickName){
this.nickName=nickName;
}

publicGameObject(){
}

publicGameObject(intid,intpassword,StringnickName){
this.id=id;
this.password=password;
this.nickName=nickName;
}

@Override
publicStringtoString(){
return"出队[id="+id+",password="+password+",nickName="+nickName+"]";
}
}

J. java循环双链表类添加求平均值的方法

public class DoubleLinkedList
{
// 节点类Node

private static class Node
{
Object value;
Node prev = this;
Node next = this;

Node(Object v)
{
value = v;
}

public String toString()
{
return value.toString();
}
}
private Node head = new Node(null); // 头节点
private int size; // 链表大小
// 以下是接口方法

public boolean addFirst(Object o)
{
addAfter(new Node(o), head);
return true;
}

public boolean addLast(Object o)
{
addBefore(new Node(o), head);
return true;
}

public boolean add(Object o)
{
return addLast(o);
}

public boolean add(int index, Object o)
{
addBefore(new Node(o), getNode(index));
return true;
}

public boolean remove(int index)
{
removeNode(getNode(index));
return true;
}

public boolean removeFirst()
{
removeNode(head.next);
return true;
}

public boolean removeLast()
{
removeNode(head.prev);
return true;
}

public Object get(int index)
{
return getNode(index).value;
}

public int size()
{
return size;
}

public String toString()
{
StringBuffer s = new StringBuffer("[");
Node node = head;
for (int i = 0; i < size; i++)
{
node = node.next;
if (i > 0)
s.append(", ");
s.append(node.value);
}
s.append("]");
return s.toString();
}

private Node getNode(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException();
Node node = head.next;
for (int i = 0; i < index; i++)
node = node.next;
return node;
}

private void addBefore(Node newNode, Node node)
{
newNode.next = node;
newNode.prev = node.prev;
newNode.next.prev = newNode;
newNode.prev.next = newNode;
size++;
}

private void addAfter(Node newNode, Node node)
{
newNode.prev = node;
newNode.next = node.next;
newNode.next.prev = newNode;
newNode.prev.next = newNode;
size++;
}

private void removeNode(Node node)
{
node.prev.next = node.next;
node.next.prev = node.prev;
node.prev = null;
node.next = null;
size--;
}
}

//测试类:

public class Test
{
public static void main(String[] args)
{
DoubleLinkedList dll = new DoubleLinkedList();
//添加
dll.add("张三");
dll.add("李四");
dll.add("王五");
System.out.println(dll);

//添加到最前
dll.addFirst("孙七");
System.out.println(dll);

//添加到最后,同添加
dll.addLast("赵六");
System.out.println(dll);

//添加到指定位置
dll.add(4, "王祖贤");
System.out.println(dll);

//移除最前的
dll.removeFirst();
System.out.println(dll);

//移除最后的
dll.removeLast();
System.out.println(dll);

//移除指定位置上的
dll.remove(2);
System.out.println(dll);

//返回指定位置上的元素
System.out.println(dll.get(1));

}
}

热点内容
编程一首诗 发布:2025-02-06 06:45:04 浏览:528
惊声尖笑5下载ftp 发布:2025-02-06 06:33:16 浏览:528
共享文件夹让输入密码 发布:2025-02-06 06:32:28 浏览:970
收银服务器响应出错什么意思 发布:2025-02-06 06:24:43 浏览:607
sql用户授权 发布:2025-02-06 06:24:42 浏览:677
苹果手机相册显示正在上传 发布:2025-02-06 06:05:43 浏览:542
hadoop下载文件夹 发布:2025-02-06 06:05:08 浏览:187
铠最强配置是哪些 发布:2025-02-06 06:04:22 浏览:360
编译器的制作环境 发布:2025-02-06 05:54:34 浏览:829
学车网源码 发布:2025-02-06 05:47:40 浏览:386