【玩轉資料結構 從入門到進階】 連結串列
阿新 • • 發佈:2018-12-08
public class LinkList<E> { private class Node { public E e; public Node next; public Node(E e, Node next) { this.e = e; this.next = next; } public Node(E e) { this(e, null); } public Node() { } @Override public String toString() { return e.toString(); } } private Node dummyhead; int size; public LinkList() { dummyhead = new Node(null, null); size = 0; } // 獲取連結串列中的元素個數 public int getSize() { return size; } // 判斷連結串列是否為空 public boolean isEmpty() { return size == 0; } // 在連結串列中間新增元素e public void add(int index, E e) { if (index < 0 || index > size) { throw new IllegalArgumentException("Add failed.Illegal index"); } Node prev = dummyhead; for (int i = 0; i < index; i++) { prev = prev.next; } Node node = new Node(e); node.next = prev.next; prev.next = node; size++; } // 在連結串列頭新增元素e public void addFirst(E e) { add(0, e); } // 在連結串列末尾新增元素e public void addList(E e) { add(size, e); } // 獲得連結串列中index位置的元素e public E get(int index) { // 判斷元素的合法性 if (index < 0 || index > size) throw new IllegalArgumentException("Get failed.Illegal index"); Node cur = dummyhead.next; for (int i = 0; i < index; i++) cur = cur.next; return cur.e; } // 獲得連結串列第一個元素 public E getFirst() { return get(0); } // 獲得連結串列最後一個元素 public E getLast() { return get(size); } // 修改連結串列中index位置的元素為e public void set(int index, E e) { // 判斷元素的合法性 if (index < 0 || index > size) throw new IllegalArgumentException("Set failed.Illegal index"); Node cur = dummyhead.next; for (int i = 0; i < index; i++) cur = cur.next; cur.e = e; } // 檢視連結串列中是否存在元素e public boolean contains(E e) { Node cur = dummyhead.next; while (cur != null) { if (cur.e == e) return true; cur = cur.next; } return false; } @Override public String toString() { StringBuilder res = new StringBuilder(); /* * Node cur=dummyhead.next; while(cur!=null) { res.append(cur+"->"); * cur=cur.next; } */ for (Node cur = dummyhead.next; cur != null; cur = cur.next) res.append(cur + "->"); res.append("NULL"); return res.toString(); } // 刪除連結串列中的元素e public E remove(int index) { // 判斷元素的合法性 if (index < 0 || index > size) throw new IllegalArgumentException("Remove failed.Illegal index"); Node prev=dummyhead; for(int i=0;i<index;i++) { prev=prev.next; } Node retNode=prev.next; prev.next=retNode.next; retNode.next=null; size--; return retNode.e; } //刪除連結串列中的第一個元素 public E removrFirst() { return remove(0); } //刪除連結串列中的最後一個元素 public E removeLast() { return remove(size-1); } }
//用連結串列實現棧 public class LinkListStack<E> implements Stack<E> { private LinkList<E> list; public LinkListStack() { list=new LinkList<>(); } //獲得棧中元素個數 @Override public int getSize() { return list.getSize(); } //判斷棧中是否為空 @Override public boolean isEmpty() { return list.isEmpty(); } //進棧操作 @Override public void push(E e) { list.addFirst(e); } //出棧操作 @Override public E pop() { return list.removrFirst(); } //獲得棧頂元素操作 @Override public E peek() { // TODO Auto-generated method stub return list.getFirst(); } @Override public String toString() { StringBuilder res=new StringBuilder(); res.append("Stack top:"); res.append(list); return res.toString(); } }
import Arr.LinkList.Node; //用連結串列實現佇列 public class LinkListQueue<E> implements Queue<E> { public class Node { public E e; public Node next; public Node(E e, Node next) { this.e = e; this.next = next; } public Node(E e) { this(e, null); } public Node() { } @Override public String toString() { return e.toString(); } } private Node head,tail; private int size; public LinkListQueue() { head=null; tail=null; size=0; } //獲得佇列中的元素個數 public int getSize() { return size; } //判斷佇列中是否為空 public boolean isEmpty() { return size==0; } //入隊操作 @Override public void enqueue(E e) { if(tail==null) { tail=new Node(e); head=tail; }else { tail.next=new Node(e); tail=tail.next; } size++; } @Override public E dequeue() { //判斷佇列中元素是否為空 if(isEmpty()) throw new IllegalArgumentException("Dequeue failed.Illegal index"); Node retNode=head; head=head.next; retNode=null; if(head==null) { tail=null; } size--; return retNode.e; } @Override public E getFront() { if(isEmpty()) throw new IllegalArgumentException("Queue is empty"); return head.e; } @Override public String toString() { StringBuilder res = new StringBuilder(); /* * Node cur=dummyhead.next; while(cur!=null) { res.append(cur+"->"); * cur=cur.next; } */ res.append("Queue front"); for (Node cur = head.next; cur != null; cur = cur.next) res.append(cur + "->"); res.append("NULL tail"); return res.toString(); } }