数据结构系列之链表
来源:互联网 发布:c语言for是什么意思 编辑:程序博客网 时间:2024/06/03 03:11
</pre><pre name="code" class="java">package 单链表;//组成链表的节点类public class IntNode { public int value;public IntNode next;public IntNode(int value){this(value,null); //调用下面的一个构造函数}public IntNode(int value,IntNode next){this.value=value;this.next=next;}}
2、再创建链表类
package 单链表;//单链链表类public class IntSLList {private IntNode head,tail;private int length;public int getLength() {return length;}public IntSLList(){ //构造函数,初始化head和tail,length;this.head=null;this.tail=null;length=0;}public boolean isEmpty(){ //判断是否为空链表return head==null;}public void addToHead(int value){ //在head节点前插入值为value的新节点head=new IntNode(value,head);if(tail==null){tail=head;}length++;}public void addToTail(int value){ //在tail节点后插入值为value的新节点if(this.isEmpty())head=tail=new IntNode(value);else{tail.next=new IntNode(value);tail=tail.next;}length++;}public int deleteFromHead(){ //删除头节点,返回头节点value信息int value=head.value;if(head==tail)head=tail=null;elsehead=head.next;length--;return value;}public int deleteFromTail(){int value=tail.value;if(head==tail)head=tail=null;else{IntNode temp;for(temp=head;temp.next!=tail;temp=temp.next); //空循环,旨在移动temptail=temp;tail.next=null;}length--;return value;}public void printAll(){if(this.isEmpty())System.out.println("单链表为空!");else{IntNode temp=head;System.out.print("单链表为:"); while(temp!=null){System.out.print(temp.value+" ");temp=temp.next; } System.out.println();}}public boolean isInList(int value){IntNode temp=head;boolean b=false;while(temp!=null){if(temp.value==value){b=true;break;}temp=temp.next;}return b;}public void deleteByValue(int value){ //根据节点值删除节点int flag=0; if(head.value==value&&head==tail){ //如果只有一个节点 head=tail=null; flag=1; } else if(head.value==value){ //不止一个节点,但目地节点在头节点 head=head.next; flag=1; } else{ //不止一个节点,目地节点不再头节点 IntNode temp,pred; for(pred=head,temp=head.next;temp!=null&&temp.value!=value;temp=temp.next,pred=pred.next); if(temp!=null){ pred.next=temp.next; flag=1; } } if(flag==1){ System.out.println("元素"+value+"删除成功!"); length--; } else System.out.println("元素"+value+"不再链表中!");}public int deleteByIndex(int index){ //根据下标删除节点int deleteValue=index;if(this.isEmpty()){System.out.print("空链表,无法进行删除操作!");return deleteValue;}else if(index>-1&&index<this.length){if(index==0)deleteValue=this.deleteFromHead();else if(index==this.length-1)deleteValue=this.deleteFromTail();else{IntNode temp=head;int i=0;for(i=0;i<index-1;i++){temp=temp.next;}deleteValue=temp.next.value;temp.next=temp.next.next;length--;}}else{System.out.println("下标越界");}return deleteValue;} public void reverseList(){ //反转链表 IntNode p=head,q=null,r=null,m=null; if(!this.isEmpty()){ if(this.length==1) q=p; else if(this.length==2){ q=p.next; q.next=p; p.next=null; } else{ q=p.next; r=q.next; while(q!=null&&r!=null){ q.next=p; p.next=m; m=p; p=q; q=r; r=r.next; } q.next=p; } head=q; } else{ System.out.println("链表为空!"); } } public void insertByValue(int value,int insertValue){ //在value前插入insertValue if(this.isInList(value)){ IntNode p=head,q=null; if(p.value==value) this.addToHead(insertValue); else{ q=p.next; while(q!=null){ if(q.value==value) break; else{ q=q.next; p=p.next; } } IntNode valueNode=new IntNode(insertValue); valueNode.next=q; p.next=valueNode; length++; } } else{ System.out.print("元素"+value+"不在链表中!"); } } public void insertByIndex(int index,int insertValue){ if(!this.isEmpty()){ if(index>-1&&index<this.length){ if(index==0) this.addToHead(insertValue); else{ IntNode p=head,q=null; int j=0; for(j=0;j<index-1;j++) p=p.next; q=p.next; IntNode insertNode=new IntNode(insertValue); insertNode.next=q; p.next=insertNode; length++; } } else System.out.print("下标越界!"); } else System.out.print("链表为空!"); }}3、最后创建测试类
<span style="font-size:12px;">package 单链表;//测试类public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubIntSLList l=new IntSLList();IntSLList m=new IntSLList(); //测试isEmpty()System.out.println(l.isEmpty());l.addToHead(1);System.out.println(l.isEmpty());//测试getLength()System.out.println(l.getLength());System.out.println(m.getLength());//测试printAll()l.printAll();m.printAll();//测试addToHead(int value)l.addToHead(2);l.printAll();//测试deleteFromHead()int dele=l.deleteFromHead();System.out.println("删除元素为:"+dele);l.printAll();int dele1=l.deleteFromHead();System.out.println("删除元素为:"+dele1);l.printAll();//测试addToTail(int value)m.addToTail(1);m.addToTail(2);m.printAll();//测试deleteByIndex(int index)m.deleteByIndex(0);m.printAll();m.deleteByIndex(0);m.printAll();m.addToTail(1);m.addToTail(2);m.addToTail(3);m.addToTail(4);m.printAll();m.deleteByIndex(1);m.printAll();m.deleteByIndex(2);m.printAll();m.deleteByIndex(2);m.printAll();}}</span><strong style="font-size:10px;"></strong>
0 0
- 数据结构系列之链表
- 喜羊羊系列之数据结构内核链表
- 喜羊羊系列之数据结构双向链表
- [考研系列之数据结构]线性表之链表
- 数据结构和算法系列2 线性表之链表
- linux内核系列(二)内核数据结构之链表
- 数据结构系列之链表——单链表的建立
- 数据结构系列之链表——单链表排序
- 数据结构系列之链表——单链表逆置
- 数据结构系列之链表——单链表遍历
- 数据结构系列之链表——双链表建立
- 数据结构系列之链表——双链表删除
- 数据结构系列之链表——双链表插入
- 还债系列之数据结构——数组和链表
- 喜羊羊系列之数据结构栈链
- [考研系列之数据结构]线性表概述
- 数据结构系列之基数排序
- [考研系列之数据结构]线性表之栈
- [leetcode][tree] Populating Next Right Pointers in Each Node II
- project Euler 14
- 数据库事务
- java--IO流
- 图像的平移和镜像
- 数据结构系列之链表
- 数据流基本问题--确定频繁元素(一)
- g++参数介绍
- objective-C 中简单数据操作
- 【bzoj1660】【单调栈】Bad Hair Day 乱发节
- The hierarchy of the type GreetingBeforeAdvice is inconsistent
- Android jni 应用初探 (MAC 环境)
- 自动化及工具介绍
- EditText属性