Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
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.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queue Overview Queue ADT Basic operations of queue
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.
CS Data Structures II Review COSC 2006 April 14, 2017
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
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.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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 Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
STACK Data Structure
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Click to edit Master text styles Stacks Data Structure.
CSCE 3110 Data Structures & Algorithm Analysis
Sections 3.4 Formal Specification
Stacks.
Stacks and Queues Chapter 4.
Stacks.
Stacks and Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Stack Data Structure, Reverse Polish Notation, Homework 7
CMSC 341 Lecture 5 Stacks, Queues
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
Stacks: Implemented using Linked Lists
Stacks CS-240 Dick Steflik.
template< class T > class Stack { public:
Python: Stacks and Queues (as an Array)
Stacks template<class T> class stack { public:
Stacks.
Presentation transcript:

Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element of stack Push … add an element to the top of the stack Pop … delete the top element of the stack

Stacks  Use a 1D array to represent a stack.  Stack elements are stored in stack[0] through stack[top].

Stacks  stack top is at element e  IsEmpty() => check whether top >= 0 O(1) time  IsFull() => check whether top == capacity – 1 O(1) time  Top() => If not empty return stack[top] O(1) time abcde

Derive From arrayList  Push(theElement) => if full then either error or increase capacity and then add at stack[top+1]  Suppose we increase capacity when full  O(capacity) time when full; otherwise O(1)  Pop() => if not empty, delete from stack[top]  O(1) time abcde

Push void push(element item) {/* add an item to the global stack */ if (top >= MAX_STACK_SIZE - 1) StackFull(); /* add at stack top */ stack[++top] = item; } abcde top

Pop element pop() { if (top == -1) return StackEmpty(); return stack[top--]; } abcde top

StackFull() void StackFull() { fprintf(stderr, “Stack is full, cannot add element.”); exit(EXIT_FAILURE); }

StackFull()/Dynamic Array  Use a variable called capacity in place of MAX_STACK_SIZE  Initialize this variable to (say) 1  When stack is full, double the capacity using REALLOC  This is called array doubling

StackFull()/Dynamic Array void StackFull() { REALLOC(stack, 2*capacity*sizeof(*stack); capacity *= 2; }

Complexity Of Array Doubling  Let final value of capacity be 2 k  Number of pushes is at least 2 k  Total time spent on array doubling is   i=k 2 i  This is O(2 k )  So, although the time for an individual push is O(capacity), the time for all n pushes remains O(n)!