1 / 38

ADTs Stack and Queue

ADTs Stack and Queue. Chapter 5. Outline. Stack Array-based Implementation Linked Implementation Queue Array-based Implementation Linked Implementation Comparison. Stacks of Coins and Bills. TOP OF THE STACK. TOP OF THE STACK. Stacks of Boxes and Books. Stacks. Logical level

fduarte
Télécharger la présentation

ADTs Stack and Queue

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. ADTs Stack and Queue Chapter 5 Yanjun Li CS2200

  2. Outline • Stack • Array-based Implementation • Linked Implementation • Queue • Array-based Implementation • Linked Implementation • Comparison Yanjun Li CS2200

  3. Stacks of Coins and Bills Yanjun Li CS2200

  4. TOP OF THE STACK TOP OF THE STACK Stacks of Boxes and Books Yanjun Li CS2200

  5. Stacks • Logical level • What do these composite objects all have in common ? Yanjun Li CS2200

  6. Stacks • Stack • An abstract data type in which elements are added and removed from only one end. • A “last in, first out” (LIFO) structure. Yanjun Li CS2200

  7. Stacks • Logical level • What operations would be appropriate for a stack? Yanjun Li CS2200

  8. Stacks • Transformers • Push • Pop • Observers • IsEmpty • IsFull • Top change state observe state What about an iterator? Yanjun Li CS2200

  9. Stacks • Application Level • For what type of problems would stacks be useful? • LIFO: A stack is great for reversing data. • Operating system function calls • Finding palindromes • Expression evaluation & syntax parsing Yanjun Li CS2200

  10. Stack Applications • Operating system function calls void DrawSquare(int x, int y, int edge) { DrawLine(x, y, edge, HORIZONTAL); DrawLine(x, y, edge, VERTICAL); DrawLine(x+edge, y, edge, VERTICAL); DrawLine(x, y+edge, edge, HORIZONTAL); } int main () { DrawSquare(1,2,3); return 0; } Yanjun Li CS2200

  11. Stack Applications • Finding palindromes (Lab!) Yanjun Li CS2200

  12. Stack Applications • Help Converting Decimal to Binary (pseudocode) • Read (number) • Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan • Problem: The binary numbers are printed backwards. • 19 becomes 11001 instead of 10011 • Solution: push each binary number onto the stack and pop the digit out of the stack and print it at the end. Yanjun Li CS2200

  13. Stacks class StackType { public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Yanjun Li CS2200

  14. Stacks • Implementation Level • Array-based Implementation • Linked Structure Yanjun Li CS2200

  15. Array-Based Implementation private: int top; ItemType items[MAX_ITEMS]; }; [0] [1] [2] …. [M..] stack .items .top Yanjun Li CS2200

  16. Array-Based Implementation Give a series of operations that could produce this situation Yanjun Li CS2200

  17. Array-Based Implementation To what do we initialize data member top ? 0 or -1 When push, do we increment or store first? 0: store and increment -1: increment and store Which is better ? • Think about member function Top() StackType::StackType() { top = -1; } Yanjun Li CS2200

  18. Array-Based Implementation //pre: Stack has been initialized. //post: function value = (stack is empty) bool StackType::IsEmpty() const { return ( top == -1); } //pre: stack has been initialized. //post: function value = (stack is full) bool StackType::IsFull() const { return ( top == MAX_ITEMS); } What does const mean? Yanjun Li CS2200

  19. Array-Based Implementation //pre: stack has been initialized and is not full //post: newItem is at the top of the stack. bool StackType::Push(ItemType newItem) { top++; items[top] = newItem; } Yanjun Li CS2200

  20. Array-Based Implementation • Before we code, we must consider error conditions • Stack overflow • The condition that results from trying to push an element on to a full stack • Stack underflow • The condition that results from trying to pop an empty stack Yanjun Li CS2200

  21. Updated Stacks #include "ItemType.h" #include "FullStack.h" #include "EmptyStack.h" class StackType { public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item) throw(FullStack); void Pop( ) throw (EmptyStack); ItemType Top() const throw (EmptyStack); Yanjun Li CS2200

  22. Array-Based Implementation //pre: stack has been initialized. //post: if stack is full, throw an exception; //Else newItem is at the top of the stack. bool StackType::Push(ItemType newItem) throw (FullStack) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } What is FullStack( ) ? Yanjun Li CS2200

  23. Exception Class: FullStack #ifndef FULLSTACK_H #define FULLSTACK_H #include <stdexcept> using std::runtime_error; class FullStack : public runtime_error { public: FullStack() : runtime_error("The stack is full") { } }; #endif Yanjun Li CS2200

  24. Array-Based Implementation //pre: stack has been initialized and is not empty. //post: top element has been removed from stack. void StackType::Pop() { top--; } //pre: stack has been initialized and is not empty. //post: A copy of the top element is returned. ItemType StackType:: Top() const { return (items[top]); } Yanjun Li CS2200

  25. Array-Based Implementation //pre: stack has been initialized. //post: if stack is empty, throw an exception; else top element //has been removed from stack. void StackType::Pop() throw (EmptyStack) { if (IsEmpty()) throw EmptyStack(); top--; } //pre: stack has been initialized. //post: if stack is empty, throw an exception; else a copy of the //top element is returned. ItemType StackType::Top() const throw (EmptyStack) { if (IsEmpty()) throw EmptyStack(); return (items[top]); } What is EmptyStack ? Yanjun Li CS2200

  26. Exception Class: Empty Stack #ifndef EMPTYSTACK_H #define EMPTYSTACK_H #include <stdexcept> using std::runtime_error; class EmptyStack : public runtime_error { public: EmptyStack() : runtime_error("The stack is empty"){ } }; #endif Yanjun Li CS2200

  27. Test Plan • Clear-box strategy to check each operation. • Push() & Pop() while it is empty or full. • Black-box strategy to check. • Try-Catch block Yanjun Li CS2200

  28. Linked Implementation • The logical level (public part of the class declaration) stays the same; class StackType { public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Yanjun Li CS2200

  29. .info .next ‘D’ Pointer to the next node in the stack .topPtr Linked Implementation The implementation level (private part of the class declaration) changes private: NodeType* topPtr; }; Stack Can we “borrow” code from UnsortedType for Pushand Pop ? Yanjun Li CS2200

  30. .info .next newItem location Linked Implementation //pre: the stack is not full //post: the new item is added on top of the stack void StackType::Push(ItemType newItem) { NodeType* location; location = new NodeType; location->info = newItem; location>next = topPtr; topPtr = location; } .info .next ‘D’ .topPtr Yanjun Li CS2200

  31. Linked Implementation //pre: the stack is not empty //post: the top item is removed from the top of the //stack void StackType::Pop() { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } Does this work for stacks of one item? More than one item? Yanjun Li CS2200

  32. Linked Implementation More than one item Yanjun Li CS2200

  33. Linked Implementation One item Yanjun Li CS2200

  34. Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all but Top() from class UnsortedType List //pre: the stack is not empty //post: the item on the top of the stack is //returned. ItemType StackType::Top() { return topPtr->info; } Yanjun Li CS2200

  35. Other Member Functions • Constructor • Destructor • Free all the node spaces. • isEmpty • isFull Yanjun Li CS2200

  36. Array vs. Linked Structure • A serious drawback of array-based implementation: the size of a stack must be determined when a stack object is declared. • Size is not enough or • Space is wasted. Yanjun Li CS2200

  37. Big-O Comparison Yanjun Li CS2200

  38. Reference • Reproduced from C++ Plus Data Structures, 4th edition by Nell Dale. • Reproduced by permission of Jones and Bartlett Publishers International. Yanjun Li CS2200

More Related