1 / 26

CS2006 - Data Structures I

This chapter covers the implementation of ADT stack using linked lists. It discusses the growth and shrinkage of stacks dynamically, various variations of linked lists, and the usage of top/head pointer as the top of the stack.

otisj
Download Presentation

CS2006 - Data Structures I

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. CS2006 - Data Structures I Chapter 7StacksII

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

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

  4. Linked List Stack Implementation • The head of List is the top of the stack head 1 3 7 9 1 top 3 7 9

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

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

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

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

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

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

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

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

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

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

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

  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

More Related