reported by: michelle b. alambra. topics what is a stack? stack representation stack operations...

Post on 17-Dec-2015

240 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Reported By:

Michelle B. Alambra

Topics

What is a stack?Stack RepresentationStack OperationsExample 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 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

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

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

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

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

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

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

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

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

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

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

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.

Function Calls

Top and BottomOperating System

THANK YOU

top related