170 likes | 319 Vues
Dive into the fundamentals of LIFO (Last In, First Out) structures through stack operations and their implementation in Java. We explore methods such as push, pop, and peek, alongside details of how a generic stack interface operates. Utilizing an inner class for node storage, this guide will demonstrate how to implement stack operations including checking if the stack is empty or calculating its size. Additionally, we touch upon practical applications like checking for palindromes and balancing parentheses with stacks.
E N D
LIFO (Last In First Out) Stack IT 179
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
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
FIFO (Frst In First Out) Queue a b h e f IT 179
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
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
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
/** * 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
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
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
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
a • ada • mom • eve • bob • deed • noon Palindromes • aaaaaaaaa • adada • mommom • eveeeeeeeeeeeve • bobdeedbob • abcdefghiihgfedcba IT 179
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
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
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
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