Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Stacks II CS Data Structures I COSC 2006

Similar presentations


Presentation on theme: "Chapter 7 Stacks II CS Data Structures I COSC 2006"— Presentation transcript:

1 Chapter 7 Stacks II CS2006 - Data Structures I COSC 2006
April 22, 2017 CS Data Structures I Chapter 7 Stacks II Chapter 6: Stacks

2 Topics Implementation of ADT Stack Using Linked lists Using ADT List

3 Linked List Stack Implementation
COSC 2006 April 22, 2017 Linked List Stack Implementation Stacks can grow and shrink dynamically No particular linked list variations are required: The head pointer can also serve as the top of the stack. Pushing becomes inserting the first node in a linked list. Popping becomes removing the first node. Chapter 6: Stacks

4 Linked List Stack Implementation
COSC 2006 April 22, 2017 Linked List Stack Implementation The head of List is the top of the stack head 1 3 7 9 1 top 3 7 9 Chapter 6: Stacks

5 LL Stack Implementation
COSC 2006 April 22, 2017 LL Stack Implementation // Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty Chapter 6: Stacks

6 LL Stack Implementation
COSC 2006 April 22, 2017 LL Stack Implementation // Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty Chapter 6: Stacks

7 LL Stack Implementation (2)
COSC 2006 April 22, 2017 LL Stack Implementation (2) public void push(Object newItem) { top = new Node(newItem, top); } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

8 LL Stack Implementation (2)
COSC 2006 April 22, 2017 LL Stack Implementation (2) public void push(Object newItem) { top = new Node(newItem, top); } // end push public Object pop() throws StackException { if (!isEmpty()) { Object temp = top.getItem(); top = top.getNext(); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

9 LL Stack Implementation (3)
COSC 2006 April 22, 2017 LL Stack Implementation (3) public void popAll() { top=null; } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackReferencedBased Chapter 6: Stacks

10 LL Stack Implementation (3)
COSC 2006 April 22, 2017 LL Stack Implementation (3) public void popAll() { top=null; } // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackReferencedBased Chapter 6: Stacks

11 ADT Stack ADT List Implementation
All stack operations can be implemented using the ADT List's operations The implementation is simpler to write than the array-based and makes used of previously-written code (code reuse) Position 1 of the list represents the top position of the stack

12 ADT Stack ADT List Implementation
top List position 10 80 60 . 5 1 2 3 List site()

13 ADT List Stack Implementation
COSC 2006 April 22, 2017 ADT List Stack Implementation // Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty Chapter 6: Stacks

14 ADT List Stack Implementation
COSC 2006 April 22, 2017 ADT List Stack Implementation // Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty Chapter 6: Stacks

15 List Stack Implementation (2)
COSC 2006 April 22, 2017 List Stack Implementation (2) public void push(Object newItem) { list.add (1, newItem); } // end push public Object pop() throws StackException { if (!list.isEmpty()) { Object temp = list.get(1); list.remove(1); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

16 List Stack Implementation (2)
COSC 2006 April 22, 2017 List Stack Implementation (2) public void push(Object newItem) { list.add (1, newItem); } // end push public Object pop() throws StackException { if (!list.isEmpty()) { Object temp = list.get(1); list.remove(1); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop Chapter 6: Stacks

17 List Stack Implementation (3)
COSC 2006 April 22, 2017 List Stack Implementation (3) public void popAll() { list.removeAll(); } // end popAll public Object peek() throws StackException { if (!list.isEmpty()) { return list.get(1); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek } // end StackListBased Chapter 6: Stacks

18 ADT Stack Implementation Comparison
Array-Based   No overhead of reference manipulation   Prevents adding elements if the array is full LL-Based More complicated than ADT List Most flexible, No size restrictions ADT List-Based Simpler to write Not as efficient as using a linked list directly

19 Review The ______ method of the ADT stack retrieves and then removes the top of the stack. createStack push pop peek

20 Review The ______ method of the ADT stack retrieves the top of the stack, but does not change the stack. createStack push pop peek

21 Review Which of the following methods of the ADT stack accepts a parameter? push pop createStack peek

22 Review If a stack is used by an algorithm to check for balanced braces, which of the following is true once the end of the string is reached? the stack is empty the stack has one “{” the stack has one “}” the stack has one “{” and one “}”

23 Review Which of the following operations of the ADT stack does not throw a StackException? push pop popAll peek

24 Review The pop operation throws a StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation of a stack that is already full delete an item from an array-based implementation of a stack that is already full delete an item from an empty stack

25 Review The push operation throws a StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation of a stack that is already full delete an item from an array-based implementation of a stack that is already full delete an item from an empty stack

26 Review In the StackInterface class, the pop method returns an item that is an instance of ______. Integer Double String Object


Download ppt "Chapter 7 Stacks II CS Data Structures I COSC 2006"

Similar presentations


Ads by Google