Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.

Slides:



Advertisements
Similar presentations
1 Procedural Programming Paradigm Stacks and Procedures.
Advertisements

Ch. 8 Functions.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Reported By: Michelle B. Alambra. Topics What is a stack? Stack Representation Stack Operations Example of a Stack.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
1 Chapter 1 Object-Oriented Programming. 2 OO programming and design Object-oriented programming and design can be contrasted with alternative programming.
Programming with Recursion
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Stacks. 2 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 removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 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 removed.
Summary of lectures (1 to 11)
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.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
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.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
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.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
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.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
STACK Data Structure
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Click to edit Master text styles Stacks Data Structure.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Functions.
Chapter 4 The easy stuff.
Chapter 15 Lists Objectives
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Programming Fundamentals Lecture #7 Functions
Introduction to C++ Recursion
CS313D: Advanced Programming Language
CSC 205 – Java Programming II
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks.
Stack Memory 2 (also called Call Stack)
Data Structures and Algorithms
Stacks.
Introduction to Data Structure
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Stacks.
Presentation transcript:

Stacks

An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions and deletions occur at one end of the list, known as the top or head of the stack. When an element is inserted at the top of a stack it is said to have been pushed onto the stack.

When an item has been deleted from the top of the stack it is said to have been popped off the stack. When a pop occurs the item at the top of the stack is returned as the output from the pop operation.

Initially the stack is empty No items can be popped push(3) 3 3 placed on top of stack

push(11) pop() inserted at top of stack 11 is returned from the pop operation and removed from stack

push(7) push(6) stack grows downwards

Since the last element pushed onto the stack is the first one to be popped, Stacks are known as Last-In-First- Out ( LIFO ) lists. Stacks and Function Invocation Stacks are very useful data structures in Computer Science. Their LIFO operation makes them suitable for certain types of problems, in particular function invocation.

Consider the following function declarations: int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; }

void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } The output from the program is: In f2: 5 In f1: 4 In main: 3

The code segment contains three different occurrences of the variable n. 1. In function main(), where it is passed twice as an argument; 2. In function f1(), where it is an input parameter; 3. In function f2(), where it is an input parameter. The question that might arise is how does the computer know which instance of n to use in each situation and what value it contains.

In computers this conflict is solved with the use of stack frames. When a function is called the computer initialises a stack frame containing the bindings of the function arguments with the formal parameters to the function, as well as any local variables. The stack frame is then pushed onto the stack. When the function is being executed, the computer uses the stack frame at the top of the stack to inspect the values of the function parameters. When the function returns, the stack is popped.

Reconsider our code segment: On execution of main(), a stack frame is initialised and pushed on the stack. The stack frame will contain the binding for the local variable n. n =3 Stack frame

n =3 Stack frame void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } f1() is now called with the value n+1.

n =3 Stack frame The computer inspects the top stack frame to identify the value of n as 3. The value 3 is then incremented by one and passed into the function f1().

n=4, i new f1() Stack frame As soon as execution of f1() begins a new stack frame is pushed onto the stack containing the variable bindings of f1(). int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } n=3 main() Stack frame

n=4, i new f1() Stack frame During execution of f1() a call to f2() is invoked with the argument n+1. The computer inspects the top stack frame to find the value of n, which is 4. The value 5 is now passed to the function f2(). n=3 main() Stack frame

n=4, i f1() Stack frame As soon as execution of f2() begins a new stack frame is pushed onto the stack containing the variable bindings of f2(). int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; } n=3 main() Stack frame n=5, j new f2() Stack frame

n=4, i f1() Stack frame In f2() the stack frame is inspected to find the value of n and the following output is printed: In f2: 5 The function then returns 25 and the stack frame is popped from the stack. int f2(int n) { int j = n * n; printf("In f2: %d\n", n); return j; } n=3 main() Stack frame n=5, j=25 f2() Stack frame

In f1() the stack frame is inspected to find the value of n and the following output is printed: In f1: 4 The function then returns 25 and the stack frame is popped from the stack. int f1(int n) { int i = f2(n + 1); printf("In f1: %d\n", n); return i; } n=3 main() Stack frame n=4, i=25 f1() Stack frame

In main() the stack frame is inspected to find the value of n and the following output is printed: In main: 3 void main() { int n = 3; f1(n + 1); printf("In main: %d\n", n); } n=3 main() Stack frame

The following example highlights the benefit of using stacks for function invocation, particularly when functions contain variables of the same name. It should also be obvious why elegant recursive solutions do not translate efficiently to computers. The initialisation, pushing and popping of stack frames in a recursive programming solution generates large execution and storage overheads, reducing overall performance.

Stack Implementation It has been shown that a stack can be implemented as a list with insertions ( pushes ) and deletions ( pops ) at one end. A push can therefore insert an element at position 0, while a pop can be defined as retrieving the element at position 0 and then deleting it Top of Stack Stack using a List implementation

The following is a stack class which inherits the properties of the List class: class Stack : public List { public: Stack(); void push(Element); Element pop(); } /* Since stacks are simple lists, simply involve the List constructor */ Stack::Stack() : List() {}

void Stack::push(Element e) { /* Rely on the List insert behavior */ insert(e,0); } Element Stack::pop() { Element e; /* Retrieve the first element in the list */ e = retrieve(0); /* Now delete it */ delete(e); /* and return it */ return e; }