Stacks template<class T> class stack { public:

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
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.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
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.
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.
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.
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.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
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.
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.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Reminder: Course Policy
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Stacks Stacks.
Stacks 황승원 Fall 2010 CSE, POSTECH.
CSCI 3333 Data Structures Stacks.
Homework 4 questions???.
Stacks.
Stacks Chapter 7 introduces the stack data type.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack Data Structure, Reverse Polish Notation, Homework 7
The Class chain.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stacks template<class T> class stack { public:
Data Representation Methods
The Class arrayList General purpose implementation of linear lists.
Deques, Stacks and Queues
Data Representation Methods
Stacks, Queues, and Deques
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();
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks.
The Class chain.
The Class chain.
Stacks template<class T> class stack { public:
Data Representation Methods
Data Representation Methods
Stacks.
CMPT 225 Lecture 7 – Stack.
Data Structures & Programming
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks template<class T> 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 1 2 3 4 5 6 a b c d e stack top is either left end or right end of linear list empty() => arrayList::empty() O(1) time size() => arrayList::size() top() => get(0) or get(size() - 1) The decision whether to use the left or right end of the linear list as the stack top is made on the basis of efficiency of resulting stack methods. The complexity of empty(), size() and top() are independent of the choice.

Derive From arrayList when top is left end of linear list 1 2 3 4 5 6 a b c d e when top is left end of linear list push(theElement) => insert(0, theElement) O(size) time pop() => erase(0)

Derive From arrayList when top is right end of linear list 1 2 3 4 5 6 a b c d e when top is right end of linear list push(theElement) => insert(size(), theElement) O(1) time pop() => erase(size()-1) use right end of list as top of stack

Derive From Chain a b c d e NULL firstNode stack top is either left end or right end of linear list empty() => chain::empty() O(1) time size() => chain::size() The complexity of empty() and size() are independent of which end of the chain is used as the top of the stack. The complexity of the remaining methods depends on which end is the stack top.

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

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

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

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

empty() And size() bool empty() const 1 2 3 4 5 6 a b c d e bool empty() const {return arrayList<T>::empty();} int size() const {return arrayList<T>::size();}

top() T& top() { if (arrayList<T>::empty()) throw stackEmpty(); 1 2 3 4 5 6 a b c d e T& top() { if (arrayList<T>::empty()) throw stackEmpty(); return get(arrayList<T>::size() - 1); }

push(theElement) 1 2 3 4 5 6 a b c d e void push(const T& theElement) 1 2 3 4 5 6 a b c d e void push(const T& theElement) {insert(arrayList<T>::size(), theElement);}

pop() void pop() { if (arrayList<T>::empty()) 1 2 3 4 5 6 a b c d e void pop() { if (arrayList<T>::empty()) throw stackEmpty(); erase(arrayList<T>::size() - 1); }

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<T> with private chain<T> 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<T>::empty()) throw stackEmpty(); erase(arrayList<T>::size() - 1); vs. try {erase(arrayList<T>::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<T> class arrayStack : public stack<T> { public: // public methods come here private: int stackTop; // current top of stack int arrayLength; // stack capacity T *stack; // element array };

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

push(…) top 1 2 3 4 a b c d e template<class T> 1 2 3 4 a b c d e top template<class T> void arrayStack<T>::push(const T& theElement) {// Add theElement to stack. if (stackTop == arrayLength - 1) {// code to double capacity comes here } // add at stack top stack[++stackTop] = theElement;

pop() top 1 2 3 4 a b c d e void pop() { if (stackTop == -1) 1 2 3 4 a b c d e top void pop() { if (stackTop == -1) throw stackEmpty(); stack[stackTop--].~T(); // destructor for T }

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 numbers can be much lower on modern hardware

To-do Balanced parentheses: https://www.hackerrank.com/challenges/balanced-parentheses