「java单向循环链表」循环单链表的实现
今天给各位分享java单向循环链表的知识,其中也会对循环单链表的实现进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java怎么用链表实现
在数据结构中经常看见的一个基本概念-链表。
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
在Java中,对于链表的实现都是基于引用数据类型操作的。实现大致如下:
定义节点类Node,节点的概念很重要,一个链表是由各各节点连接在一起组成的。在节点类Node中定义节点内容及指向下一节点的引用,再增加一个添加节点的方法即可完成链表实现。
链表有很多种不同的类型:单向链表,双向链表以及循环链表。在执行效率上,相比数组而言,链表插入快查找慢,开发中得根据实际业务使用。
用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循环单链表实现约瑟夫环,我的代码出列顺序不正确
你的remove方法不对,你的方法每次删掉的是从head开始第m个位置的节点,
但约瑟夫环需要的是要删掉每次循环数到m的位置的节点。
remove方法可以去掉,再把out方法改一下就可以了。
public void out(int m) throws Exception {
Node p = head;
Node pre = null;
int count = 1;
while (curlen 0) {
if (count == m) {
System.out.print(p.getData() + " ");
if(pre != null){
pre.setNext(p.getNext());
}
p = p.getNext();
curlen = curlen - 1;
count = 1;
} else {
pre = p;
p = p.getNext();
count++;
}
}
}
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单向循环链表的信息别忘了在本站进行查找喔。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。