java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue
LikedList:
package Date_pacage; public class LinkedList<E> { public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList<>(); for(int i = 0 ; i < 5 ; i ++) { linkedList.addFirst(i); System.out.println(linkedList); } linkedList.add(2, 666); System.out.println(linkedList); linkedList.remove(2); System.out.println(linkedList); } 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() { this(null, null); } @Override public String toString() { return e.toString(); } } private Node dummyHead; private int size; public LinkedList() { dummyHead = new Node(null, null); size = 0; } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } 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; prev.next = new Node(e, prev.next); size ++; } public void addFirst(E e) { // Node node = new Node(e); // node.next = head; // head = node; add(0, e); } public void addLast(E e) { add(size, 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 - 1); } //修改链表的第index位置的元素为e public void set(int index, E e) { 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; } cur.e = e; } public boolean contains(E e) { Node cur = dummyHead.next; while(cur != null){ if(cur.e.equals(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 ; cur != null ; cur = cur.next) { res.append(cur.e + "->"); } res.append("NULL"); return res.toString(); } public E remove(int index) { if(index < 0 || index > size) { throw new IllegalArgumentException("Get 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 removeFirst() { return remove(0); } public E removrLast() { return remove(size - 1); } }
链表栈 LinkedListStack:
package Date_pacage; public class LinkedListStack<E> implements Stack<E> { public static void main(String[] args) { LinkedListStack<Integer> stack = new LinkedListStack<>(); for(int i = 0 ; i < 5 ; i ++) { stack.push(i); System.out.println(stack); } stack.pop(); System.out.println(stack); } private LinkedList<E> list; public LinkedListStack() { list = new LinkedList<>(); } @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.removeFirst(); } @Override public E peek() { return list.getFirst(); } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append("Stack: top"); res.append(list); return res.toString(); } }
链表队列 LinkedListQueue:
package Date_pacage; //使用带尾节点的链表 public class LinkedListQueue<E> implements Queue<E> { public static void main(String[] args) { LinkedListQueue<Integer> linkedList = new LinkedListQueue<>(); for(int i = 0 ; i < 5 ; i ++) { linkedList.enqueue(i); System.out.println(linkedList); } linkedList.enqueue(666); System.out.println(linkedList); linkedList.dequeue(); System.out.println(linkedList); } 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() { this(null, null); } @Override public String toString() { return e.toString(); } } private Node head, tail; private int size; public LinkedListQueue() { head = null; tail = null; size = 0; } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size == 0; } @Override public E getFront() { if(isEmpty()) { throw new IllegalArgumentException("Cannot getFront from an empty queue."); } return head.e; } @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("Cannot dequeue from an empty queue."); } Node retNode = head; head = head.next; retNode.next = null; if(head == null) { tail = null; } size--; return retNode.e; } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append("Queue: front"); Node cur =head; while(cur != null) { res.append(cur + "->"); cur = cur.next; } res.append("NULL tail"); return res.toString(); } }