Professional Documents
Culture Documents
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).
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
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.
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
(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
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
Stack Stack
(before the operation) (i) ____________ (after the operation)
_______________________________________________________
(1)
_______________________________________________________
_______________________________________________________
(1)
_______________________________________________________
_______________________________________________________
(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.
_______________________________________________________
(1)
_______________________________________________________
(1)
(i) an array;
_______________________________________________________
(1)
_______________________________________________________
(1)
(Total 4 marks)
Q4.
(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)
___________________________________________________________
___________________________________________________________
___________________________________________________________
(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)