Professional Documents
Culture Documents
DS Mod2
DS Mod2
DS Mod2
Mrs.JIMSHA K MATHEW
Assistant Professor
Department of ISE
Acharya Institute Of Technology
C top
B top B B top
A A A A top
A top
Stack Operations
Top is incremented by 1
Function for push() operation:
Top is decremented by 1
Function for pop() operation
if(top==-1)
printf("Stack Underflow:");
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
printf(“Stack is full\n”);
}
else
{
printf(“Stack is not full\n”);
}
}
function to check if the stack is empty
printf(“Stack is empty\n”);
}
else
{
printf(“Stack is not empty\n”);
}
}
Function for display the elements in stack
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
Stack can be implemented in two ways
1. Using Arrays
2. Using Linked list
ARRAY REPRESENTATION OF STACK
Top=-1;
Push(x)
{
Top++;
Stack[top]=x;
}
Array representation of stack
• Stack can be represented using a linear array.
• There is a pointer called TOP to indicate the top of the stack
0 1 2 3 4 5
aaa bbb ccc ddd
top 3
• Overflow: If we try to insert a new element in the stack top(push) which is
already full, then the situation is called stack overflow
• Underflow: If we try to delete an element(pop) from an empty stack, the
situation is called stack underflow
Implement Stack by using dynamic arrays
• When a stack is implemented by using static arrays the size of the
stack is bound to MAXSIZE(i.e maximum elements a stack can
have)but some time stack need be dynamic.
X=A * (B + C * D) + E
(
A ( A Operand –add it to expression
* (* A Operator-no other operator on stack .push to stack
( (*( A push to stack
B (*( AB Operand –add it to expression
+ (*(+ AB Operator-no other operator on stack .push to stack
C (*(+ ABC Operand –add it to expression
* (*(+* ABC Operator-priority of +on stack <*. Then push * to stack
D (*(+* ABCD Operand –add it to expression
) (* ABCD*+ Pop from Stack until a left parenthesis is encountered.and add to
expression
+ (+ ABCD*+* Pop * . Add to exp. push +(priority*>priority+)
E ABCD*+*E Operand –add it to expression
Problem 1
Convert the following infix expression to postfix
and prefix
((6+(3–2)*4)^5+7)
T4 7 +
Postfix Conversion T3 5 ^ 7 +
( ( 6 + ( 3 – 2 ) * 4 ) ^ 5 + 7 ) // T1 = 3 2 6 T2 + 5 ^ 7 +
- 6 T1 4 * + 5 ^ 7 +
( ( 6 + T1 * 4 ) ^ 5 + 7 ) // T2 = T1 4 *
632-4*+5^7+
( ( 6 + T2 ) ^ 5 + 7 ) // T3 = 6 T2 + (Postfix Expression)
( T3 ^ 5 + 7 ) // T4 = T3 5 ^
( T4 + 7 ) // T5 = T4 7 +
T5
a + b ^ T1 // T2 = ^ b T1
a + T2 // T3 = + a T2
T3
+ a T2
+ a ^ b T1
+a^b^cd (Prefix Expression)
632–5*+ 3–2=1
615*+ 1*5=5
65+ 6 + 5 = 11
Result:18
void recursion()
{
recursion(); /* function calls itself */
}
void main()
{
recursion();
}
Recursive functions are very useful to solve many mathematical problems, such
Analysis of recursion-One may argue that why to use recursion as the same
task can be done with iteration. The first reason is recursion makes a program
more readable and because of today's enhance CPU systems, recursion is more
efficient than iterations.
Department of ISE Acharya Institute of Technology
FACTORIAL NUMBER
The product of the positive integers from 1 to n, inclusive is called n factorial
and is usually denoted by n!
n!=1*2*3*4*………*(n-2)*(n-
1)*n. Factorial function may be
defined as
a. if n=0 then n!=1
b. if n>0, then n!=n*(n-1)!
• The initial state of the puzzle is, when all the disks in the ascending
order that is the smallest disk being on top is stacked on the first pole.
Rules
The mission is to move all the disks to some another tower without violating
the sequence of arrangement.
The below mentioned are few rules which are to be followed for tower of
hanoi −
•Only one disk can be moved among the towers at any given time.
with n disks can be solved in minimum 2n−1 steps. This presentation shows that
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) //Move n-1 disks from source to aux
move disk from source to dest //Move last disk from source to dest
Hanoi(disk - 1, aux, dest, source) // Move n-1 disks from aux to dest
END Procedure
STOP
Department of ISE Acharya Institute of Technology
PROGRAM:
#include<stdio.h>
#include<conio.h>
• A new element is added at one end called rear end and the existing elements are
deleted from the other end called front end.
front rear
Elements of queue:-
Front: -
This end is used for deleting an element from a queue. Initially front end is set to -1.
Front end is incremented by one when a new element has to be deleted from
queue.
Rear: -
This end is used for inserting an element in a queue. Initially rear end is set to -1.
rear end is incremented by one when a new element has to be inserted in queue.
The various types of queues are
•Circular queue
•Priority queue
• Inserting an element in a queue which is already full is known as Queue Full condition
(Rear = Max-1), position starts from 0
• When the queue is fully occupied and enqueue() operation is called queue overflow occurs.
isfull()
To check whether the queue is full or not
bool isfull()
{
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
Queue Empty(Underflow) Condition
Queue Empty:
Deleting an element from queue which is already empty is known as Queue Empty condition
Size of queue = 4
Front = Rear = -1
Basic Operations
isempty()
To check whether the queue is empty or not
bool isempty()
{
if( front > rear||front<0)
return true;
else
return false;
}
Enqueue() operation
void enqueue(int queue[ ],int ele) else
{ {
if(rear==-1)
rear++;
{
queue[rear]=ele;
front=rear=0;
queue[rear]=ele; } printf("\nItem inserted..");
} }
else if(rear==MAX-1)
{
printf("\nQUEUE is full.\n");
}
Basic Operations
Dequeue() operation
void dequeue(int queue[ ])
{
int ele;
if(front<0||front>rear)
{
printf("QUEUE is Empty.");
}
else if(front==rear) //only one element remains
{
ele=queue[front];
front=rear= -1;
}
else
{ ele=queue[front];
front++;
} }
Display contents of Queue:
void display(int f, int r, int q[])
{
int i;
if (f > r||f<0)
{
printf("queue is empty\n");
}
else
{
printf("the contents of the queue are\n");
for(i=f; i<=r; i++)
{
printf("%d\n",q[i]);
} Department of ISE Acharya Institute of Technology
}
Distinguish between stack and queue
Sr STACK QUEUE
.N
o
Insertion and deletion take place at only Insertion takes place at rear and deletion takes place at
2
one end called top front.
Plate counter at marriage reception is an Student standing in a line at fee counter is an example
6
example of stack of queue.
Representation Of Queues
1.Using an array
2.Using linked list
Array representation of Queue
Queue can be represented using a linear array.
There are two pointers called FRONT & REAR to indicate the two
ends of the queue
0 1 2 3 4 5
aaa bbb ccc ddd FRONT=0
REAR=3
Here the maximum size(MAXSIZE) of the array is :6
Insertion takes place at REAR and deletion at FRONT
0 1 2 3 4
A,B,C are inserted FRONT=0 A B C
REAR=2
0 1 2 3 4
A deleted FRONT=1 B C
REAR=2
0 1 2 3 4
D & E inserted FRONT=1 B C D E
REAR=4
(Now Q is full !!, REAR=Maxsize-1)
Here, even if there is free space, it shows Queue full.
0 1 2 3 4
B, & C deleted FRONT=2 D E
REAR=4
3 free spaces. Still we cant enter new elements in to the queue
Disadvantages of linear queue
#define MAX 10
if(rear==-1)
front=rear=0;
queue[rear]=ele;
else if(rear==MAX-1)
printf("\nQUEUE is full.\n");
return;
}
else
{
rear++;
queue[rear]=ele;
}
printf("\nItem inserted..");
}
void display_Q(int queue[])
{
int i;
if(rear==-1)
{
printf("\nQUEUE is Empty.");
return;
}
for(i=front;i<=rear;i++)
{
printf("%d,",queue[i]);
}
void remove_from_Q(int queue[])
{
int ele;
if(front==-1)
{
printf("QUEUE is Empty.");
return;
}
else if(front==rear)
{
ele=queue[front];
front=rear=-1;
}
int main()
{
int ele,choice;
while(1)
{
printf("\n\nEnter choice (1:Insert,2:Display,3:Remove,0:Exit):");
scanf("%d",&choice);
switch(choice)
{
case 0:
exit(1);
break;
case 1:
printf("Enter an element to insert:");
scanf("%d",&ele);
insert_in_Q(QUEUE,ele);
break;
case 2:
display_Q(QUEUE);
break;
case 3:
remove_from_Q(QUEUE);
break;
default:
printf("\nInvalid choice\n");
break;
}
} //end of while(1)
return 0;
}
Circular Queue
• In circular queue last element of the array is logically followed by
the first element of the array (element with index 0). It has a
structure as shown below
q[0]
q[3]
q[2] q[1]
// Initially r = -1 and f = 0
r q[3]
40 10 q[0]
30 20
f q[2] q[1]
Overflow()-
((rear == MAXSIZE-1 && front == 0) || (front=rear+1)).
Underflow()-
If this approach is used, to check for queue overflow and underflow, we use a
variable called as ‘count’. Count contains the number of items or elements present
in the queue at any instant. Therefore, count = 0 means the queue is empty and
count = queuesize means the queue is full.
•Delete we follow
item = q[f]
f = ( f + 1 ) % queuesize
count = count – 1
Department of ISE Acharya Institute of Technology
Function to insert element: provided f=0,r=-1,count=0;
void insert_rear(int item, int r, int count, int q[ ])
{
if(count = = queuesize)
{
printf("queue overflow\n");
}
else
{
rear = (rear+1) % queuesize;
q[rear] = item;
count = count + 1;
}
Department of ISE Acharya Institute of Technology
Function to delete element:
void delete_front(int front, int count, int q[ ])
{
int item;
if(count = = 0)
{
printf("queue is empty\n");
}
Else
{
item = q[front];
printf("\nthe item deleted is %d",item);
front = (front+1) % queuesize;
count = count - 1;
Department of ISE Acharya Institute of Technology
} }
Function to display elements:
}
Write a C Program to implement Circular
queue//refer notes
Circular Queues using Dynamic Arrays
(with the concept of array doubling)
• For example consider a circular queue with queue size 5, after inserting 5
elements the structure is as shown below.
Now insert 3 elements into the queue we get the following structure.
The idea is similar to the multiple stacks here we need to use three extra arrays. In
stack post, we needed two extra arrays, one more array is required because in queues,
enqueue() and dequeue() operations are done at different ends.
Following are the three extra arrays are used:
1) front[]: This is of size k and stores indexes of front elements in all queues.
2) rear[]: This is of size k and stores indexes of rear elements in all queues.
int ele;
if(front<0||front>rear)
{
printf("QUEUE is Empty.");
}
else if(front==rear) //only one element remains
{
ele=queue[front];
front=rear= -1;
}
else
{ ele=queue[front];
front++;
} }
The operations that can be performed on dequeue are:
• An ascending order priority queue gives the highest priority to the lower
number in that queue.
• For example, you have six numbers in the priority queue that are 4, 8, 12,
45, 35, 20. Firstly, you will arrange these numbers in ascending order.
• The new list is as follows: 4, 8, 12, 20. 35, 45. In this list, 4 is the smallest
number.
• Hence, the ascending order priority queue treats number 4 as the highest
priority.
Descending order priority queue
• A descending order priority queue gives the highest priority to
the highest number in that queue.
• For example, you have six numbers in the priority queue that
are 4, 8, 12, 45, 35, 20.
• Firstly, you will arrange these numbers in ascending order.
• The new list is as follows: 45, 35, 20, 12, 8, 4. In this list, 45 is
the highest number.
• Hence, the descending order priority queue treats number 45 as
the highest priority.
There are various methods of implementing priority queues using arrays.
Implementation is as follows:
□Insert the items in any order (structure).
□ get Items and the priority values from the user.
• Before deletion sorting (Ascending)to be done(any sorting algorithm)
□so,the items with least value can be considered as the items with highest
priority and items with highest value can be considered as the items with
least priority.
□To implement priority queue,we insert the elements into queue in such
a way that they are always ordered in increasing order.
□with this technique the highest priority elements are at the front end of the
queue and lowest priority elements are at the rear end of the queue.
Department of ISE Acharya Institute of Technology
Priority Queue(cont.)
struct priority
{
int prior;
int data;
}pq[10],temp;
1. ENQUEUE() OPERATION:
void insert()
{
pq[rear].data=key;
if(rear==size-1)
pq[rear].prior=p;
{
}
printf("Queue full, insertion not possible\n");
}
else if(front==-1) else {
{ printf("enter the item and priority\n");
front=front+1; scanf("%d %d",&key,&p);
printf("enter the item and priority\n"); rear=rear+1;
scanf("%d %d",&key,&p); pq[rear].data=key;
rear=rear+1;
pq[rear].prior=p; }}
2. Sorting : //Before deletion sorting to be done
for(i=front;i<=rear;i++)
{
for(j=i+1;j<=rear;j++)
{
if(pq[i].prior>pq[j].prior)
{
temp=pq[i];
pq[i]=pq[j];
pq[j]=temp;
} }}
3. Dequeue()
{ {
if(front==-1) { key=pq[front].data;
front=front+1;
printf("Queue Empty\n");
printf("Deleted item is %d\n",key);
}
}}
else if (front==rear)
//after sorting in ascending order,the highest
{ priority element will be in front
key=pq[front].data;
front=rear= -1;
}
PRIORITY QUEUE OPERATION VIDEO
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology
Department of ISE Acharya Institute of Technology