Download presentation
Presentation is loading. Please wait.
Published byRandolph Derek Atkinson Modified over 9 years ago
1
Reported By: Michelle B. Alambra
2
Topics What is a stack? Stack Representation Stack Operations Example of a Stack
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
(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
16
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
17
Retrieving an element from a stack TopMost (Arr1, Top) { Print Arr1[Top] }
19
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:
20
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.
21
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.
22
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.
23
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.
24
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
25
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.
26
Function Calls Top and Bottom Operating System
27
THANK YOU
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.