TUTORIAL ACTIVITY 3: OPERATION OF STACKS
Learning Outcomes
This Tutorial encompasses activities 3A and 3B.
By the end of this laboratory session, you should be able to:
1. Use arrays to represent stack 2.
Use linked list to represent stack.
3. Implement the operation of stack
Activity 3A
Activity Outcome: Use array and linked list to represent stack of data structure
The stack data structure can be implemented in two ways, which are:
i. Using arrays
ii. Using linked list
1. Show the top of given stack using arrays
the top
[2] 45
[1] 68
[0] 99
Figure 1
2. Convert the stack data structure at Figure 1 to linked list. Then, show top of stack
45 68 99
head
3. Differentiate implementation of stack using linked list and array
Array Linked List
An array contains only one The linked list is comprised of nodes
Top of
field which stores data consisting of two fields: data and address
stack
element field
Size Size of the array is fixed Size of linked list is not fixed
Element Inserting new element at the
Insertion front is potentially expensive Inserting a new element at any position can
because existing element be carried out easily
need to be shifted over to
make room
Test full Memory allocation is done at Memory allocation is done at run time
stack compile time
Activity 3B
Activity Outcome: Array implementation operation of stack
1. Implement the operation of Stack by using Array
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 )
b) Delete item from the stack as below:
i.Pop (500, &T)
ii. Pop (600, &T)
Step 4: Draw the final picture after all of the operations has been performed.
[2] push [2] push [2] push [2] 500
(700) (600) (500)
[1] [1] [1] 600 [1] 600
[0] [0] 700 [0] 700 [0] 700
T T T T
pop ()
[2] [2]
pop ()
[1] [1] 600
[0] 700 [0] 700
T T
2. 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
[5] [5]
[5] [5]
[4] [4]
[4] [4]
push (3) push (5) [3] [3]
[3] [3] push (7)
[2] [2] 7
[2] [2]
[1] [1] 5
[1] [1] 5
[0] [0] 3
[0] [0] 3 3
s1 s1 s1 s1
push (9)
[5] [5] [5]
[5] 13
[4] 11 11 [4] 11 [4]
[4] 11 Stack is full
[3] 9 pop () push (13) [3] [3] 9
[3] 99 9 push (11)
[2] 7 [2] [2] 7
[2] 7 7
[1] 5 7 [1] [1] 5
[1] 5 5
5 3
[0] 3 [0] 3 [0]
[0] 3
s1 s1 s1 s1
pop ()
[5] [5]
[4] [4] 11
15
[3] 9 push (15) [3] 99
[2] 7 [2] 7
[1] 5 7
[1] 5
5
[0] 3 [0] 3
s1 s1
3. Imagine we have two empty stacks of integers, t1 and t2. Draw a picture of each stack
after the following operations being performed:
Create new stack (t1)
Create new stack (t2)
pushStack (t1, 2)
pushStack (t1, 6)
pushStack (t1, 8)
pushStack (t1, 9)
pushStack (t1, 10)
pushStack (t1, 12)
loop no emptyStack
(s1)
i. popStack (t1,x)
ii. popStack (t1, x)
iii. pushStack(t2,15
iv. pushStack (t2, 5)
v. pushStack (t2, 1)
vi. popStack (t2, x)
end loop
[5] [5] [5] [5] [5] [5]
[4] [4] [4] [4] [4] [4]
push (2), t1 push (6), t1
[3] [3] [3] [3] [3] [3]
[2] [2] [2] [2] [2] [2]
[1] [1] [1] [1] [1] 6 [1]
[0] [0] [0] 2 [0] [0] 2 [0]
t1 t2 t1 t2 t1 t2
push (8), t1
[5] [5] [5] [5] [5] [5]
[4] 10 [4] [4] [4] [4] [4]
push (10), t1 push (9), t1
[3] 9 [3] [3] 9 [3] [3] [3]
[2] 8 [2] [2] 8 [2] [2] 8 [2]
[1] 6 [1] [1] 6 [1] [1] 6 [1]
[0] 2 [0] [0] 2 [0] [0] 2 [0]
t1 t2 t1 t2 t1 t2
stack is full
push (12), t1
[5] 12 [5] [5] [5] [5] [5]
[4] 10 [4] [4] 10 [4] [4] [4]
pop () pop ()
[3] 9 [3] [3] 9 [3] [3] 9 [3]
[2] 8 [2] [2] 8 [2] [2] 8 [2]
[1] 6 [1] [1] 6 [1] [1] 6 [1]
[0] 2 [0] [0] 2 [0] [0] 2 [0]
t1 t2 t2 t1 t2
push (15), t2
[5] [5] [5] [5] [5] [5]
[4] [4] [4] [4] [4] [4]
push (1), t2 push (5), t2
[3] [3] [3] [3] [3] [3]
[2] [2] 1 [2] [2] [2] [2]
[1] [1] 5 [1] [1] 5 [1] [1]
[0] [0] 15 [0] [0] 15 [0] [0] 15
t1 t2 t1 t2 t1 t2
pop (), t2
[5] [5]
[4] [4]
[3] [3]
[2] [2]
[1] [1] 5
[0] [0] 15
t1 t2