1 / 11

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.

ronna
Télécharger la présentation

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 ITK 275

  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 ITK 275

  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 ITK 275

  4. FIFO (Frst In First Out) Queue a b h e f ITK 275

  5. ITKStack interface 1 publicinterface ITKStack<T> { T push(T obj); T peek(); T pop(); boolean empty(); } 2 2 6 3 1 4 4 3 5 5 6 ITK 275

  6. publicclassLinkedListStack<T>implementsITKStack<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; publicLinkedListStack() { top = null; } ..... ..... .... } Using an inner class for the internal nodes ITK 275

  7. publicclassLinkedListStack<T>implementsITKStack<T>{ ..... ..... public T push(T obj){ top = new Node<T>(obj, top); return obj; } public T peek(){ returntop.data; } public T pop(){ T data = top.data; top = top.next; return data; } publicboolean empty(){ if (top == null) returntrue; returnfalse; } } Methods implementation ITK 275

  8. a • ada • mom • eve • bob • deed • noon Palindromes • aaaaaaaaa • adada • mommom • eveeeeeeeeeeeve • bobdeedbob • abcdefghiihgfedcba ITK 275

  9. publicstaticboolean isPalindrome(String aWord) { int j=aWord.length()-1; for (int i=0;i<aWord.length();i++) { if (! aWord.charAt(i).equals(aWord.charAt(j--)) returnfalse; } returntrue; }// end isPalindrome Palindrome Test ITK 275

  10. Not random accessible a  c  d  e  d  c  a a  c  e  e  d  c  a Palindrome Test Using Stack fail ITK 275

  11. publicstaticboolean isPalindrome(String aWord) { ArrayList<Character> cstr = new ArrayList<Character>(); LinkedListStack<Character> rcstr = new LinkedListStack<Character>(); for (int i=0;i<aWord.length();i++) { cstr.add(aWord.charAt(i)); rcstr.push(aWord.charAt(i)); } for (int i=0;i<aWord.length();i++) if (! rcstr.pop().equals( cstr.get(i) )) returnfalse; returntrue; }// end isPalindrome Palindrome test ITK 275

More Related