0 课程地址
https://coding.imooc.com/lesson/207.html#mid=13449
1 重点关注
1.1 使用链表实现栈代码解析
见3.1
2 课程内容
3 Coding
3.1 使用链表实现栈代码解析
- 主类:
package com.company; public class LinkedListStack<E> implements Stack<E> { private LinkedList<E> linkedList; public LinkedListStack(){ linkedList = new LinkedList<>(); } @Override public void push(E e) { linkedList.addFirst(e); } @Override public E pop() { return linkedList.removFirst(); } @Override public E peek() { return linkedList.findByFirst(); } @Override public boolean isEmpty() { return linkedList.isEmpty(); } @Override public int getSize() { return linkedList.getSize(); } @Override public String toString() { final StringBuffer sb = new StringBuffer("LinkedListStack:pop "); sb.append(linkedList); return sb.toString(); } }
- 测试类:
public static void main(String[] args) { LinkedListStack<Integer> linkedListStack = new LinkedListStack<>(); for(int i = 0;i < 5;i++){ linkedListStack.push(i); System.out.println(linkedListStack); } linkedListStack.pop(); System.out.println(linkedListStack); System.out.println(linkedListStack.peek()); System.out.println(linkedListStack.getSize()); System.out.println(linkedListStack.isEmpty()); }
- 测试结果:
LinkedListStack:pop 0->Null LinkedListStack:pop 1->0->Null LinkedListStack:pop 2->1->0->Null LinkedListStack:pop 3->2->1->0->Null LinkedListStack:pop 4->3->2->1->0->Null LinkedListStack:pop 3->2->1->0->Null 3 4 false Process finished with exit code 0
标签:LinkedListStack,linkedList,linkedListStack,pop,链表,玩转,Override,数据结构,public From: https://www.cnblogs.com/1446358788-qq/p/16841350.html