27
Reported By: Michelle B. Alambra

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

Embed Size (px)

Citation preview

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

Reported By:

Michelle B. Alambra

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

Topics

What is a stack?Stack RepresentationStack OperationsExample of a Stack

Page 3: 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

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

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

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

Stack Representation

As a doubly-linked listwhen 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. TopBottom

Node1

Node2

Node3

Node n

……

Head Tail

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

Stack Operations

There are basically three (3) operations associated with stacks.

PushInserts 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.

TopMostRetrieves the element at the top of the

stack

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

Pushing an Element into a StackPush (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

Arr N Top

Value

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

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]

Banana Top

Arr [2]

Orange

Arr [1]

Apple Bottom

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

Pushing an Element into a StackTo “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

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

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

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

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

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

Poping an Element from a StackPop (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

Page 13: Reported By: Michelle B. Alambra. Topics What is a stack? Stack Representation Stack Operations Example of a 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]

Apple Bottom

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

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”

StrawberryDecrement Top Top = 6

Poping an element from a stack

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

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

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

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

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

Retrieving an element from a stack

TopMost (Arr1, Top){ Print Arr1[Top]}

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

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:

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

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.

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

Function Calls

When PROC1 begins executing, the stack will contain the following information:

Top and BottomOperating 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.

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

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.

Page 23: Reported By: Michelle B. Alambra. Topics What is a stack? Stack Representation Stack Operations Example of a 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.

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

Function Calls

BottomOperating 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

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

Function Calls

BottomOperating System

Topa:

Contents of Machine Registers

Once PROC1 has completed, the stack is POPed once more and control will

return to the operating system.

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

Function Calls

Top and BottomOperating System

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

THANK YOU