Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
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.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Queues CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
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 Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
SNU IDB Lab. 1 Ch9. Stacks © copyright 2006 SNU IDB Lab.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Linear List Array representation Data structures A data structure is a particular way of storing and manipulating data in a computer so that it can be.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
Reminder: Course Policy
Stacks Stacks.
Stacks 황승원 Fall 2010 CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks.
The Class chain.
Stacks, Queues, and Deques
Stacks template<class T> class stack { public:
The Class arrayList General purpose implementation of linear lists.
Stacks, Queues, and Deques
Stacks template<class T> class stack { public:
Queues Linear list. One end is called front. Other end is called rear.
Stacks template<class T> class stack { public:
Stacks public interface Stack { public boolean empty();
The Class chain.
The Class chain.
Stacks template<class T> class stack { public:
Stacks, Queues, and Deques
Queues Linear list. One end is called front. Other end is called rear.
Data Structures & Programming
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Stack Of Cups Add a cup to the stack. bottom top C A B D E F Remove a cup from new stack. A stack is a LIFO list. bottom top C A B D E

The Abstract Class stack template class stack { public: virtual ~stack() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& top() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

Derive From A Linear List Class arrayList chain

Derive From arrayList  stack top is either left end or right end of linear list  empty() => arrayList::empty() O(1) time  size() => arrayList::size() O(1) time  top() => get(0) or get(size() - 1) O(1) time abcde

Derive From arrayList when top is left end of linear list  push(theElement) => insert(0, theElement)  O(size) time  pop() => erase(0)  O(size) time abcde

Derive From arrayList  when top is right end of linear list push(theElement) => insert(size(), theElement) O(1) time pop() => erase(size()-1) O(1) time  use right end of list as top of stack abcde

Derive From Chain  stack top is either left end or right end of linear list  empty() => chain::empty() O(1) time  size() => chain::size() O(1) time abcde NULL firstNode

Derive From Chain abcde NULL firstNode – when top is left end of linear list  top() => get(0)  O(1) time  push(theElement) => insert(0, theElement)  O(1) time  pop() => erase(0)  O(1) time

Derive From Chain abcde null firstNode – when top is right end of linear list top() => get(size() - 1) O(size) time push(theElement) => insert(size(), theElement) O(size) time pop() => erase(size()-1) O(size) time – use left end of list as top of stack

Derive From arrayList template class derivedArrayStack : private arrayList, public stack { public: // code for stack methods comes here };

Constructor derivedArrayStack(int initialCapacity = 10) : arrayList (initialCapacity) {}

empty() And size() bool empty() const {return arrayList ::empty();} int size() const {return arrayList ::size();} abcde

top() T& top() { if (arrayList ::empty()) throw stackEmpty(); return get(arrayList ::size() - 1); } abcde

push(theElement) void push(const T& theElement) {insert(arrayList ::size(), theElement);} abcde

pop() void pop() { if (arrayList ::empty()) throw stackEmpty(); erase(arrayList ::size() - 1); } abcde

Evaluation Merits of deriving from arrayList  Code for derived class is quite simple and easy to develop.  Code is expected to require little debugging.  Code for other stack implementations such as a linked implementation are easily obtained. Just replace private arrayList with private chain For efficiency reasons we must also make changes to use the left end of the list as the stack top rather than the right end.

Demerits Unecessary work is done by the code.  top() verifies that the stack is not empty before get is invoked. The index check done by get is, therefore, not needed.  insert(size(), theElement) does an index check and a copy_backward. Neither is needed.  pop() verifies that the stack is not empty before erase is invoked. erase does an index check and a copy. Neither is needed.  So the derived code runs slower than necessary.

Evaluation Code developed from scratch will run faster but will take more time (cost) to develop. Tradeoff between software development cost and performance. Tradeoff between time to market and performance. Could develop easy code first and later refine it to improve performance.

A Faster pop() if (arrayList ::empty()) throw stackEmpty(); erase(arrayList ::size() - 1); vs. try {erase(arrayList ::size()-1); catch (illegalIndex {throw stackEmpty();}

Code From Scratch Use a 1D array stack whose data type is T.  same as using array element in arrayList Use an int variable stackTop.  Stack elements are in stack[0:stackTop].  Top element is in stack[stackTop].  Bottom element is in stack[0].  Stack is empty iff stackTop = -1.  Number of elements in stack is stackTop + 1.

Code From Scratch template class class arrayStack : public stack { public: // public methods come here private: int stackTop; // current top of stack int arrayLength; // stack capacity T *stack; // element array };

Constructor template arrayStack ::arrayStack(int initialCapacity) {// Constructor. if (initialCapacity < 1) {// code to throw an exception comes here } arrayLength = initialCapacity; stack = new T[arrayLength]; stackTop = -1; }

push(…) template void arrayStack ::push(const T& theElement) {// Add theElement to stack. if (stackTop == arrayLength - 1) {// code to double capacity coms here } // add at stack top stack[++stackTop] = theElement; } abcde top

pop() void pop() { if (stackTop == -1) throw stackEmpty(); stack[stackTop--].~T(); // destructor for T } abcde top

Linked Stack From Scratch See text.

Performance 50,000,000 pop, push, and peek operations initial capacity Class 10 50,000,000 arrayStack 2.7s 1.5s derivedArrayStack 7.5s 6.3s STL 5.6s - derivedLinkedStack 41.0s 41.0s linkedStack 40.5s 40.5s

Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.

The Abstract Class queue template class queue { public: virtual ~queue() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& front() = 0; virtual T& back() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

Revisit Of Stack Applications Applications in which the stack cannot be replaced with a queue.  Parentheses matching.  Towers of Hanoi.  Switchbox routing.  Method invocation and return.  Try-catch-throw implementation. Application in which the stack may be replaced with a queue.  Rat in a maze. Results in finding shortest path to exit.

Derive From arrayList  when front is left end of list and rear is right end empty() => queue::empty() –O(1) time size() => queue::size(0) –O(1) time front() => get(0) –O(1) time back() => get(size() - 1) –O(1) time abcde

Derive From arrayList pop() => erase(0) –O(size) time push(theElement) => insert(size(), theElement) –O(1) time abcde

Derive From arrayList  when front is right end of list and rear is left end empty() => queue::empty() –O(1) time size() => queue::size(0) –O(1) time front() => get(size() - 1) –O(1) time back() => get(0) –O(1) time abcde

Derive From arrayList pop() => erase(size() - 1) –O(1) time push(theElement) => insert(0, theElement) –O(size) time abcde

Derive From arrayList  to perform each opertion in O(1) time (excluding array doubling), we need a customized array representation.

Derive From extendedChain abcde NULL firstNode lastNode frontrear  when front is left end of list and rear is right end empty() => extendedChain::empty() – O(1) time size() => extendedChain::size() – O(1) time

Derive From ExtendedChain abcde NULL firstNode lastNode frontrear front() => get (0) – O(1) time back() => getLast() … new method

Derive From ExtendedChain abcde NULL firstNode lastNode frontrear push(theElement) => push_back(theElement) – O(1) time pop() => erase(0) – O(1) time

Derive From extendedChain edcba NULL firstNode lastNode rearfront  when front is right end of list and rear is left end empty() => extendedChain::empty() – O(1) time size() => extendedChain::size() – O(1) time

Derive From extendedChain abcde NULL firstNode lastNode rearfront front() => getLast() – O(1) time back() => get(0) – O(1) time

Derive From extendedChain abcde NULL firstNode lastNode rearfront push(theElement) => insert(0, theElement) – O(1) time pop() => erase(size() - 1) – O(size) time

Custom Linked Code Develop a linked class for queue from scratch to get better preformance than obtainable by deriving from extendedChain.

Custom Array Queue Use a 1D array queue. queue[] Circular view of array. [0] [1] [2][3] [4] [5]

Custom Array Queue Possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Another possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Use integer variables theFront and theBack. –theFront is one position counterclockwise from first element –theBack gives position of last element –use front and rear in figures [0] [1] [2][3] [4] [5] AB C front rear [0] [1] [2][3] [4] [5] AB C front rear

Push An Element [0] [1] [2][3] [4] [5] AB C front rear Move rear one clockwise.

Push An Element Move rear one clockwise. [0] [1] [2][3] [4] [5] AB C front rear Then put into queue[rear]. D

Pop An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise.

Pop An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise. Then extract from queue[front].

Moving rear Clockwise [0] [1] [2][3] [4] [5] AB C front rear rear++; if ( rear = = arrayLength) rear = 0; rear = (rear + 1) % arrayLength;

Empty That Queue [0] [1] [2][3] [4] [5] AB C front rear

Empty That Queue [0] [1] [2][3] [4] [5] B C front rear

Empty That Queue [0] [1] [2][3] [4] [5] C front rear

Empty That Queue When a series of removes causes the queue to become empty, front = rear. When a queue is constructed, it is empty. So initialize front = rear = 0. [0] [1] [2][3] [4] [5] front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear D

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE F When a series of adds causes the queue to become full, front = rear. So we cannot distinguish between a full queue and an empty queue!

Ouch!!!!! Remedies.  Don’t let the queue get full. When the addition of an element will cause the queue to be full, increase array size. This is what the text does.  Define a boolean variable lastOperationIsPush. Following each push set this variable to true. Following each pop set to false. Queue is empty iff (front == rear) && !lastOperationIsPush Queue is full iff (front == rear) && lastOperationIsPush