Stacks and Queues - 3
Stacks and Queues - 3
PUSH OPERATION
The process of putting a new data element onto stack is known as a Push Operation.
Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
POP OPERATION
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success
data ← stack[top]
top ← top - 1
return data
end procedure
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
LIST ADT
List is basically the collection of elements arranged in a sequential manner. In memory we can
store the list in two ways: one way is we can store the elements in sequential memory locations.
That means we can store the list in arrays. The other way is we can use pointers or links to associate
elements sequentially. This is known as linked list.
LINKED LISTS:
The linked list is very different type of collection from an array. Using such lists, we can store
collections of information limited only by the total amount of memory that the OS will allow us to
use. Further more, there is no need to specify our needs in advance. The linked list is very flexible
dynamic data structure: items may be added to it or deleted from it at will.
A programmer need not worry about how many items a program will have to accommodate in
advance. This allows us to write robust programs which require much less maintenance.
The linked allocation has the following draw backs:
1.No direct access to a particular element.
2.Additional memory required for pointers.
Linked list are of 3 types:
1.Singly Linked List
2.Doubly Linked List
3.Circularly Linked List
Applications of Stack
• Evaluating Arithmetic Expression: Stack is used to convert an infix expression into
postfix/prefix form. Stack is used to evaluate a postfix expression.
• Balancing the symbols (Parenthesis): Stack is used to evaluate a postfix expression.
• In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
• Function Calls: During a function call the return address and arguments are pushed onto a
stack and on return they are popped off
Algorithm
Suppose P is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression Q. Besides operands and operators, P (infix notation) may also
contain left and right parentheses. We assume that the operators in P consists of only
exponential ( ^ ), multiplication ( * ), division ( / ), addition ( + ) and subtraction ( - ). The
algorithm uses a stack to temporarily hold the operators and left parentheses. The postfix
expression Q will be constructed from left to right using the operands from P and operators,
which are removed from stack. We begin by pushing a left parenthesis onto stack and adding
a right parenthesis at the end of P. the algorithm is completed when the stack is empty.
1. Push “(” onto stack, and add “)” to the end of P.
2. Scan P from left to right and repeat Steps 3 to 6 for each element of P until the stack is
empty.
3. If an operand is encountered, add it to Q.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator ⊗ is encountered, then:
(a) Repeatedly pop from stack and add P each operator (on the top of stack), which has the
same precedence as, or higher precedence than ⊗.
(b) Add ⊗ to stack.
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack and add to P (on the top of stack until a left parenthesis is
encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
7. Exit.
Note. Special character ⊗ is used to symbolize any operator in P.