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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
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.
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-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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.
Stacks, Queues & Deques CSC212.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Summary of lectures (1 to 11)
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Chapter 7 Stacks II CS Data Structures I COSC 2006
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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,
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 The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
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.
Object-Oriented Programming Simple Stack Implementation.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
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.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Week 3 - Friday CS221.
Cinda Heeren / Geoffrey Tien
Stacks.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
ADT list.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks CS-240 Dick Steflik.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Presentation transcript:

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 chronological order (newest first)

Applications reversing the items in a list returning from a function/subroutine evaluation of postfix expressions converting infix expressions to postfix localization of parameters and variables in a function

Overflow and Underflow Stack overflow is the condition that arises when you attempt adding (pushing) an item onto an already full stack. –To avoid always check isFull( ) before adding Stack Underflow is the condition that arises when you attempt to remove (pop) an item from an already empty stack –To avoid always check isEmpty before popping

Normal methods constructor - create and initialize a stack object copy constructor - create a stack object that is a duplicate of another existing stack object (this method needed to insure correct value semantics) overloaded assignment operator - assign the value of an existing stack object (a) to another existing stack object (b) so that the result is that b is a duplicate of a (this method needed to insure correct value semantics) destructor - destroy a stack object by returning any of its dynamic storage back to the heap and setting its static elements to NULL or zero

Methods push - add an item (as the most recent) pop - delete the most recently added item pull - return the value of the most recently added item then delete the item peek - return the value of the most recently added item ifFull - return false/true depending if the stack is full; true if it is, false otherwise isEmpty - return false/true depending if the stack is empty; true if it is, false otherwise

Private data strategies use an array to hold items and use an int as an index for the array to indicate where the next item is to go same as above, but use a dynamic array use a struct to define a node and add nodes dynamically as they are needed; use one static pointer to a node at point at most recently added item(top of stack)

Static Static Implementation Class Stack { public: Stack( ){ top = 0; ) void push(int v) { data[top++] = v ; } void pop( ){ --top; } int pull( ) { return data[top--]; } int peek( ) { return data[top]; ) boolean isFull( ) { return top = = 20;} boolean isEmpty( ) { return top = = 0;} private: int data[20]; int top; }

Dynamic Array Implementation Class Stack { public: Stack( ){ size = 20 ; top = 0 ; data = new int[20] ; } Stack( int s ) { size = s ; top = 0 ; data = new int[s] ; ) void push(int v) { if (this.isFull( )) { temp = new int[size*2] ; size = size * 2; for (int j = 0 ; j < top; j++) temp[j] = data[j]; delete data ; data = temp; } data[top++] = v ; } int pop( ){ return data[--top]; } boolean isFull( ) { return top = = size;} boolean isEmpty( ) { return top = = 0;} private: int * data; int top; int size; }

Dynamic (nodal) implementation Class Stack { public: Stack( ){ top = NULL; ) ~Stack( ) { while (!this.isEmpty()) this.pop( ); } void push(int v) { node * temp = new node() ; temp -> data = v ; temp ->next = top ; top = temp; } int pull( ){ int value = top->data ; node * temp = top->next ; delete top ; top = temp; } void pop( ){ node * temp = top->next ; delete top ; top = temp; } boolean isEmpty( ) { return top = = NULL;} private: struct node { int data; node * next; } node * top; }