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.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
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.
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.
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
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
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.
Data Structures & Algorithms
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.
Chapter 3. 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.
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.
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.
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.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
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 STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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 and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CSCE 3110 Data Structures & Algorithm Analysis
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Stacks.
Stacks and Queues Chapter 4.
Homework 4 questions???.
Stacks.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack.
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 passing arguments to a function 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

ADT 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) 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

ADT 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 isFull - 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 use a struct to define a stack type 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 int stack[MAXSIZE]; int top; void empty(void) { top = 0; }; void push(int v) { if (top >= MAXSIZE) stackfull(); else stack[top++] = v; }; int pop(void) { if (top < 0 ) stackempty(); else return (stack[--top];};

Define a stack type typedef int item; typedef struct { int top; item data[MAXSIZE]; } stack; void stackInit( stack * id); void push(stack * id, item v); item pop(stack * id) ; int isFull(stack id); int isEmpty(stack id);

struct based stack Remember structs are passed by value, so if you pass a struct to a function the function can look at the data but not change it To be able to change it you must pass its address and pass data back through that address (see push on next slide)

void push(Stack * id, int v) { int k = id->top; id->data[k] = v ; id->top++; } ; in the main() push would be invoked as: stack a; stackInit(&a); push( &a, 5);

with a Macro #ifndef _STACK_H #define _STACK_H #include #define STACK (name) int name[MAXSTACKSIZE]={0} ; name[0]=1; void push(int id[],int v); int pop (int id[]); int isFull(int id[]); int isEmpty(int id[]); #endif // notice the clever way that the 0 th position of the array is used as the top // pointer; this makes everything the stack needs self contained

How it’s used #include #include "stack.h" int main() { int i; a = 5; STACK(stack1) ; STACK(stack2) ; push(stack1,5); push(stack1,10); push(stack1,15); push(stack1,20); push(stack1,25); for (i=0 ; i< MAXSTACKSIZE ; i++) printf("V = %d ", stack1[i]); printf("\n"); };

Stack Machines A machine where the memory of the computer model takes the form of one or more stacks that are manipulated by the machines instruction set. Could be : –A real physical machine –A virtual machine

Real Physical Stack machines Burroughs Large System Computers Rockwell-Collins Atmel MARC4 MCU Saab Erocsson Tandem Computers Pascal and Forth machines/chips

Virtual Stack Machines UCSD Pascal p-machine Java Virtual Machine Virtual Execution System (VES) used to execute managed code for Common Intermediate Language for Microsoft.Net (VB, C++ and C#) Forth programming language Adobe PostScript interpreter