Reported By: Michelle B. Alambra. Topics What is a stack? Stack Representation Stack Operations Example of a Stack.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
1 Procedural Programming Paradigm Stacks and Procedures.
CPU Review and Programming Models CT101 – Computing Systems.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Data Structures & Algorithms
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data Structures from Cormen, Leiserson, Rivest & Stein.
Summary of lectures (1 to 11)
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Stack and Queue.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
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.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
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.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CHP-3 STACKS.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Computer Engineering Rabie A. Ramadan Lecture 6.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Stack Operations Dr. Hadi AL Saadi.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Queues.
Chapter 15 Lists Objectives
Dr. Bernard Chen Ph.D. University of Central Arkansas
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Stacks Chapter 4.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
Principles of Computing – UFCFA3-30-1
Data Structures and Database Applications Stacks in C#
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Type Abstract Data Type as a design tool
Chapter 5 Stack (part 1).
List Iterator Implementation
Computer Organization and Assembly Language
Introduction to Pointers
Presentation transcript:

Reported By: Michelle B. Alambra

Topics What is a stack? Stack Representation Stack Operations Example of a Stack

What is a Stack? A stack is an ordered list where all operations are restricted at one end of the list known as the top. Imagine the cafeteria tray holder. The type of behavior in list processing is called LIFO or Last-In-First-out. A pile of Tray

Stack Representation There are two ways to represent a stack  As a one-dimensional array when using a one-dimensional array to portray a stack, insertion, deletion, and retrieval is done from the last element in the array. This form of representation is used when the maximum size of the stack is known. As one –dimensional Array Arr[n] Arr[n-1] … … … Arr[3] Arr[2] Arr[1] Top Bottom

Stack Representation  As a doubly-linked list when using a doubly-linked list to portray a stack, insertion, deletion, and retrieval is done from the tail. This form of representation is used when the maximum size of the stack is unknown. As Doubly-Linked List Top Bottom Node1Node2Node3Node n …… Head Tail

Stack Operations There are basically three (3) operations associated with stacks.  Push Inserts a new element at the top of the stack.  Pop Retrieves the element at the top of the stack and deletes it from the stack.  TopMost Retrieves the element at the top of the stack

Pushing an Element into a Stack Push (Arr, N, Top, Value) { If (Top = N) then { Print (“Error: Stack is Full!”) Exit } Increment Top Set Arr[Top] to Value // insert new element } Push (,,, ) { If (Top = N) then { Print (“Error: Stack is Full!”) Exit } Increment Top Set Arr[Top] to Value // insert new element } Name of a one Dimensional array representing the stack The size of an Array Position of the topmost element in the stack The value to be inserted into the stack ArrNTopValue

Pushing an Element into a Stack Let us simulate the algorithm by using the following stack. Arr [10] Arr [9] Arr [8] Arr [7] Arr [6] Arr [5] Arr [4] Arr [3]BananaTop Arr [2]Orange Arr [1]AppleBottom

Pushing an Element into a Stack To “push” an element into the stack, we will pass the following parameters to the algorithm: (Arr, 10, 3, “Mango”) Arr [10] Arr [9] Arr [8] Arr [7] Arr [6] Arr [5] Arr [4] Arr [3]Banana Arr [2]Orange Arr [1]Apple Mango Top Bottom Increment Top Set Arr[Top] to Value

Pushing an Element into a Stack (Arr, 10, 4, “Pomelo”) Arr [10] Arr [9] Arr [8] Arr [7] Arr [6] Arr [5] Arr [4]Mango Arr [3]Banana Arr [2]Orange Arr [1]Apple Pomelo Top Bottom Increment Top Set Arr[Top] to Value

Pushing an Element into a Stack (Arr, 10, 5, “Melon”) Arr [10] Arr [9] Arr [8] Arr [7] Arr [6] Arr [5]Pomelo Arr [4]Mango Arr [3]Banana Arr [2]Orange Arr [1]Apple Melon Top Bottom Increment Top Set Arr[Top] to Value

Poping an Element from a Stack Pop (Arr1, N, Top) { If (Top = 0) then { Print (“Error: Stack is Empty!”) Exit } Print Arr1[Top] Decrement Top } Pop (,, ) { If (Top = 0) then { Print (“Error: Stack is Empty!”) Exit } Print Arr1[Top] Decrement Top } Arr1 N Top Name of a one Dimensional array representing the stack The size of an Array Position of the topmost element in the stack

Poping an element from a stack Let us use the following stack Arr1 [10] Arr 1[9] Arr 1[8] Arr 1[7] Arr 1[6]Strawberry Top Arr 1[5]Pomelo Arr 1[4]Mango Arr 1[3]Banana Arr 1[2]Orange Arr 1[1]AppleBottom

To “pop” an element into the stack, we should have the following parameters: (Arr1, 10, 7) Arr 1[10] Arr 1[9] Arr 1[8] Arr 1[7] Arr 1[6]Melon Arr 1[5]Pomelo Arr 1[4]Mango Arr 1[3]Banana Arr 1[2]Orange Arr 1[1]Apple Top Bottom Print Arr1[Top] “Strawberry” Strawberry Decrement Top Top = 6 Poping an element from a stack

(Arr1, 10, 6) Arr1 [10] Arr 1[9] Arr 1[8] Arr 1[7] Arr 1[6] Arr 1[5]Pomelo Arr 1[4]Mango Arr 1[3]Banana Arr 1[2]Orange Arr 1[1]Apple Top Bottom Melon Print Arr1[Top] “Melon” Decrement Top Top = 5

Poping an element from a stack (Arr1, 10, 6) Arr 1[10] Arr 1[9] Arr 1[8] Arr 1[7] Arr 1[6] Arr 1[5] Arr 1[4]Mango Arr 1[3]Banana Arr 1[2]Orange Arr 1[1]Apple Top Bottom Pomelo Print Arr1[Top] “Pomelo” Decrement Top Top = 4

Retrieving an element from a stack TopMost (Arr1, Top) { Print Arr1[Top] }

Function Calls  One natural example of stacks which arises in computer programming is the processing of function calls and their returns.  Suppose we have the following procedure which indirectly combines two (2) functions:

Function Calls  PROC1 calls FUNC1, and FUNC1 calls FUNC2.  When FUNC2 has completed, processing will continue at statement b in FUNC1.  When FUNC1 has completed, processing will resume at statement a in PROC1.  Generally, the variables used in a program are assigned by the compiler to machine registers.  When another function called the variables used by this new function are also assigned to the same machine registers, thereby erasing all previous content.  Once the new function has completed, the invoking procedure would have lost all relevant procedure prior to the call.

Function Calls When PROC1 begins executing, the stack will contain the following information: Top and Bottom Operating System It relinquishes control to the operating system. As with function calls, there must be a way for PROC1 to know “where to go” when it is done. This is why the bottom of the stack always contains information about the operating system.

Function Calls When FUNC1 is invoke by PROC1, the stack contains the following: Bottom a: Contents of Machine Registers Operating System Top When FUNC1 was called, the system pushed the return address, a, and the contents of the machine registers into the stack.

Function Calls When FUNC1 will reference FUNC2, the stack will contain the following information: Bottom b: Contents of Machine Registers a: Contents of Machine Registers Operating System Top After the FUNC2 has completed, the system will determine where processing should continue by POPing the information from the stack. Since the top of the stack contains, b, processing will continue with statement b in FUNC1.

Function Calls Bottom Operating System Top The system will return the values of all variables by once again POPing the stack. b: Contents of Machine Registers a: Contents of Machine Registers

Function Calls Bottom Operating System Top a: Contents of Machine Registers Once PROC1 has completed, the stack is POPed once more and control will return to the operating system.

Function Calls Top and Bottom Operating System

THANK YOU