「java实现循环链表」Java循环链表
今天给各位分享java实现循环链表的知识,其中也会对Java循环链表进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
双向循环链表java
我们也做这个,,你是是吧
package KeCheng1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Scanner;
//定义节点类
class NodeAnyType {
public AnyType data;
public NodeAnyType prev;
public NodeAnyType next;
public Node(AnyType d,NodeAnyType p,NodeAnyType n){
data=d;
prev=p;
next=n;}}
public class MyLinkedListAnyType {
private int theSize;
private NodeAnyType beginMarker; //头标记或头节点
private NodeAnyType endMarker; //尾标记或尾节点
public MyLinkedList(){
beginMarker=new NodeAnyType(null,endMarker,endMarker);
endMarker = new NodeAnyType(null,beginMarker,beginMarker);
beginMarker.next = endMarker;
theSize = 0;}
public int size(){
return theSize;}
public boolean add(AnyType x){
add(size(),x);
return true;}
public void add(int idx,AnyType x){
NodeAnyType p;
p=getNode(idx);
addBefore(p,x);}
private NodeAnyType getNode(int idx) {
NodeAnyType p;
if( idx 0 || idx size( ) )
throw new IndexOutOfBoundsException( );
if( idx size( ) / 2 ) {
p = beginMarker.next;
for( int i = 0; i idx; i++ )
p = p.next;}
else
{ p = endMarker;
for( int i = size( ); i idx; i-- )
p = p.prev; }
return p;}
private void addBefore( NodeAnyType p, AnyType x ) {
NodeAnyType newNode = new NodeAnyType( x, p.prev, p );
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;}
public AnyType remove( int idx ){
return remove( getNode(idx));}
private AnyType remove(NodeAnyType p){
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
return p.data;}
public void print(){//输出链表
for(NodeAnyTypex=beginMarker.next;x.next!=beginMarker;x=x.next)
System.out.print(x.data+" ");
System.out.print("\n");}
public AnyType get( int idx ){
return getNode( idx ).data; }
public static void main(String[] args){
MyLinkedListInteger La = new MyLinkedListInteger();
int Length;
Scanner sc = new Scanner(System.in);
System.out.println("请输入要创建的双向链表的长度:(大于6)");
Length = sc.nextInt();
System.out.println("请输入"+Length+"个元素创建La双向链表:");
for(int i=0;iLength;i++)
{La.add(sc.nextInt());}
System.out.println("输入的原La双向链表:");
La.print();
System.out.println("在双向链表的第3个位置插入20:");
La.add(3,20);
La.print();
System.out.println("删除第五位置上的数:");
La.remove(5);
La.print();
System.out.println("插入最后一个节点:99");
La.add(Length,99);
La.print();
System.out.println("插入第一个节点0:");
La.add(0,0);
La.print();
System.out.println("就地逆置:");
int M=Length+2;
int b=0;
for(Length=Length+1;Length=0;Length--){
int a=La.get(M-1);
La.add(b,a);
b=b+1;
La.remove(M);
}
La.print();
}
}
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、不需要tail节点
2、remove方法应该对删除节点前面的节点操作,而不是使用数字找
给你我修改的LinkList类,你参考一下:
public class LinkList {
private Node head;
int curlen = 0;
// 创建链表
public void createlist(int code) throws Exception {
insert(curlen, code);
}
public void insert(int i, int code) throws Exception {
Node s = new Node(code);
if (i == 0) {
s.setNext(head);
head = s;
}
Node p = head;
int j = 0;
while (p != null j i - 1) {
p = p.getNext();
j++;
}
if (j i || p == null) {
throw new Exception("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
// tail = s;
// tail.setNext(head);
curlen = curlen + 1;
}
public void remove(int i) throws Exception {
Node p = head, q = null;
int j = 0;
i = i - 1;
while (j i) {
q = p;
p = p.getNext();
j++;
}
if (j i || p == null)
throw new Exception("删除位置不合法");
if (q == null) {
// tail.setNext(p.getNext());
head = head.getNext();
} else
q.setNext(p.getNext());
curlen = curlen - 1;
}
/**
* 按照节点删除
* @param i
* @throws Exception
*/
public void remove(Node p) throws Exception {
if(p.getNext()==p){
p=null;
head=null;
}
else{
Node q = p.getNext();
p.setNext(q.getNext());
}
curlen = curlen - 1;
}
public void out(int m) throws Exception {
Node p = head;
if(m==1){
System.out.print("按照顺序出列");
return;
}
int count = 1;
int n=m-1;
while (curlen 0) {
if (count == n) {
System.out.print(p.getNext().getData() + " ");
remove(p);
count = 1;
} else {
count++;
}
p = p.getNext();
}
}
public void display() {
Node node = head;
for (int i = 0; i 2 * curlen; i++) {
System.out.print(node.getData() + " ");
node = node.getNext();
}
System.out.println();
}
}
java 有循环链表吗
网页链接
java可以实现链表的,不管是单链表、双链表还是循环链表。
关于java实现循环链表和Java循环链表的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-15,除非注明,否则均为
原创文章,转载请注明出处。