Professional Documents
Culture Documents
At all times, we maintain a pointer to the last pushed data on the stack. As this
pointer always represents the top of the stack, hence named top.
The top pointer provides top value of the stack without actually removing it.
Linked List Implementation of Stack
The stack implemented using a linked list can work for a large number of
values.
That means, stack implemented using the linked list works for the variable
size of data.
So, there is no need to fix the size, at the beginning of the implementation.
Applications of Stack
Data structure has some important applications in different aspects. These are
Like:-
a) Evaluation of Algebraic Expression
b) Infix and Post fix (RPN)
c) Function calls
a) Evaluation of Algebraic Expression
Stack data structure is used for evaluating the given expression. For example,
consider the following expression
5 * ( 6 + 2 ) - 12 / 4
Since parenthesis has the highest precedence among the arithmetic
operators, ( 6 +2 ) = 8 will be evaluated first. Now, the expression becomes
5 * 8 - 12 / 4
* and / have equal precedence and their associativity is from left-to-right. So,
start evaluating the expression from left-to-right.
5 * 8 = 40 and 12 / 4 = 3
Now, the expression becomes
40 - 3
And the value returned after the subtraction operation is 37.
This means that after the end of evaluation 37 is stored on the stack.
b) Infix and Post fix (RPN)
Infix notation: The expression of the form a op b. When an operator is in-
between every pair of operands. Example: 2+3, 4-1, (3 + 4) × 5 – 6, etc.
Postfix notation: The expression of the form a b op. When an operator is
followed for every pair of operands. Example: 2 3+, 4 2 *, etc.
Infix and Postfix reverse-polish notation (RPN):
Normally, mathematics is written using what we call in-fix notation:
(3 + 4) × 5 – 6
The operator is placed between to operands.
One weakness: parentheses are required
(3 + 4) × 5 – 6 = 29
3+4 × 5–6 = 17
3 + 4 × (5 – 6) = –1
(3 + 4) × (5 – 6) = –7
RPN conti…..
Examples of postfix:
3 4 5 × + 6 –
3 20 + 6 –
23 6 –
17
3 4 5 6 – × +
3 4 –1 × +
3 –4 +
–1
RPN conti…..
Benefits:
– No ambiguity and no brackets are required
– It is the same process used by a computer to perform
computations:
• operands must be loaded into registers before operations can
be performed on them.
– Reverse-Polish can be processed using stacks.
RPN conti…..
1
RPN conti…..
178
RPN conti…..
– Some clients may have to wait while the servers are busy.
– Those clients are placed in a queue and serviced in the order of arrival.
• Bank and airport security use queues
• The SSH (Secure Shell) and SFTP (Secure file transfer protocol) are clients
Requirements:
– All queue operations must run in O(1) time
Priority Queue,
A priority queue is a special type of queue in which each element is
associated with a priority value.
And, elements are served on the basis of their priority. That is, higher
priority elements are served first.
However, if elements with the same priority occur, they are served
according to their order in the queue.
Assigning Priority Value
Generally, the value of the element itself is considered for assigning the
priority.
For example,
The element with the highest value is considered the highest
priority element.
However, in other cases, we can assume the element with the
lowest value as the highest priority element.
We can also set priorities according to our needs.
Conti…