CMSC 341 Lecture 7.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Mark Redekopp David Kempe
Chapter (3) - Lists, Stacks, and Queues
C++ Plus Data Structures
Lists The List ADT.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists.
Linked Lists, Stacks and Queues Textbook Sections
Lecture 21 Stacks and Queues Richard Gesick.
Deques, Stacks and Queues
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a.
CSC 143 Queues [Chapter 7].
CMSC 341.
Doubly Linked List Implementation
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
CMSC 341 Lists 3.
CMSC 341 Lists 3.
CMSC 341.
CMSC 341.
CMSC 341 Stacks and Queues 4/17/2019.
CMSC 341 Stacks and Queues 4/17/2019.
Stacks.
CMSC 341 Stacks and Queues 4/29/2019.
Queues.
CMSC 341 List 2.
Stacks, Queues, and Deques
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
Abstract Data Types Stacks CSCI 240
CMPT 225 Lecture 8 – Queue.
Data Structures & Programming
Presentation transcript:

CMSC 341 Lecture 7

Announcements Proj 2 up Project Preview tonight and tomorrow

Comparing Performance Constants: higher for doubly linked list lower for cursor

Stack ADT Restricted List only add to top only remove from top Examples pile of trays partial results local state

Relation to Previous List Using inheritance: Stack Is-A List Need to pay special intention that List operations are properly performed Using aggregation: Stack Has-A List Could code stack from scratch -- as book does could inherit from list, but some ops not appropriate use aggregation to use some ops With inheritance: stack.first() not valid, since it would allow iteration With aggregation: Stack operations translated to List operations

Stack.H void pop(); #include “LinkedList.H” template <class Object> class Stack { public: Stack(); Stack(const Stack &coll); ~Stack(); bool isEmpty() const; bool isFull() const; const Object &top() const; void makeEmpty(); void pop(); void push(const Object &x); Object topAndPop(); const Stack &operatr=(const Stack &stk); Private List data member which stores all stack elements. Operations are same as those in text. One difference, on an empty stack the following are illegal: top pop topAndPop

Stack.H (cont) protected: const List<Object> &getList() const; void setList(List<Object> &lst ListItr<Object> &getZeroth(); private: List<Object> _theList; ListItr<Object> _zeroth; }; Private data member that is an iterator. Used to provide position for insertion (push). It never changes.

Stack.C template <class Object> Stack<Object>::Stack(){ _zeroth = getList().zeroth(); } Stack<Object>::Stack(const Stack &stk) { _theList = stk.getList(); Stack<Object>::~Stack() {} bool Stack<Object>::isEmpty() const{ return getList().isEmpty(); Top() uses first() to get top element. Each time top is called, an iterator is constructed.

Stack.C (cont) template <class Object> bool Stack<Object>::isFull()const{ return false; } const Object & Stack<Object>::top() const{ if (isEmpty()) throw StackException(“top on empty stack”); return getList().first().retrieve(); void Stack<Object>::makeEmpty () { getList().makeEmpty(); Top() uses first() to get top element. Each time top is called, an iterator is constructed.

Stack.C (cont) template <class Object> void Stack<Object>::pop() { if (isEmpty()) throw StackException(“pop on empty stack”); getList().remove(top()); } void Stack<Object>::push(const Object &x) { getList().insert(x, getZeroth()); Object Stack<Object>::topAndPop () { throw StackException(“topAndPop on empty stack”); Object tmp = top(); pop(); return tmp; Top() uses first() to get top element. Each time top is called, an iterator is constructed.

Stack.C (cont) template <class Object> const Stack<Object> &Stack<Object>::operator=(const Stack &stk) { if (this != &stk){ setList(stk.getList()); setZeroth(getList().zeroth()); } return *this; const List<Object> &Stack<Object>::getList() { return _theList; ListItr<Object> &Stack<Object>::getZeroth () { return _zeroth; Top() uses first() to get top element. Each time top is called, an iterator is constructed.

StackException.H class StackException { public: StackException(); // Message is the empty string StackException(const string & errorMsg); StackException(const StackException & ce); ~StackException(); const StackException & operator=(const StackException & ce); const string & errorMsg() const; // Accessor for msg private: string _msg; };

StackException.C StackException::StackException(){} StackException::StackException(const string & errorMsg){ _msg = errorMsg; } StackException::StackException(const StackException &ce){ _msg = ce.errorMsg(); StackException::~StackException(){}

StackException.C (cont) const StackException & StackException::operator=(const StackException & ce){ if (this == &ce) return *this; // don't assign to itself _msg = ce.errorMsg(); return *this; } const string & StackException::errorMsg() const { return _msg;

TestStack.C int main (){ Stack<int> stk; stk.push(1); printStack(stk); Stack<int> otherstk; otherstk = stk; printStack(otherstk); cout << stk.topAndPop() << endl;

TestStack.C (cont) printStack(stk); printStack(otherstk); try { stk.pop(); } catch (StackException & e){ cout << e.errorMsg() << endl;

TestStack_aux.C template <class Object> void printStack( Stack<Object> & theStack ){ Stack<Object> tmp; if( theStack.isEmpty( ) ){ cout << "Empty stack" << endl; return; } while (theStack.isEmpty() == false) { Object topObj = theStack.top(); cout << topObj; tmp.push(topObj); // save on other stack theStack.pop(); if (theStack.isEmpty() == false) cout << ", "; cout << endl;

TestStack_aux.C while (tmp.isEmpty() == false) { Object topObj = tmp.top(); theStack.push(topObj); tmp.pop(); }

Queue ADT Restricted List only add to end only remove from front Examples line waiting for service jobs waiting to print Implementing a queue could use similar approach to stack want efficient access to both ends could use circular doubly-linked lists

Queue.H void dequeue(); template <class Object> class Queue { public: explicit Queue(int capacity=10); bool isEmpty() const; bool isFull() const; const Object &getFront() const; void makeEmpty(); void dequeue(); private: vector<Object> theArray; int currentSize; int front; int back; void increment (int &x); } Private List data member which stores all stack elements. Operations are same as those in text. One difference, on an empty stack the following are illegal: top pop topAndPop

Queue.C template <class Object> void Queue<Object>::enqueue(const Object &x){ if (isFull()) throw Overflow(); increment (back); theArray[back] = x; currentSize++; } void Queue<Object>::increment(int &x) { if (++x == theArray.size()) x = 0; Top() uses first() to get top element. Each time top is called, an iterator is constructed.

theArray Template<class Object> void Queue<Object>::makeEmpty() { currentSize = 0; front = 0; back = -1; }

Queue.C (cont) template <class Object> Object Queue<Object>::dequeue(){ if (isEmpty()) throw Underflow(); currentSize--; Object frontItem = theArray[front]; increment(front); return frontItem; } Top() uses first() to get top element. Each time top is called, an iterator is constructed.