Day13 链表

1、内部类的方法可以访问外部类的数据;

2、定义了成员内部类后,必须使用外部类对象来创建内部类对象,而不能直接去 new 一个内部类对象,内部类 对象名 = 外部类对象.new 内部类( );

3、内部类可以直接访问内部类的成员变量或方法,但内部类访问外部类的成员变量或者方法时,需要使用this关键字;

4、delete 方法一开始我还以为表尾的不能删,模拟了一下发现 for 循环结束后 tempNode 结点就是需要删除结点的前驱结点了,此时令 tempNode.next = tempNode.next.next 就是指向表尾结点的后一个,也就是 null 了;

复制代码
  1 public class LinkedList {
  2     class Node {
  3         /**
  4          * The data.
  5          */
  6         int data;
  7         /**
  8          * The reference to the next node.
  9          */
 10         Node next;
 11 
 12         public Node(int paraValue) {
 13             data = paraValue;
 14             next = null;
 15         }// Of the constructor
 16     }// Of class Node
 17 
 18     /**
 19      * The header node. The data is never used.
 20      */
 21     Node header;
 22 
 23     /**
 24      * Construct an empty linked list.
 25      */
 26     public LinkedList() {
 27         header = new Node(0);
 28     }// Of the first constructor.
 29 
 30     /**
 31      * Overrides the method claimed in Object, the superclass of any class.
 32      */
 33     public String toString() {
 34         String resultString = "";
 35 
 36         if (header.next == null) {
 37             return "empty";
 38         } // Of if
 39 
 40         Node tempNode = header.next;
 41         while (tempNode != null) {
 42             resultString += tempNode.data + ", ";
 43             tempNode = tempNode.next;
 44         } // Of while
 45 
 46         return resultString;
 47     }// Of toString
 48 
 49     // Reset to empty. Free the space through garbage collection.
 50     public void reset() {
 51         header.next = null;
 52     }// Of reset
 53 
 54     /**
 55      * Locate the given value. If it appears in multiple positions,
 56      * simply return the first one.
 57      * 
 58      * @param paraValue
 59      * @return The position. -1 for not found.
 60      */
 61     public int locate(int paraValue) {
 62         int tempPosition = -1;
 63 
 64         Node tempNode = header.next;
 65         int tempCurrentPosition = 0;
 66         while (tempNode != null) {
 67             if (tempNode.data == paraValue) {
 68                 tempPosition = tempCurrentPosition;
 69                 break;
 70             } // Of if
 71 
 72             tempNode = tempNode.next;
 73             tempCurrentPosition++;
 74         } // Of while
 75 
 76         return tempPosition;
 77     }// Of locate
 78 
 79     /**
 80      * Insert a value to a position. If the list is already full, do nothing.
 81      * 
 82      * @param paraPosition The given position.
 83      * @param paraValue    The given value.
 84      * @return Success or not.
 85      */
 86     public boolean insert(int paraPosition, int paraValue) {
 87         Node tempNode = header;
 88         Node tempNewNode;
 89 
 90         for (int i = 0; i < paraPosition; i++) {
 91             if (tempNode.next == null) {
 92                 System.out.println("The position " + paraPosition + " is illegal.");
 93                 return false;
 94             } // Of if
 95 
 96             tempNode = tempNode.next;
 97         } // Of for i
 98 
 99         // Construct a new node.
100         tempNewNode = new Node(paraValue);
101 
102         // Now link them.
103         tempNewNode.next = tempNode.next;
104         tempNode.next = tempNewNode;
105 
106         return true;
107 
108     }// Of insert
109 
110     /**
111      * Delete a value at a position.
112      * 
113      * @param paraPosition The given position.
114      * @return Success or not.
115      */
116     public boolean delete(int paraPosition) {
117         if (header.next == null) {
118             System.out.println("Cannot delete element from an empty list.");
119             return false;
120         }// Of if
121 
122         Node tempNode = header;
123 
124         for (int i = 0; i < paraPosition; i++) {
125             if (tempNode.next.next == null) {
126                 System.out.println("The position " + paraPosition + " is illegal.");
127                 return false;
128             }// Of if
129             tempNode = tempNode.next;
130         }// Of for i
131 
132         tempNode.next = tempNode.next.next;
133 
134         return true;
135     }// Of delete
136 
137     /**
138      * The entrance of the program.
139      * 
140      * @param args
141      */
142     public static void main(String args[]) {
143         LinkedList tempFirstList = new LinkedList();
144         System.out.println("Initialized, the list is: " + tempFirstList.toString());
145 
146         for (int i = 0; i < 5; i++) {
147             tempFirstList.insert(0, i);
148         }// Of for i
149         System.out.println("Inserted, the list is: " + tempFirstList.toString());
150 
151         tempFirstList.insert(6, 9);
152 
153         tempFirstList.delete(4);
154 
155         tempFirstList.delete(2);
156         System.out.println("Deleted, the list is: " + tempFirstList.toString());
157 
158         tempFirstList.delete(0);
159         System.out.println("Deleted, the list is: " + tempFirstList.toString());
160 
161         for (int i = 0; i < 5; i++) {
162             tempFirstList.delete(0);
163             System.out.println("Looped delete, the list is: " + tempFirstList.toString());
164         }// Of for i
165     }// Of main
166 }//Of class LinkedList
复制代码

发现之前有些的地方的注释没写完整,懒得改了后面改。

posted @   滑稽的炒饭  阅读(21)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示