Download
stack n.
Skip this Video
Loading SlideShow in 5 Seconds..
Stack PowerPoint Presentation

Stack

0 Vues Download Presentation
Télécharger la présentation

Stack

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. LIFO (Last In First Out) or FILO (First In Last 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 pop push capacity peek f e h size Java Doc java.util Interface Stack<E> b a

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

  5. 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

  6. Check Parenthesis Balance • (((a))(([{aa+ss}]))) • (((a))(([{aa+ss}])()) IT 179

  7. 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

  8. 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

  9. 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

  10. /** * 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

  11. publicclassLStack<T>implementsStack<T> { /*****Thisisaninnerclassforinternalnodes ********/ privateclass 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

  12. 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

  13. 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

  14. 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

  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