Professional Documents
Culture Documents
STACKS
A Stack is a Linear data structure in which an element may be inserted and deleted only at one
end called Top of the stack.
A Stack is called LIFO (Last in First Out) structure because the last element to be added to a stack
is the first element to be removed. The other name of stacks is piles or push down list.
STACK OPERATIONS
IMPLEMENTATION
Meaning :-The act of implementing (providing a practical means for accomplishing something);
Stack Underflow / Empty:- while removing an existing element if there is no element present in the
stack ,this condition is called as Underflow condition.
PUSH(STACK,TOP,MAX,ITEM)
Step 1. [Is their any item in the stack ? Check for Underflow ]
APPLICATION OF STACKS
1.Reversing a String
The stack can be used to reversing the order of execution. Ex. is Reverse the strings of line of
characters.
INFIX NOTATION
In arithmetic expression, when the operator symbol is placed between its operand it is called as INFIX
NOTATION.
E.g. A + B , X- Y , G / H
This notation is named in the name of Polish Mathematician Jan Lukasiewicz , In which operator is
placed before its operand.
e.g. A+ B -> + AB
C–D -> - CD
The advantage of this notation is, arithmetic expressions are evaluated without parenthesis. Due to
absence of parenthesis it becomes easier for the compiler to translate and execute it
In this notation the operator symbol is placed after its operands. Parenthesis are not used to
determine the order of evaluation
e.g. A + B -> AB +
C+D -> CD +
G/H -> GH /
NOTATION CONVERSIONS
Operator Precedence
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression P.
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the stack is empty.
(a). Repeatedly pop from STACK & add to P each operator which has same precedence as or
higher precedence than .
[End if ]
[End if ]
7. Exit
Step 2. Scan P from left to right & repeat steps 3 and 4 for each element of P until the “ ) “ is
encountered.
[End of IF]
6.Exit
This algorithm finds the VALUE of an arithmetic expression P written in prefix notation.
2. Scan P right to left & repeat steps 3 and 4 for each element of P until the “ ( “ is encountered.
Here all the steps are same as evaluation of Postfix notation so write all the points……………
5. Exit
[Do one diagram for stack] and [do one diagram each for array and linked representation of stack and
label it ]
Stack is a LIFO type of data structure in which insertion is done from the top of the stack and the last
added element will be the 1st to be removed from the stack.
Stack always contains a variable called Top which always points to the TOP element of the stack.
When the stack is full so that no element can be added , this is called overflow condition . This overflow
condition will arise when the size of the stack is fixed that is whenever we are implementing static data
structure by using array.
We can apply many operations on stack like push, pop , display, traverse .
The static implementation used array to create stack. It is a simple technique but is not a flexible way of
creating stack as the size is fixed during compilation time. So no increase or decrease of size can be
done.
The dynamic implementation stacks are implemented by Linked list using pointers so we can increase or
decrease the size of the stack.
LINK OR DYNAMIC IMPLEMENTATION OF STACK
Stacks can be implemented by linked list. It uses pointers to link one element to another. The insert and
delete operations can only be performed at the beginning but not any other positions because it is a
LIFO structure.
TOP is the pointer variable which points to 1st element of the list. When any element is added then TOP
points to it. Here no overflow condition is present as dynamic in nature.
struct node
Int info;
step 6. exit
Step 2. temp=Top
Step 5. temp=temp->link
[end of while]
Step 6. Exit
Recursion
A function that calls itself is called a recursive function and this technique is called recursion. A
recursive function will call itself until a final call that does not require a call to itself is made. It
takes advantage of the system stack to temporarily store the calling function’s return address and
local variables.
There are two major cases in any recursive solution. They are
Base case in which the problem is simple enough to be solved directly without the need for any
more calls to the same function
Recursive case
o The problem at hand is initially subdivided into simpler sub-parts.
o The function calls itself again, but this time with sub-parts of the problem obtained in the
first step.
o The final result is obtained by combining the solutions of simpler sub-parts.
here must be a base criteria in which the function doesn’t call itself.
Every time the function is called itself, it must be closer to the base criteria.
Factorial Function
To illustrate recursive functions, consider calculating the factorial of an integer.
That is,
n! = n * (n - 1)!
Then,
5! = 5 * 4!
, where
4! = 4 * 3!
Therefore,
5! = 5 * 4 * 3!
int Fact(int);
int main()
scanf("%d", &num);
val = Fact(num);
//print result
int Fact(int n)
if (n == 1)
return 1;
else
return (n * Fact(n-1));
Output
Factorial of 5 = 120
TYPES OF QUEUE
1.SIMPLE QUEUE
2.CIRCULAR QUEUE
3.DEQUEUE(Double ended queue)
4.PRIORITY QUEUE
Operation Description
enqueue() the process of adding or storing an element to the end of the queue
dequeue() the process of removing or accessing an element from the front of the queue
peek() it is used to get the element at the front of the queue without removing it
1. Array or static
We can implement Queue by using Arrays. Here we know about exact no. of elements to store in a
Queue.
When an element is added in queue its rear is increased by 1 i.e. Rear := Rear +1
Whenever an element is deleted from queue its front is increased by 1. i.e. Front := Front +1
For the first time when we add one element in a queue its front and rear both increased by 1 after that
only rear will increases but not front.
When the queue totally full then it is called Overflow condition. Whereas when there is no element in
[end if]
Step 5. EXIT
[end if]
Step 5. Exit
It is a easy technique to insert or delete element from any position of the list. It takes more memory
space than array representation due to use of one more pointer field. Two pointers Front and Rear to
point 1st and last element respectively.
Step 4. tmp->link=NULL
Step 8. Exit
Step 4. free(tmp)
Step 5. Exit
1. Disadvantage of Simple Queue is when it is implemented using Array is:- when the Rear reaches
at last location after that we can’t insert more elements into the queue though the queue is
empty , if we try to do so it flashes message that Queue is Full .
2. One rule is that we can insert elements at the Rear and Delete from Front so at any position
insertion and deletion in linked implementation of queue is violates the rule.
3. The Limitations of simple queue can be overcome upto some extent in Circular queue.
2.CIRCULAR QUEUE
In Circular Queue the insertion of a new element is done at the very first location of the queue when the
last location is full. So we can say that a circular queue is one in which the first element comes just after
last element.
If we have a Queue of say N elements then after inserting an element last(n-1th) location of the array the
next element will be inserted at the very first location (i.e. location with subscript zero).it is possible only
if the first location is empty.
1.Static 2. Dynamic
A circular queue can be implemented in an array using two variables FRONT and REAR. FRONT and
REAR keeps track of the elements to be deleted and inserted respectively.
Rear=Rear+1
[End of If]
Step 4. Exit.
else
set Front=Front+1
[end if]
Step 4. Exit.
In linked implementation of queue two pointers Front and Rear points to the 1st and last node of the
circular queue.
When circular queue is implemented using linked list it has no fixed size, so we can continue inserting
node at the rear side and the last node contains the address of the first node.
Recall in array representation when REAR and FRONT reaches at last then both reset to 0 i.e. first
position , so this situation will not occur in linked implementation as no overflow condition is present.
INSERT OPERATION
else
Rear->link=ptr
Rear=ptr
Step 4. Exit
DELETE OPERATION
[End if]
Step 4. Front=Front->link
Rear->link=Front
Step 5. Free(tmp)
Step 6. Exit
Advantages
1.A circular queue overcomes the problem of unutilised space in simple queue implemented as array
Disadvantages
1.Here retrieval of data is performed on the basis of FIFO order but in real time systems we need data
retrieval by highest priority tasks first. So this is found in Priority Queue.
3. Priority Queue
In a priority queue, the nodes will have some predefined priority in the priority queue. The node
with the least priority will be the first to be removed from the queue. Insertion takes place in the
order of arrival of the nodes. The applications of priority queue include Dijkstra’s shortest path
algorithm, prim’s algorithm, and data compression techniques like Huffman code.
In a double ended queue, insertion and deletion can take place at both the front and rear ends of
the queue.
APPLICATIONS OF QUEUE
Queue is used when things don’t have to be processed immediately, but have
to be processed in First In First Out order. This property of Queue makes it also
useful in following kind of scenarios.
1) When a resource is shared among multiple consumers.
Examples include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily received at
same rate as sent) between two processes. Examples include IO Buffers, pipes,
file IO, etc.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
5) Variations: ( Deque, Priority Queue, Doubly Ended Priority Queue )
Some other applications of Queue:
Applied as waiting lists for a single shared resource like CPU, Disk, Printer.
Applied as buffers on MP3 players and portable CD players.
Applied on Operating system to handle interruption.
Applied to add song at the end or to play from the front.
******