You are on page 1of 6

STACKS

A stack is an abstract data type that holds an ordered, linear sequence of items. In
contrast to a queue, a stack is a last in, first out (LIFO) structure. A real-life example
is a stack of plates: you can only take a plate from the top of the stack, and you can
only add a plate to the top of the stack. If you want to reach a plate that is not on the
top of the stack, you need to remove all of the plates that are above that one. In the
same way, in a stack data structure, you can only access the element on the top of
the stack. The element that was added last will be the one to be removed first.
Therefore, to implement a stack, you need to maintain a pointer to the top of the
stack (the last element to be added).

The main stack operations are:

A stack can involve a static or dynamic implementation.

Static array implementation

With a static array, the first element of the array, which is located at position 0, will
be the bottom of the stack. The stack will have a fixed capacity, which means that if
you continuously add items to the stack, it will result in a stack overflow. For this
reason, static implementations must include an is_full() operation to check whether a
stack is at its maximum capacity. Similarly, stack underflow can occur if you try to
remove elements from an empty stack.

As the top of the stack will change every time you add or remove an element, the
index position of the top of the stack needs to be stored in a variable so that the top
can be accessed directly. Initially, the stack is empty, so top will be set to -1.

C# Implementation

public const int MaxSize = 100; // Use a constant


string[] stack = new string[MaxSize];
int top = -1;
Dynamic linked list implementation
A stack can be implemented dynamically using a linked list. In this case, new
elements will be added to the front of the linked list, so the head of the list can be
used as the top pointer. When head is a null pointer (i.e. does not point to anything),
the stack is empty. If the size of the stack needs to be constrained, an additional
variable can be used to track the number of elements on the stack.

Stacks have many uses, for example in checking for balanced parentheses in an
expression, and converting postfix (Reverse Polish Notation) to infix notation and
vice versa. They can be used to maintain a list of operations for an 'undo' function in
a piece of software, where the most recent operation is the first to be undone. Stacks
are also used to facilitate recursive subroutines where the state of each call is stored
in a stack frame and placed on a stack.

Q1.
To evaluate an expression in Reverse Polish notation, you start from the left
hand side of the expression and look at each item until you find an operator (eg
+ or −).

This operator is then applied to the two values immediately preceding it in the
expression. The result obtained from this process replaces the operator and the
two values used to calculate it. This process continues until there is only one
value in the expression, which is the final result of the evaluation.

For example 5 2 7 + + would change to 5 9 + after the first replacement.

Explain how a stack could be used in the process of evaluating an expression in


Reverse Polish notation.

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________
(Total 3 marks)

Q2.

(a) State the principle of operation of a set of data values which behave as a
stack.

___________________________________________________________

___________________________________________________________
(1)
(b) Memory locations 600 to 605 are to be used as a stack area to store
character data, and the first value added to the stack is to be stored at
address 600.

Figure 1 shows the initial empty state of the stack.

Figure 1
600

601

602

603

604

605

(i) Show on Figure 2 the state of the stack after the characters ‘A’, ‘V’,
‘E’, ‘R’ and ‘Y’ join the stack.

Figure 2
600

601

602

603

604

605
(1)

(ii) Two items are removed from the stack. Show on Figure 3 the state
of the stack.

Figure 3
600

601

602

603

604

605
(1)

(iii) Two new characters ‘S’ and ‘P’ join the stack. Show on Figure 4 the
final state of the stack.
Figure 4
600

601

602

603

604

605
(1)

(c) The original items in this stack are to be reversed. This can be done using
a second data structure which uses locations 700 to 705 respectively. The
first item added to the stack was character ‘A’.

Figure 5

600 ‘A’ 700 600

601 ‘V’ 701 601

602 ‘E’ 702 602

603 ‘R’ 703 603

604 ‘Y’ 704 604

605 705 605

Stack Stack
(before the operation) (i) ____________ (after the operation)

(i) Name the second data structure. Label Figure 5.

_______________________________________________________
(1)

(ii) Describe Step 1 in Figure 5.

_______________________________________________________

_______________________________________________________
(1)

(iii) Describe Step 2 in Figure 5.

_______________________________________________________

_______________________________________________________
(1)

(iv) Show on Figure 5 the final contents of all the memory locations.
(2)
(Total 9 marks)

Q3.
A stack may be implemented by using either an array or a linked list.

(a) Give a disadvantage of:

(i) an array implementation;

_______________________________________________________
(1)

(ii) a linked list implementation.

_______________________________________________________
(1)

(b) Under what circumstances would it be more appropriate to use:

(i) an array;

_______________________________________________________
(1)

(ii) a linked list.

_______________________________________________________
(1)
(Total 4 marks)

Q4.

(a) In the context of data structures what is meant by the terms:

(i) FIFO;
_________________________________________________________

(ii) LIFO?
________________________________________________________
(2)

(b) Queue and stack are examples of data structures. Tick in the following
table to indicate whether they are FIFO or LIFO data structures.

FIFO LIFO

Queue

Stack
(2)
(c) Describe one example of the use of a stack.

___________________________________________________________

___________________________________________________________

___________________________________________________________
(2)

(d) Describe one example of the use of a Binary Search Tree.

___________________________________________________________

___________________________________________________________

___________________________________________________________
(2)
(Total 8 marks)

Q5.
Describe how the elements in a non-empty queue are reversed with the aid of a
stack.

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________

________________________________________________________________
(Total 4 marks)

You might also like