You are on page 1of 7

Exercise 3: STACKS

1. A stack is a data structure that works on the principle of Last in First out (LIFO). List and
draw 3 examples implementation of stack in real life situation.

Stack of
Cookies
Tennis Ball

Stack of Paper
2. List and describe 5 operations in a stack:

Operations name Description

i. PUSH

Add data into stack

ii. POP

Delete data from stack

iii. PEEK

Allow to see top data in stack

iv. IsFull

Allow to check if stack is full or not

v.IsEmpty

Allow to check if stack is empty or not


3. Show the top of given stack using arrays

[2] 45
[1] 68
[0] 99

Figure 1

Answer = 45 [2]

4. Convert the stack data structure at Figure 1 to linked list. Then, show top of stack

2 45 1 68 0 99

Head node 2 1 0

5. Differentiate implementation of stack using linked list and array

Array Linked List

Top of bottom top


stack

Size fixed dynamic

Element Random access Sequential access


Insertion

Test full
stack IsEmpty IsFull
6. Draw a suitable stack diagram for each statement below. Given that the Stack of T
contents of four (4) elements.

Procedure:

Step 1: Create new stack (T)


Step 2: Check stack : full or empty
Step 3: If empty;

a) Insert item to the stack as below:


i. Push (700, &T)
ii. Push (600, &T)
iii. Push (500, &T )

500

Push Push 600 Push 600


700 600 500
700 700 700

Stack T

b) Delete item from the stack as below:


i. Pop (500, &T)
ii. Pop (600, &T)

500
Pop 500 Pop 600
600 600

700 700 700

Stack T
Step 4: Draw the final picture after all of the operations has been performed.

7. Imagine we have one empty stack of integers, s1. Draw a picture of each stack after the
following operations being performed:

Step 1: Create new stack (s1)


Step 2: pushStack (s1, 3)
Step 3: pushStack (s1, 5)
Step 4: pushStack (s1, 7)
Step 5: pushStack (s1, 9)
Step 6: pushStack (s1, 11)
Step 7: pushStack (s1, 13)
Step 8: loop no emptyStack (s1)
i. popStack
ii. popStack
iii. pushStack (s1, 15)
Step 9: end loop
9

7 7
Push
Push Push 5 Push 5 9 5
3 5 7
3 3 3
3

13

15 11 11 11 Push
Push
15 Pop pop13 Push 11
9 9 9 9 9
11 13
7 7 7 7 7

5 5 5 5 5

3 3 3 3 3

Stack s1
8. Imagine we have two empty stacks of integers, t1 and t2. Draw a picture of each stack
after the following operations being performed:

pushStack (t1, 2)
pushStack (t1, 6)
pushStack (t1, 8)
pushStack (t1, 9)
pushStack (t1, 10)
pushStack (t1, 12)
popStack (t1, x)
popStack (t1, x)
pushStack (t2, 15)
pushStack (t2, 5)
pushStack (t2, 1)
popStack (t2, x)

Push Push 8 Push 8


Push
6 8 9
2
6 6 6

2 2 2 2

12

10 10 10

9 9 Pop 9 9
Pop Push Push
10 12
8 8 8 12 8 10

6 6 6 6

2 2 2 2
Stack t1

1
Push Push Push Pop 1
5 5 5 5
15 1
15 15 15 15

Stack t2

You might also like