Professional Documents
Culture Documents
push(): It inserts an element to the top of the stack. It takes O(1) time, as each node is
inserted at the head/top of the linked list.
pop(): It removes an element from the top of the stack. It takes O(1) time, as the top
always points to the newly inserted node.
peek(): It returns the top element of the stack.
size(): It returns the size of the stack, i.e., the total number of items in a stack.
Isfull(S) : to check whether the Stack 'S' is full.
Isempty(S) : to check whether the Stack 'S' is empty.
DIS-ADVANTAGES
A stack can be implemented using arrays. But arrays are of limited size, and the size of the stack
has to be predetermined, whereas, in a linked list, implementation nodes can be added according
to the user's requirements.
A stack is represented using nodes of a linked list. Each node consists of two parts: data and
next(storing the address of the next node). The data part of each node contains the assigned
value, and the next points to the node containing the next item in the stack. The top refers to the
Procedure display()
{
if(top == -1)
{
printf("stack is empty \n");
exit(0);
}
else
{
printf("stack contents are \n");
for(i=top;i>=0;i--)
printf("elements are %d \n",stack[i]);
}
}
• Arithmetic Expressions involving operators and operands can be classified three types
depending on how the operator is placed between operands.
1. Infix notation
2. Prefix notation (polish notation
It refers to the notation in which the operator is placed before its two operands . Here no
parentheses are required, i.e.,
+AB
It refers to the notation in which the operator is placed before its two operands . Here no
parentheses are required, i.e.,
AB+
• Stack organized computers are better suited for post-fix notation than the traditional infix
notation. Thus the infix notation must be converted to the post-fix notation. The
conversion from infix notation to post-fix notation must take into consideration the
operational hierarchy.
• There are 3 levels of precedence for 5 binary operators as given below:
• Highest: Exponentiation (^) Next highest: Multiplication (*) and division (/) Lowest:
Addition (+) and Subtraction (-)
PROBLEM : 5,3,+,6,2,/,*,3,5,*,+
ANS - 39
What is recursion? 2m
Recursion is a powerful programming tool, that allows to execute the function repeatedly
in terms of itself.
or
“Recursion is a process of calling the function repeatedly in terms of itself. The repeated
calling is terminated by the specific condition”.
The objective of the puzzle is to move the entire stack to another rod, obeying the
following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the rods and sliding it onto
another rod, on top of the other disks that may already be present on that rod.
No disk may be placed on top of a smaller disk.
Algorithm Hanoi(n,s,i,d)
// s -> source peg, d->destinations,
//i-> intermediate peg
// input: no of discs n
// output: the sequence of movements.
{
if (n==1)
move disk s to d
else
Hanoi(n-1,s,d,i);
move disk from s to d;
Hanoi(n-1,i,s,d);
}
Move the top N - 1 disks from Src to Aux (using Dst as an intermediary peg)
Move the bottom disk from Src to Dst
Move N - 1 disks from Aux to Dst (using Src as an intermediary peg)
Definition
Array representation of queue
Types of queue: Simple queue
Circular queue
Double ended queue(deque)
Priority queue
Operations on all types of queues
What is a linear queue?
• A Queue is an ordered collection of items.
• Items are deleted at one end called front and inserted at the other end called rear of
the queue.
• The first element inserted into the queue is the first element to be removed. Also called as
FIFO.
Conditions
• REAR = FRONT - 1 will indicate queue is empty
• REAR = FRONT , only 1 element in queue.
• REAR = N -1 , Where N is array size, queue is full
2. CIRCULAR QUEUE
3. PRIORITY QUEUE
• In deque, insertions and deletions can be made at either end but not in the middle.
• Types of Deque:
• Input Restricted Deque: allows the insertions at only one end but allows deletions at
both ends.
• Output Restricted Deque: allows the deletions at only one end but allows insertion at
both ends.
Applications of Queue