栈的实现
public interface Stack<E> {
void push(E e);
E pop();
E peek();
int getSize();
boolean isEmpty();
}
public class LinkedList<E> {
private Node dummyHead;
private int size;
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();
}
}
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;
prev.next = new Node(e, prev.next);
size++;
}
public void addFirst(E e) {
add(0, e);
}
public void addList(E e) {
add(size, e);
}
public E get(int index) {
if(index < 0 || index > size)
throw new IllegalArgumentException("Add 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);
}
public void set(int index, E e) {
if(index < 0 || index > size)
throw new IllegalArgumentException("Add 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;
}
public E remove(int index) {
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 retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size--;
return retNode.e;
}
public E removeFirst() {
return remove(0);
}
public E removeLast() {
return remove(size - 1);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
Node cur = dummyHead.next;
while(cur != null) {
res.append(cur + "-->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
}
public class LinkedListStack<E> implements Stack<E> {
private LinkedList<E> list;
public LinkedListStack() {
list = new LinkedList<>();
}
@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 int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Stack top ");
res.append(list);
return res.toString();
}
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);
}
}
队列的实现
public interface Queue<E> {
int getSize();
boolean isEmpty();
void enqueue(E e);
E dequeue();
E getFront();
}
public class LinkedListQueue<E> implements Queue<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() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private Node head, tail;
private int size;
public LinkedListQueue() {
this.head = null;
this.tail = null;
this.size = 0;
}
@Override
public int getSize() {
return size;
}
@Override
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("不能出队!");
Node retNode = head;
head = head.next;
retNode.next = null;
if(head == null)
tail = null;
size--;
return retNode.e;
}
@Override
public E getFront() {
if(isEmpty())
throw new IllegalArgumentException("不能出队!");
return head.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();
}
public static void main(String[] args) {
LinkedListQueue<Integer> queue = new LinkedListQueue<>();
for(int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if(i % 3 == 2) {
queue.dequeue();
System.out.println(queue);
}
}
}
}