You are on page 1of 3

To_Stack (PUSH)

Case 1:
no. of elements used in array = 0

First Note:
Is the no. of elements used in array greater or equal to 1?

1 2 3 4 5 6 7 8

No (0) – Skip Loop

Store New element at the TOP

Element 1
1(Top) 2 3 4 5 6 7 8

Increment no. of elements used in array by 1: 0 + 1 = 1

no. of elements used = 1

Second Note:
Is the no. of elements used in array greater or equal to 1?

Element 1
1 2 3 4 5 6 7 8

Yes (1) – Run Loop

Loop: Move each element in array to next place

Element 1
1 2 3 4 5 6 7 8

Store New element at the TOP

Element 2 Element 1
1 (Top) 2 3 4 5 6 7 8

Increment no. of elements used in array by 1: 1 + 1 = 2

no. of elements used = 2

Third Note:
Is the no. of elements used in array greater or equal to 1?

Element 2 Element 1
1 2 3 4 5 6 7 8

Yes (2) – Run loop

Loop: Move each element in array to next place

Element 2 Element 1
1 2 3 4 5 6 7 8

Store New element at the TOP

Element 3 Element 2 Element 1


1 (Top) 2 3 4 5 6 7 8
Increment no. of elements used in array by 1: 2 + 1 = 3

no. of elements used = 3

9th Note:
Is the no. of elements used in array greater or equal to 1?

Element 8 Element 7 Element 6 Element 5 Element 4 Element 3 Element 2 Element 1


1 2 3 4 5 6 7 8

Yes (8) – Run Loop

Loop: Move each element in array to next place

Element 8 Element 7 Element 6 Element 5 Element 4 Element 3 Element 2


1 2 3 4 5 6 7 8

Store New element at the TOP

Element 10 Element 9 Element 8 Element 7 Element 6 Element 5 Element 4 Element 2


1 (Top) 2 3 4 5 6 7 8

Increment no. of elements used in array by 1: 8 + 1 = 9

no. of elements used = 9


From_Stack (POP)

You might also like