标签:find rip 插入 code des ack 基本 head ros
1 package Eric.ADT; 2 3 /** 4 * <p> 5 * Title:doubleLinkedList 6 * </p> 7 * <p> 8 * Description:用java实现双向循环链表的基本操作 9 * </p> 10 * <p> 11 * Location:Frostburg 12 * </p> 13 * 14 * @author: Eric.Chen 15 * @date:2017年9月23日下午12:54:05 16 */ 17 public class doubleLinkedList { 18 class Node {// 节点元素 19 private Node prev; 20 public Object value; 21 private Node next; 22 } 23 24 private Node header = null; 25 26 void initiate()// 初始化链表 27 { 28 header = new Node(); 29 header.prev = header; 30 header.value = null; 31 header.next = header; 32 } 33 34 public void insertList(int i, Object n)// 在链表的i位置插入元素n 35 { 36 if (i <= 0 || i > size()) { 37 System.out.println("插入位置不合法!!!" + "链表长度为:" + size()); 38 } else { 39 Node e = new Node(); 40 e.value = n; 41 if (header.prev == header)// 说明这是第一次插入元素,在头结点后面插入元素 42 { 43 e.prev = header; 44 e.next = header; 45 header.next = e; 46 header.prev = e; 47 48 } else if (i == size())// 在最后插入元素 49 { 50 e.next = header; 51 e.prev = header.prev; 52 header.prev.next = e; 53 header.prev = e; 54 } else {// 在i位置插入元素 55 Node temp = header; 56 int count = 0; 57 while (temp.next != header) { 58 count++; 59 if (count == i) { 60 e.next = temp.next; 61 e.prev = temp; 62 temp.next.prev = e; 63 temp.next = e; 64 65 } 66 temp = temp.next; 67 } 68 69 } 70 } 71 } 72 73 public void deleteByIndex(int i)// 删除指定位置上的元素 74 { 75 if (i <= 0 || i > size()) { 76 System.out.println("删除的位置误" + "链表长度为:" + size()); 77 } else { 78 Node temp = header; 79 int count = 0; 80 while (temp.next != header) { 81 count++; 82 if (count == i) { 83 temp.next = temp.next.next; 84 temp.next.prev = temp; 85 86 } 87 88 temp = temp.next; 89 } 90 91 } 92 93 } 94 95 public int size()// 返回链表的长度 96 { 97 int count = 1; 98 Node temp = header; 99 while (temp.next != header) { 100 count++; 101 temp = temp.next; 102 } 103 104 return count; 105 } 106 107 public void printList() { 108 Node temp = header; 109 while (temp.next != header) { 110 System.out.print(temp.next.value + " "); 111 temp = temp.next; 112 } 113 } 114 115 public void deleteByEle(Object element)// 删除链表中指定元素 116 { 117 Node temp = header; 118 int count = 0; 119 int num = 0; 120 while (temp.next != header) { 121 count++; 122 if (temp.next.value == element) { 123 num = count; 124 } 125 temp = temp.next; 126 } 127 deleteByIndex(num); 128 } 129 130 public Object findEleByIndex(int i) { 131 int count = 0; 132 Object p = null; 133 if (i <= 0 || i > size()) { 134 System.out.println("输入数据有错!" + size()); 135 return -1; 136 } else { 137 Node temp = header; 138 while (temp.next != header) { 139 count++; 140 if (count == i) { 141 p = temp.next.value; 142 } 143 temp = temp.next; 144 } 145 } 146 return p; 147 } 148 149 public static void main(String[] args) { 150 doubleLinkedList list = new doubleLinkedList(); 151 list.initiate(); 152 for (int i = 1; i <= 10; i++) { 153 list.insertList(i, i); 154 } 155 System.out.println("原来的链表为:"); 156 list.printList(); 157 System.out.println(); 158 System.out.println("链表的长度为:" + list.size()); 159 /*list.deleteByEle(8); 160 System.out.println("删除元素后的链表为:"); 161 list.printList(); 162 System.out.println(); 163 System.out.println("删除元素后的链表的长度为:" + list.size());*/ 164 int idem =(int) list.findEleByIndex(8); 165 System.out.println(idem); 166 167 } 168 }
标签:find rip 插入 code des ack 基本 head ros
原文地址:http://www.cnblogs.com/AwinChen/p/7584531.html