BCA-302 - UNIT-II Notes
BCA-302 - UNIT-II Notes
STACKS
A Stack is linear data structure. A stack is a list of elements in which an element may be
inserted or deleted only at one end, called the top of the stack. Stack principle is LIFO (last
in, first out). Which element inserted last on to the stack that element deleted first from the
stack.
As the items can be added or removed only from the top i.e. the last item to be added to a
stack is the first item to be removed.
Operations on stack:
While performing push and pop operations the following test must be conducted on the
stack.
a) Stack is empty or not b) stack is full or not
1. Push: Push operation is used to add new elements in to the stack. At the time of addition
first check the stack is full or not. If the stack is full it generates an error message "stack
overflow".
2. Pop: Pop operation is used to delete elements from the stack. At the time of deletion first
check the stack is empty or not. If the stack is empty it generates an error message "stack
underflow".
All insertions and deletions take place at the same end, so the last element added to the stack
will be the first element removed from the stack. When a stack is created, the stack base
remains fixed while the stack top changes as elements are added and removed. The most
accessible element is the top and the least accessible element is the bottom of the stack.
Initially top=-1, we can insert an element in to the stack, increment the top value i.e
top=top+1. We can insert an element in to the stack first check the condition is stack is full
or not. i.e top>=size-1. Otherwise add the element in to the stack.
We can insert an element from the stack, decrement the top value i.e top=top-1.
We can delete an element from the stack first check the condition is stack is empty or not.
i.e top==-1. Otherwise remove the element from the stack.
2. display(): This operation performed display the elements in the stack. We display the
element in the stack check the condition is stack is empty or not i.e top==-1.Otherwise
display the list of elements in the stack.
void display() Algorithm: procedure pop():
{ Step 1: START
If(top==-1) Step 2: if top==-1 then
{ Write “Stack is
Printf(“Stack is Underflow”); Underflow” Step 3: otherwise
} 3.1: print “Display elements are”
else 3.2: for top to 0
{ Print
printf(“Display elements are:); ‘stack[i]’ Step 4: END
for(i=top;i>=0;i--)
printf(“%d”,stack[i]);
}
}
Applications of stack:
Infix Notation
Prefix Notation
Postfix Notation
Infix Notation
The infix notation is a convenient way of writing an expression in which each operator is placed
between the operands. Infix expressions can be parenthesized or unparenthesized depending upon
the problem requirement.
Example: A + B, (C - D)
All these expressions are in infix notation because the operator comes between the operands.
Prefix Notation
The prefix notation places the operator before the operands. This notation was introduced by the Polish
mathematician and hence often referred to as polish notation.
All these expressions are in prefix notation because the operator comes before the operands.
Postfix Notation
The postfix notation places the operator after the operands. This notation is just the reverse of Polish
notation and also known as Reverse Polish notation.
All these expressions are in postfix notation because the operator comes after the operands.
Postfix expression is without parenthesis and can be evaluated as two operands and an operator at a time,
this becomes easier for the compiler and the computer to handle.
1. While reading the expression from left to right, push the element in the stack if it is an operand.
2. Pop the two operands from the stack, if the element is an operator and then evaluate it.
3. Push back the result of the evaluation. Repeat it till the end of the expression.
Algorithm
Expression: 456*+
Example 2. Evaluate 5,6,2+*12,4/-
Result=37
1. Infix to postfix
2. Infix to prefix
3. Postfix to prefix
4. Postfix to infix
5. Prefix to postfix
6. Prefix to infix
1. Infix to postfix
Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix
expression Y.
2. Infix to prefix
Iterate the given expression from right to left, one character at a time
If the precedence of scanned operator is equal then check the associativity of the operator. If
associativity left to right then simply put into stack. If associativity right to left then pop the operators
from stack until we find a low precedence operator.
If the scanned character is opening round bracket ( '(' ), push it into operator's stack.
If the scanned character is closing round bracket ( ')' ), pop out operators from operator's stack
until we find an opening bracket ('(' ).
Step 5: Now pop out all the remaining operators from the operator's stack and push into postfix
expression.
Step 6: Reverse the expression in step 5.
Step 7: Exit
Algorithm:
Iterate the given expression from left to right, one character at a time
Algorithm:
Iterate the given expression from right to left, one character at a time
Algorithm:
Iterate the given expression from right to left (in reverse order), one character at a time
A queue is linear data structure and collection of elements. A queue is another special kind of
list, where items are inserted at one end called the rear and deleted at the other end called the
front. The principle of queue is a “FIFO” or “First-in-first-out”.
Queue is an abstract data structure. A queue is a useful data structure in programming. It is
similar to the ticket queue outside a cinema hall, where the first person entering the queue
is the first person who gets the ticket.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters
first, exits first.
More real-world examples can be seen as queues at the ticket windows and bus-stops and our
college library.
The operations for a queue are analogues to those for a stack; the difference is that the
insertions go at the end of the list, rather than the beginning.
Applications of Queue
1. Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not being transferred at the same
rate between two processes) for eg. pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to add and remove the songs from
the play-list.
5. Queues are used in operating systems for handling interrupts.
A queue is an object (an abstract data structure - ADT) that allows the following operations:
Working of Queue
Enqueue Operation
We can easily represent queue by using linear arrays. There are two variables i.e. front and rear, that are
implemented in the case of every queue. Front and rear variables point to the position from where
insertions and deletions are performed in a queue. Initially, the value of front and rear is -1 which
represents an empty queue. Array representation of a queue containing 5 elements along with the
respective values of front and rear, is shown in the following figure.
The above figure shows the queue of characters forming the English word "HELLO". Since, No
deletion is performed in the queue till now, therefore the value of front remains -1 . However, the
value of rear increases by one every time an insertion is performed in the queue. After inserting an
element into the queue shown in the above figure, the queue will look something like following. The
value of rear will become 5 while the value of front remains same.
After deleting an element, the value of front will increase from -1 to 0. however, the queue will look
something like following.
CIRCULAR QUEUE
A circular queue is the extended version of a queue where the last element is connected to the first element.
Thus forming a circle-like structure.
The circular queue solves the major limitation of the normal queue. In a normal queue, after a bit of
insertion and deletion, there will be non-usable empty space.
Here, indexes 0 and 1 can only be used after resetting the queue (deletion of all elements).
This reduces the actual size of the queue.
Circular Queue Operations
1. Enqueue(Insertion) Operation
However, the check for full queue has a new additional case:
9. To push an element into a stack, Top is 9. To insert an element into Queue, Rear is
incremented by one incremented by one.
10. To POP an element from stack,top 10. To delete an element from Queue, Front is
is decremented by one.
incremented by one.
11.The conceptual view of Stack is as
follows: 11.The conceptual view of Queue is as
follows: