1 / 16

Stack

LIFO ( L ast I n F irst O ut). Stack. Operations on a Stack. pop. LIFO ( L ast I n F irst O ut). pop. peek. push. h. peek. push. g. push. e. d. push. f. b. empty. b. push. a. c. push. LIFO ( L ast I n F irst O ut). Stack. a. b. b. b. b. h. h. h. h. h.

Download Presentation

Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LIFO (Last In First Out) Stack IT 179

  2. Operations on a Stack pop LIFO (Last In First Out) pop peek push h peek push g push e d push f b empty b push a c push IT 179

  3. LIFO (Last In First Out) Stack a b b b b h h h h h e e e e e e e e e f f f f f f f f f f f push push push pop push push push pop pop pop pop pop f e b b h b a a b h e f IT 179

  4. FIFO (Frst In First Out) Queue a b h e f IT 179

  5. Stack interface Java doc 1 publicinterface Stack<T> { T push(T obj); T peek(); T pop(); boolean empty(); } 2 2 6 3 1 4 4 3 5 5 6 IT 179

  6. Stack interface Java doc 1 publicinterface Stack<T> { T push(T obj); T peek(); T pop(); boolean empty(); } 2 2 6 3 1 4 4 3 5 5 6 IT 179

  7. package myUtil; /** * This generic interface is the standard Stack API * @authorChung-Chih Li * @param<T> */ publicinterface Stack<T> { /** * Push the item on the top of this Stack. * @param item * @return the same item that is pushed. */ T push(T item); /** * Pop out the item at the top of this Stack. * @return the item is is popped out. * @exception throw an EmptyStackException if this Stack is empty. */ T pop(); Stack Interface IT 179

  8. /** * Peek at the top of this Stack. * @return the item at the top of this Stack. * @exception throw a EmptyStackException if this Stack is empty. */ T peek(); /** * @return true if this Stack is empty, false otherwise */ boolean empty(); Stack Interface IT 179

  9. publicclassLStack<T>implementsStack<T> { /*****Thisisaninnerclassforinternalnodes ********/ privatestatic class Node<E> { private E data; private Node<E> next; private Node(E data, Node<E> next) { // Construct a node pointing to next this.data = data; this.next = next; } } /****ThisistheendoftheinnerclassNode<E>******/ private Node<T> top; publicLStack() { top = null; } ..... ..... .... } Using an inner class for the internal nodes IT 179

  10. publicclassLStack<T>implementsStack<T>{ ..... ..... public T push(T item){ top = new Node<T>(item, top); return item; } public T peek(){ if (top == null) thrownew EmptyStackException(); returntop.data; } public T pop(){ if (top == null) thrownew EmptyStackException(); T data = top.data; top = top.next; return data; } Methods implementation (i) IT 179

  11. publicclassLStack<T>implementsStack<T>{ ..... ..... publicboolean empty(){ if (top == null) returntrue; returnfalse; } publicint size() { int counter=0; Node<T> n=top; while (n != null) { n = n.next; counter++; } return counter; } } Methods implementation (ii) IT 179

  12. a • ada • mom • eve • bob • deed • noon Palindromes • aaaaaaaaa • adada • mommom • eveeeeeeeeeeeve • bobdeedbob • abcdefghiihgfedcba IT 179

  13. publicstaticboolean isPalindrome(String aWord) { int i=0,j=aWord.length()-1; while (i < j) { if (aWord.charAt(i++) != aWord.charAt(j--)) returnfalse; } returntrue; } Palindrome Test IT 179

  14. Not random accessible a  c  d  e  d  c  a a  c  e  e  d  c  a Palindrome List Test Using Stack fail IT 179

  15. publicstaticboolean isPalindrome(MySLinkedList<Character> aWord) { LStack<Character> reverse = new LStack<Character>(); Iterator<Character> ai = aWord.iterator(); while (ai.hasNext()) reverse.push(ai.next()); ai = aWord.iterator(); while (ai.hasNext()) if (! reverse.pop().equals(ai.next())) returnfalse; returntrue; } Palindrome test IT 179

  16. publicstaticboolean checkBalance(String s) { LStack<Character> pStack = new LStack<Character>(); try { for (int i=0;i<s.length();i++) { if (s.charAt(i)=='(') { pStack.push(s.charAt(i)); continue; } if (s.charAt(i)!=')') continue; // ignore other character pStack.pop(); } } catch (EmptyStackException e) { returnfalse; } return pStack.empty(); } Check parentheses balance IT 179

More Related