Data Structures - Lecture 5 [Stack]

22
Stack Muhammad Hammad Waseem [email protected]

Transcript of Data Structures - Lecture 5 [Stack]

StackMuhammad Hammad Waseem

[email protected]

What is a Stack?

• A stack is a data structure that stores data in such a way that the last piece of data stored, is the first one retrieved• LAST IN FIRST OUT = LIFO

• Example

• Only access to the stack is the top element• consider trays in a cafeteria

• to get the bottom tray out, you must first remove all of the elements above

• Which is the first element to pick up?

M.Hammad Waseem 2

Last In First Out

BA

DCBA

CBA

DCBA

EDCBA

top

top

top

toptop

A

top

M.Hammad Waseem 3

Stack Operation

• Push• The operation to place a

new item at the top of the stack.

• Top is incremented after adding item.

• Pop• The operation to remove

the next item from the top of the stack.

• Top is decremented before deleting item.

A

X

R

C

push(M)

A

X

R

C

M

item = pop()item = M

A

X

R

C

M.Hammad Waseem 4

Push

void push(int *top, element item)

{/* add an item to the global stack */

if (*top >= MAX_STACK_SIZE-1) {

stack_full( );

return;

}

stack[++*top] = item;

}

M.Hammad Waseem 5

Pop

element pop(int *top)

{

/* return the top element from the stack */

if (*top == -1)

return stack_empty( ); /* returns and error key */

return stack[(*top)--];

}

M.Hammad Waseem 6

Notes on push() and pop()

• Other ways to do this even if using arrays• may want to keep a size variable that tracks how many items in the list

• may want to keep a maxSize variable that stores the maximum number of elements the stack can hold (size of the array)• you would have to do this in a language like C++

• could add things in the opposite direction• keep track of nextOut and decrement it on every push; increment it on every pop

M.Hammad Waseem 7

Implementing Stacks: Array

• Advantages• best performance

• Disadvantage• fixed size

• Basic implementation• initially empty array• field to record where the next data gets placed into• if array is full, push() returns false

• otherwise adds it into the correct spot

• if array is empty, pop() returns null• otherwise removes the next item in the stack

M.Hammad Waseem 8

Implementing Stacks#include<iostream.h>

void push(); void pop();

int top=0;

int stack[10];

void main()

{

int opt;

do {

cout<<"\nMain Menu\n1-PUSH an Element\n2-POP an Element\n3-

Exit\nEnter Your Option : ";

cin>>opt;

switch(opt)

{

case 1:

push(); break;

case 2:

pop(); break;

case 3:

return;

default:

cout<<"\nInvalid Option";

}

} while(1);

}

void push()

{

if(top==10)

cout<<"\nStack Overflow";

else

{

cout<<"\nEnter An Element : ";

cin>>stack[top];

top++;

}

}

void pop()

{

if(top==0)

cout<<"\nStack Underflow\n";

else

{

cout<<"\n"<<stack[top]<<"Has Been

Poped\n";

top--;

}

}

M.Hammad Waseem 9

Stack Applications

• Real life• Pile of books• Plate trays

• More applications related to computer science• Program execution stack (read more from your text)• Evaluating expressions• Compilers

• parsing data between delimiters (brackets)

• Operating systems• program stack

• Virtual machines• manipulating numbers

• pop 2 numbers off stack, do work (such as add)• push result back on stack and repeat

• Artificial intelligence• finding a path

M.Hammad Waseem 11

A Legend: The Towers of Hanoi

• In the great temple of Brahma in Benares, on a brass plate under the dome that marks the center of the world, there are 64 disks of pure gold that the priests carry one at a time between these diamond needles according to Brahma's immutable law: No disk may be placed on a smaller disk. In the begging of the world all 64 disks formed the Tower of Brahma on one needle. Now, however, the process of transfer of the tower from one needle to another is in mid course. When the last disk is finally in place, once again forming the Tower of Brahma but on a different needle, then will come the end of the world and all will turn to dust.

M.Hammad Waseem 12

The Towers of HanoiA Stack-based Application

• GIVEN: • three poles

• a set of discs on the first pole, discs of different sizes, the smallest discs at the top

• GOAL: move all the discs from the left pole to the right one.

• CONDITIONS: • only one disc may be moved at a time.

• A disc can be placed either on an empty pole or on top of a larger disc.

M.Hammad Waseem 13

Towers of Hanoi

M.Hammad Waseem 14

Towers of Hanoi

M.Hammad Waseem 15

Towers of Hanoi

M.Hammad Waseem 16

Towers of Hanoi

M.Hammad Waseem 17

Towers of Hanoi

M.Hammad Waseem 18

Towers of Hanoi

M.Hammad Waseem 19

Towers of Hanoi

M.Hammad Waseem 20

Towers of Hanoi

M.Hammad Waseem 21

Is the End of the World Approaching?

• Problem complexity 2n

• 64 gold discs

• Given 1 move a second

600,000,000,000 years until the end of the world

M.Hammad Waseem 22

In the Lab

• Towers of Hanoi• Recursive Solution Implementation

M.Hammad Waseem 23