zoukankan      html  css  js  c++  java
  • 循环链表增删改查java实现

      采用的是内部类的方法,内部类是Element,代表着结点,里面声明了两个变量,一个用来存放数据,一个用来存放下一个结点的地址,类似于指针。
    1
    public class CircularLinkedList { 2 private Element header=null;//定义了一个Elemment的引用变量 3 4 5 private class Element{ 6 public String data=null; 7 private Element next=null; 8 public Element(String data) { 9 this.data=data; 10 } 11 public Element() { 12 13 } 14 } 15 /** 16 * 初始化链表 17 */ 18 public void initList() { 19 header = new Element();//将申请的Element对象地址赋值给引用变量 20 header.data=null; 21 header.next=header; 22 } 23 24 /** 25 * 插入链表 26 */ 27 public void insertList(String data) { 28 Element node=new Element(); 29 node.data=data; 30 31 if (header.next==header) {//判断是不是第一次插入元素 32 header.next=node; 33 node.next=header; 34 }else { 35 //temp引用在栈中,temp和header引用都指向堆中的initList()new的Element对象 36 Element temp=header;//head在这里是一个头结点,存放的是new出来的对象的地址 37 while(temp.next!=header) { 38 temp=temp.next; 39 } 40 temp.next=node; 41 node.next=header; 42 } 43 } 44 45 /** 46 * 删除链表中的某个元素 47 */ 48 public void deleteList(String data) { 49 Element temp=header; 50 while(temp.next!=header) { 51 //判断当前temp的下一个结点是不是要删除的节点 52 if (temp.next.data.equals(data)) { 53 temp.next=temp.next.next; 54 }else { 55 temp=temp.next;//temp指针后移 56 } 57 } 58 } 59 60 /** 61 * 获取链表的第i个位置 62 */ 63 public String getElement(int i) { 64 Element element=new Element(); 65 Element temp=header; 66 if (i<0||i>size()) { 67 System.out.println("获取链表的位置有误!"); 68 }else { 69 int count =0; 70 71 while(temp.next!=header) { 72 count++; 73 if(count==i) { 74 element.data=temp.next.data; 75 } 76 temp=temp.next; 77 } 78 } 79 return element.data; 80 } 81 82 /** 83 * 获取链表长度 84 */ 85 private int size() { 86 87 Element temp=header; 88 int size=0; 89 while(temp.next!=header) { 90 size++; 91 temp=temp.next; 92 } 93 return size; 94 } 95 /** 96 * 判断链表中是否存在某个元素 97 */ 98 public Boolean isContain(String data) { 99 Element temp=header; 100 while(temp.next!=header) { 101 if(temp.next.data.equals(data)) { 102 return true; 103 } 104 temp=temp.next; 105 } 106 return false; 107 } 108 /** 109 * 打印链表 110 */ 111 public void print() { 112 System.out.println("打印链表!"); 113 Element temp=header; 114 while(temp.next!=header) { 115 temp=temp.next; 116 System.out.print(temp.data+" "); 117 } 118 System.out.println(); 119 } 120 }
    编写测试类
     1 public class LinkText {
     2 
     3     public static void main(String[] args) {
     4         CircularLinkedList clist=new CircularLinkedList();
     5         clist.initList();
     6         clist.insertList("张三");
     7         clist.insertList("李四");
     8         clist.insertList("王二");
     9         clist.insertList("麻子");
    10         clist.print();
    11         clist.deleteList("张三");
    12         clist.print();
    13         String a=clist.getElement(2);
    14         System.out.println(a);
    15     }
    16 
    17 }
    运行结果如下:

    需要注意的是:这里的引用变量,例如Element temp=new Element()

                     Element node=hader

    这里的temp和node都是引用变量,存放在栈空间中,相当于一个指针指向堆空间中new出来的对象。

    
    
  • 相关阅读:
    微服务架构技术栈选型手册(万字长文)
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    MFC对话框中使用CHtmlEditCtrl
    ATL开发 ActiveX控件的 inf文件模板
    ActiveX: 如何用.inf和.ocx文件生成cab文件
    Xslt 1.0中使用Array
    如何分隔两个base64字符串?
    An attempt was made to load a program with an incorrect format
    JQuery 公网 CDN
  • 原文地址:https://www.cnblogs.com/had1314/p/11192203.html
Copyright © 2011-2022 走看看