Module 7 - Basic ADTS (Stack Data Structures)
Module 7 - Basic ADTS (Stack Data Structures)
7.1 Stack
A Stack is an Abstract Data Type (ADT), commonly used in most programming
languages. It is named stack as it behaves like a real-world stack such as a
deck of cards or a pile of plates, etc. Other names for stacks are Piles and
Push-down lists
A Bounded stack is a stack has a fixed maximum capacity. If the stack is full
and does not contain enough space to accept an entity to be pushed, the stack
is then considered to be in an overflow state.
Let consider a linear array STACK, a variable TOP which contain the location
of the top element of the stack; and a variable STACKSIZE which gives the
maximum number of elements that can be hold by the stack. The condition
TOP = 0 or TOP = Null will indicate that the stack is empty.
The differences between a Stack and an Array are shown in the table below:
▪ Infix Notation
▪ Prefix (Polish) Notation
▪ Postfix (Reverse-Polish) Notation
These notations are named the way the operator is used in an expression. To
evaluate a complex infix expression, a compiler would first convert the
expression to postfix notation, and then evaluate the postfix version of the
expression.
Associativity describes the rule where operators with the same precedence
appear in an expression. For example, in expression a + b − c, both + and –
have the same precedence, then which part of the expression will be evaluated
first, is determined by associativity of those operators. Here, both + and − are
left associative, so the expression will be evaluated as (a + b) – c. Precedence
and associativity determines the order of evaluation of an expression.
We use the following three levels of precedence and associativity for the five
binary operations.
a–b+c
where operators are used in-between operands. With this notation, we must
distinguish between (A+B) *C and A+(B*C) by using either parentheses or some
Operator-precedence convention such as the usual precedence levels
discussed above. Accordingly, the order of the operators and operands in an
arithmetic expression does not uniquely determine the order in which the
operations are to be performed. It is easy for us humans to read, write, and
speak in infix notation but the same does not go well with computing devices.
An algorithm to process infix notation could be difficult and costly in terms of
time and space consumption.
8 + 5 * 4 - 12 / 6
The following Infix Notations are translated into Polish notations using
brackets [] to indicate a partial translation:
The fundamental property of Polish notation is that the order in which the
operations are to be performed is completely determined by the positions of
the operators and operands in the expression. Accordingly, one never needs
parentheses when writing expressions in Polish notation.
Again, one never and parentheses to determine the order of the operations in
any arithmetic expression written in reverse Polish notation. This notation is
frequently called Postfix (or Suffix) notation.
Solution
Now add “$” at the end of expression as a sentinel.
P: 5. 6. 2, +, *,12, 4, /, - $
Note that parentheses are necessary for the infix expression Q but not for the
postfix expression P.
The elements of P have been labelled from left to right for easy reference. The
table above shows the contents of the Stack as each element of P is scanned.
The following algorithm transform the infix expression Q into its equivalent
postfix expression P. It uses a stack to temporary hold the operators and left
parenthesis. The postfix expression will be constructed from left to right using
operands from Q and operators popped from STACK.
Solution
Now add “)” at the end of expression A + (B * C – (D / E ^ F) * G) * H) and also
Push a “(“ on Stack.
(f) Browsers: The back button in a browser saves all the URLs that have
been visited previously in a stack. Each time a new page is visited, it is
added on top of the stack. When the back button is pressed, the current
URL is removed from the stack, and the previous URL is accessed.
(g) Syntax Parsing: Since many programming languages are context-free
languages, the stack is used for syntax parsing by many compilers.
(h) Memory Management: Memory management is an essential feature of
the operating system, so the stack is heavily used to manage memory.