List container -- consumer List and common APIs, stack and queue implementation, and consumer listapi
Consumer list and common APIs
① Linked list ---- linked list
② Extended AbstractSequentialList in the listlist class and implemented the List Interface
③ The linked list provides a linked list data structure.
④ There are two constructor methods for listing objects.
A) Explain list ()
B) collections list (Collection c)
⑤ In addition to the inherited methods, the javaslist class also defines some useful methods for operating and accessing the data in the container;
A) void addFirst (E e)
B) void addLast (E e)
C) E removeFirst ()
D) E removeLast ()
1 sort list <String> sList = new sort list <String> (); 2 sList. add ("zhangsan"); // add the specified element to the end of this list. add ("lisi"); 4 sList. add ("wangwu"); 5 sList. add ("rose"); 6 sList. add ("mary"); 7 sList. add ("jack"); 8 sList. addFirst ("jay"); // inserts the specified element into the beginning of this List 9 sList. addLast ("jhon"); // Add the specified element to the end of this list 10 for (String name: sList) {11 System. out. println (name); 12} 13 14 System. out. println ("************************************* ***"); 15 System. out. println (sList. removeFirst (); // remove and return the first element of this list; if this list is empty, NoSuchElementException 16 sList. clear (); 17 System. out. println (sList. size (); // The number of elements returned to this list is 18 System. out. println (sList. pollFirst (); // obtain and remove the first element in the list. If the list is empty, null is returned.
The Linked list structure in Linked is as follows:
The remove (Object) method in the revoke list is as follows:
1 public boolean remove(Object o) { 2 if (o == null) { 3 for (Node<E> x = first; x != null; x = x.next) { 4 if (x.item == null) { 5 unlink(x); 6 return true; 7 } 8 } 9 } else {10 for (Node<E> x = first; x != null; x = x.next) {11 if (o.equals(x.item)) {12 unlink(x);13 return true;14 }15 }16 }17 return false;18 }
Find the unlink method.
1 E unlink(Node<E> x) { 2 // assert x != null; 3 final E element = x.item; 4 final Node<E> next = x.next; 5 final Node<E> prev = x.prev; 6 7 if (prev == null) { 8 first = next; 9 } else {10 prev.next = next;11 x.prev = null;12 }13 14 if (next == null) {15 last = prev;16 } else {17 next.prev = prev;18 x.next = null;19 }20 21 x.item = null;22 size--;23 modCount++;24 return element;25 }
We can see that when deleting, the operation is to set Element B to null, and point element a to the next element c of Element B and point c to;
Summary:
The data structure of the two-way linked list is encapsulated internally.
Each Node is a Node object. The Node object encapsulates the elements you want to add.
There is also a reference pointing to the previous Node object and a reference pointing to the next Node object.
Different containers have different data structures, and different data structures have different performance operations.
The linked list data structure is highly efficient for insertion and deletion, but the query efficiency is relatively low.
Array structure, which is highly efficient for query, because elements can be directly found through subscripts.
However, insertion and deletion efficiency is relatively low, because the migration operation is required.
Ii. Use consumer list to implement stack and queue
Stack features, first-in-first-out
Stack method:
1 class MyStack <T> {2 private writable list <T> data = null; 3 public MyStack () {4 data = new writable list <T> (); 5} 6 7 // method of stack pressure 8 public void push (T obj) {9 data. addFirst (obj); 10} 11 12 public T pop () {13 return data. removeFirst (); 14} 15 16 public Iterator <T> iterator () {17 return data. iterator (); 18} 19}
Add and use the main function:
1 MyStack<String> mystack=new MyStack<String>(); 2 mystack.push("zhangsan"); 3 mystack.push("lisi"); 4 mystack.push("wangwu"); 5 mystack.push("zhaoliu"); 6 mystack.pop(); 7 mystack.pop(); 8 Iterator<String> it=mystack.iterator(); 9 while(it.hasNext()){10 System.out.println(it.next());11 }
Output result:
Lisi
Zhangsan
Queue features: first-in-first-out
Queue method:
1 class myQueue<T>{ 2 private LinkedList<T> data=null; 3 public myQueue(){ 4 data=new LinkedList<T>(); 5 } 6 7 public void push(T obj) { 8 data.addFirst(obj); 9 }10 11 public T pop() {12 return data.removeLast();13 }14 15 public Iterator<T> iterotor() {16 return data.iterator();17 }18 }
Add and use the main function:
1 myQueue<Integer> myQueue=new myQueue<Integer>(); 2 myQueue.push(1); 3 myQueue.push(2); 4 myQueue.push(3); 5 myQueue.push(4); 6 myQueue.push(5); 7 myQueue.pop(); 8 myQueue.pop(); 9 Iterator<Integer> it= myQueue.iterotor();10 while (it.hasNext()) {11 System.out.println(it.next());12 }
Output result:
5
4
3