السلام عليكم
لو سمحتو ابغي مساعدتكم في فهم كود linked list الكود موجود عندي بس مب فاهم شي:")
هذا الكود:
class ListNode {
7
8 // package access members; List can access these directly
9 Object data;
10 ListNode nextNode;
11
12 // constructor to create a ListNode that refers to object
13 ListNode( Object object )
14 {
15 this( object, null );
16 }
17
18 // constructor to create ListNode that refers to Object
19 // and to next ListNode in List
20 ListNode( Object object, ListNode node )
21 {
22 data = object;
23 nextNode = node;
24 }
25
26 // return Object in this node
27 Object getObject()
28 {
29 return data;
30 }
31
// get next node
33 ListNode getNext()
34 {
35 return nextNode;
36 }
37
38 } // end class ListNode
39
40 // class List definition
41 public class List {
42 private ListNode firstNode;
43 private ListNode lastNode;
44 private String name; // String like "list" used in printing
45
46 // construct an empty List with a name
47 public List( String string )
48 {
49 name = string;
50 firstNode = lastNode = null;
51 }
52
53 // construct empty List with "list" as the name
54 public List()
55 {
56 this( "list" );
57 }
58
59 // Insert Object at front of List. If List is empty,
60 // firstNode and lastNode will refer to same object.
61 // Otherwise, firstNode refers to new node.
62 public synchronized void insertAtFront( Object insertItem )
63 {
64 if ( isEmpty() )
65 firstNode = lastNode = new ListNode( insertItem );
66
else
68 firstNode = new ListNode( insertItem, firstNode );
69 }
70
71 // Insert Object at end of List. If List is empty,
72 // firstNode and lastNode will refer to same Object.
73 // Otherwise, lastNode's nextNode refers to new node.
74 public synchronized void insertAtBack( Object insertItem )
75 {
76 if ( isEmpty() )
77 firstNode = lastNode = new ListNode( insertItem );
78
79 else
80 lastNode = lastNode.nextNode =
81 new ListNode( insertItem );
82 }
83
84 // remove first node from List
85 public synchronized Object removeFromFront()
86 throws EmptyListException
87 {
88 Object removeItem = null;
89
90 // throw exception if List is empty
91 if ( isEmpty() )
92 throw new EmptyListException( name );
93
94 // retrieve data being removed
95 removeItem = firstNode.data;
96
97 // reset the firstNode and lastNode references
98 if ( firstNode == lastNode )
99 firstNode = lastNode = null;
100
else
102 firstNode = firstNode.nextNode;
103
104 // return removed node data
105 return removeItem;
106 }
107
108 // Remove last node from List
109 public synchronized Object removeFromBack()
110 throws EmptyListException
111 {
112 Object removeItem = null;
113
114 // throw exception if List is empty
115 if ( isEmpty() )
116 throw new EmptyListException( name );
117
118 // retrieve data being removed
119 removeItem = lastNode.data;
120
121 // reset firstNode and lastNode references
122 if ( firstNode == lastNode )
123 firstNode = lastNode = null;
124
125 else {
126
127 // locate new last node
128 ListNode current = firstNode;
129
130 // loop while current node does not refer to lastNode
131 while ( current.nextNode != lastNode )
132 current = current.nextNode;
133
134 // current is new lastNode
135 lastNode = current;
136 current.nextNode = null;
137 }
138
139 // return removed node data
140 return removeItem;
141 }
142
143 // return true if List is empty
144 public synchronized boolean isEmpty()
145 {
146 return firstNode == null;
147 }
148
149 // output List contents
150 public synchronized void print()
151 {
152 if ( isEmpty() ) {
153 System.out.println( "Empty " + name );
154 return;
155 }
156
157 System.out.print( "The " + name + " is: " );
158
159 ListNode current = firstNode;
160
161 // while not at end of list, output current node's data
162 while ( current != null ) {
163 System.out.print( current.data.toString() + " " );
164 current = current.nextNode;
165 }
166
167 System.out.println( "\n" );
168 }
169
170 } // end class List
167 System.out.println( "\n" );
168 }
169
170 } // end class List
;