NAME: Nitesh Patel Class: Se Comps B' Roll No: 49 Practical 1 Implementation of Stack Using Array
NAME: Nitesh Patel Class: Se Comps B' Roll No: 49 Practical 1 Implementation of Stack Using Array
#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#define MAX 3
int main() {
int val,
option;
do
{
printf("\n *****MAIN
MENU*****"); printf("\n 1.
PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option:
"); scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st,
val);
break;
case 2:
val = pop(st); if(val != -1)
printf("\n The value deleted from stack is: %d",
val); break;
case 3:
val = peek(st);
if(val != -1)
printf("\n The value stored at top of stack is: %d", val);
break;
case 4:
displa
y(st);
brea
k;
}
}while(option != 5);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top]
= val;
}
}
int pop(int st[])
{
int val;
if (top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
else
{
val =
st[top]
; top--;
return val;
}
}
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
{
if(top == -1)
{
printf("\n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 2
#include
<stdio.h>
#include
<conio.h>
#define MAX
10 int
queue[MAX]
;
int front = -1, rear = -
1; void insert(void);
int
delete_element(voi
d); int peek(void);
void
display(void);
int main()
{
int
option,
val; do
{
printf(“\n\n ***** MAIN MENU
*****”); printf(“\n 1. Insert an
element”);
printf(“\n 2. Delete an
element”); printf(“\n 3.
Peek”);
printf(“\n 4. Display the
queue”); printf(“\n 5.
EXIT”);
printf(“\n Enter your option : “);
scanf(“%d”, &option);
switch(option)
{
case 1: insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf(“\n The number deleted is : %d”,
val); break;
case 3:
val = peek();
if (val != -1)
printf(“\n The first value in queue is : %d”,
val); break;
case 4:
disp
lay
();
br
ea
k;
}
}while(option
!= 5);
getch();
return 0;
}
void insert()
{
int num;
printf(“\n Enter the number to be inserted in the
queue : “); scanf(“%d”, &num);
if(rear == MAX-1)
printf(“\n OVERFLOW”);
else if(front == -1 && rear ==
-1) front = rear = 0;
el
s
e
r
e
a
r
+
+
;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf(“\n
UNDERFLOW”);
return -1;
}
else
{
val =
queue[front]
; front++;
if(front >
rear) front =
rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf(“\n QUEUE IS
EMPTY”); return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf(“\n”);
if(front == -1 || front > rear)
printf(“\n QUEUE IS
EMPTY”);
else
{
for (i = front;i <= rear;i++) printf(“\t %d”, queue[i]);
}
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 3
#include
<stdio.h>
#include
<conio.h>
#include
<ctype.h>
#include
<string.h>
#define MAX 100
char
st[MAX];
int top=–
1;
void push(char st[],
char); char pop(char
st[]);
void InfixtoPostfix(char source[], char
target[]); int getPriority(char);
int main()
{
char infix[100], postfix[100];
clrscr();
printf("\n Enter any infix expression
: "); gets(infix);
strcpy(postfix, "");
InfixtoPostfix(infix,
postfix);
printf("\n The corresponding postfix expression is : ");
puts(postfix);
getch();
return
0;
}
void InfixtoPostfix(char source[], char target[])
{
int i=0,
j=0; char
temp;
strcpy(target, "");
while(source[i]!='\0')
{
if f(source[i]=='(')
{
push(st,
source[i]);
i++;
}
else if(source[i] == ')')
{
while((top!=–1) && (st[top]!='('))
{
target[j] =
pop(st);
j++;
}
if(top==–1)
{
printf("\n INCORRECT EXPRESSION");
exit(1);
}
temp = pop(st);//remove left
parenthesis i++;
}
else if(isdigit(source[i]) || isalpha(source[i]))
{
target[j] =
source[i]; j++;
i++;
}
else if (source[i] == '+' || source[i] == '–' || source[i] == '*' || source[i] == '/' || source[i] == '%')
{
while( (top!=–1) && (st[top]!= '(') && (getPriority(st[top]) > getPriority(source[i])))
{
target[j] =
pop(st); j++;
}
push(st,
source[i]);
i++;
}
else
{
printf("\n INCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=–1) && (st[top]!='('))
{
target[j] =
pop(st); j++;
}
target[j]='\0';
}
int getPriority(char op)
{
if(op=='/' || op == '*' ||
op=='%') return 1;
else if(op=='+' ||
op=='–') return 0;
}
void push(char st[], char val)
{
if(top==MAX–1)
printf("\n STACK OVERFLOW");
else
{
top++;
st[to
p]=va
l;
}
}
char pop(char st[])
{
char val=' ';
if(top==–1)
printf("\n STACK UNDERFLOW");
else
{
val=st[
top];
top–
–;
}
return val;
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 4
#include<stdi
o.h>
#include<co
nio.h>
#define
MAX 10
int queue[MAX];
int front=-1,rear=-1;
void insert();
int
delete_elemen
t(); void
display();
int main()
{
int
val,opti
on; do
{ printf("\n 1.Insert
element"); printf("\n
2.Delete element");
printf("\n 3.Display
element"); printf("\n
4.EXIT");
printf("\n Enter a
choice");
scanf("%d",&option)
; switch(option)
{
case
1:insert();
break;
case
2:val=delete_el
ement();
if(val!=-1)
printf(“The number deleted is
%d",val); break;
case 3:display();
break;
}
}while(option!=4);
getch();
}
void insert()
{
int num;
printf("Enter number to be
inserted"); scanf("%d",&num);
if(front==-1&&rear==-1)
{
front=rear=
0;
queue[rear]
=num;
}
else if(front==0&&rear==MAX-1)
{ printf("\n OVERFLOW");
}
else if(rear==MAX-1&&FRONT!=0)
{ rear=0;
queue[rear]
=num;
}
else
{ rear++;
queue[rear]
=num;
}
}
int delete_element()
{
int val;
if(front==-1&&rear==-1)
{
printf("\n Underflow");
}
val=queue[front];
if(front==rear)
{
front=rear=-1;
}
else
{
if(front==
MAX-1)
front=0;
else
front=fr
ont+1;
}
return val;
}
void display()
{
int i;
if(front==-1&&rear==-
1) printf("\n Queue is
empty");
else
{
if(front<rear)
{
for(i=front;i<=rear
;i++) printf("\t
%d",queue[i]);
}
else
{
for(i=front;i<MAX
;i++) printf("\t
%d",queue[i]);
for(i=0;i<=rear;i++
) printf("\t
%d",queue[i]);
}
}
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 5
#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *display(struct node *);
struct node *insert_beg(struct
node *); struct node
*insert_end(struct node *); struct
node *insert_after(struct node *);
struct node *delete_beg(struct
node *); struct node
*delete_end(struct node *);
int main()
{
int option;
do
{
printf(“\n\n *****MAIN MENU
*****”); printf(“\n 1: Display the
list”);
printf(“\n 2: Add a node at the
beginning”); printf(“\n 3: Add a node
at the end”);
printf(“\n 4: Add a node after a given node”);
printf(“\n 5: Delete a node from the
beginning”); printf(“\n 6: Delete a node
from the end”);
printf(“\n 7: EXIT”);
printf(“\n\n Enter your option :
“); scanf(“%d”, &option);
switch(option)
{
case 1: start =
display(start); break;
case 2: start =
insert_beg(start); break;
case 3: start =
insert_end(start); break;
case 4: start =
insert_after(start); break;
case 5: start =
delete_beg(start); break;
case 6: start =
delete_end(start); break;
}
}while(option
!=7);
getch();
return 0;
}
struct node *display(struct node *start)
{
struct node
*ptr; ptr =
start;
while(ptr != NULL)
{
printf(“\t %d”, ptr ->
data); ptr = ptr ->
next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node
*new_node; int
num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next =
start; start =
new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr,
*new_node; int num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next =
NULL; ptr = start;
while(ptr -> next !=
NULL) ptr = ptr ->
next;
ptr -> next =
new_node; return
start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr,
*preptr; int num, val;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
printf(“\n Enter the value after which the data has to be inserted :
“); scanf(“%d”, &val);
new_node = (struct node *)malloc(sizeof(struct
node)); new_node -> data = num;
ptr =
start;
preptr =
ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr ->
next=new_node;
new_node -> next =
ptr; return start;
}
struct node *delete_beg(struct node *start)
{
struct node
*ptr; ptr =
start;
start = start ->
next;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr,
*preptr; ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}
#include
<stdio.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node
*front;
struct node
*rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue
*,int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
int main()
{
int val,
option;
create_que
ue(q);
clrscr();
do
{
printf("\n *****MAIN
MENU*****"); printf("\n 1.
INSERT");
printf("\n 2. DELETE");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to insert in the queue:");
scanf("%d", &val);
q =
insert(q,v
al); break;
case 2:
q=
delete_element(
q); break;
case 3:
val =
peek(q)
; if(val!
= –1)
printf("\n The value at front of queue is : %d",
val); break;
case 4:
q=
display(
q);
break;
}
}while(option != 5);
getch();
return
0;
}
void create_queue(struct queue *q)
{
q -> rear =
NULL; q ->
front =
NULL;
}
struct queue *insert(struct queue *q,int val)
{
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct
node)); ptr -> data = val;
if(q -> front == NULL)
{
q -> front
= ptr; q -
> rear =
ptr;
q -> front -> next = q -> rear -> next = NULL;
}
else
{
q -> rear -> next
= ptr; q -> rear
= ptr;
q -> rear -> next = NULL;
}
return q;
}
struct queue *display(struct queue *q)
{
struct node
*ptr; ptr = q -
> front;
if(ptr == NULL)
printf("\n QUEUE IS EMPTY");
else
{
printf("\n");
while(ptr!=q -
> rear)
{
printf("%d\t", ptr ->
data); ptr = ptr ->
next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node
*ptr; ptr = q -
> front;
if(q -> front == NULL)
printf("\n
UNDERFLOW");
else
{
q -> front = q -> front -> next;
printf("\n The value being deleted is : %d", ptr ->
data); free(ptr);
}
return q;
}
int peek(struct queue *q)
{
if(q->front==NULL)
{
printf("\n QUEUE IS
EMPTY"); return –1;
}
else
return q->front->data;
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 7
#include
<stdio.h>
#include
<conio.h>
#include
<malloc.h>
struct node
{
int data;
struct node
*left; struct
node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *,
int); void inorderTraversal(struct node
*);
struct node *deleteElement(struct node *, int);
int main()
{
int option,
val; struct
node *ptr;
create_tree
(tree);
clrscr();
do
{
printf("\n ******MAIN MENU*******
\n"); printf("\n 1. Insert Element");
printf("\n 2. Inorder
Traversal"); printf("\n 3.
Delete an element");
printf("\n 4. Exit");
printf("\n\n Enter your option :
"); scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree,
val); break;
case 2:
printf("\n The elements of the tree are :
\n"); inorderTraversal(tree);
bre
ak;
cas
e
3:
printf("\n Enter the element to be deleted : ");
scanf("%d", &val);
tree = deleteElement(tree,
val); break;
}
}while(option!
=14); return
0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr–>data = val;
ptr–>left =
NULL; ptr–
>right =
NULL;
if(tree==NU
LL)
{
tree=ptr;
tree–
>left=NULL;
tree–
>right=NULL
;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=
NULL)
{
parentptr=nodeptr
; if(val<nodeptr–
>data)
nodeptr=nodeptr
–>left; else
nodeptr = nodeptr–>right;
}
if(val<parentptr–
>data)
parentptr–>left =
ptr; else
parentptr–>right = ptr;
}
return tree;
}
void inorderTraversal(struct node *tree)
{
if(tree != NULL)
{
inorderTraversal(tree-
>left); printf("%d\t",
tree->data);
inorderTraversal(tree-
>right);}
}
struct node *deleteElement(struct node *tree, int val)
{
struct node *cur, *parent, *suc, *psuc, *ptr; if(tree–
>left==NULL)
{
printf("\n The tree is
empty "); return(tree);
}
parent =
tree; cur =
tree–
>left;
while(cur!=NULL && val!= cur–>data)
{
parent = cur;
cur = (val<cur–>data)? cur–>left:cur–>right;
}
if(cur == NULL)
{
printf("\n The value to be deleted is not present in the tree");
return(tree);
}
if(cur–>left ==
NULL) ptr =
cur–>right;
else if(cur–>right ==
NULL) ptr = cur–
>left;
else
{
psuc = cur;
cur = cur–>left;
while(suc–
>left!=NULL)
{
psuc = suc;
suc = suc–>left;
}
if(cur==psuc)
{
suc–>left = cur–>right;
}
else
{
suc–>left = cur–
>left; psuc–>left =
suc–>right; suc–
>right = cur–
>right;
}
ptr = suc;
}
if(parent–>left
== cur) parent–
>left=ptr;
else parent–
>right=ptr;
free(cur);
return tree;
}
OUTPUT
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 8
}
}
OUTPUT:
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 9
#include <stdio.h>
#include <stdlib.h>
main()
{
int array[100],search_key,i,j,n,low,high,location,choice;
void linear_search(int search_key,int array[100],int n);
void binary_search(int search_key,int array[100],int n);
clrscr();
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&n);
printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}
printf("ENTER THE SEARCH KEY:");
scanf("%d",&search_key);
printf("___________________\n");
printf("1.LINEAR SEARCH\n");
printf("2.BINARY SEARCH\n");
printf("___________________\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice)
{
case 1:
linear_search(search_key,array,n);
break;
case 2:
binary_search(search_key,array,n);
break;
default:
exit(0);
}
getch();
return 0;
}
void linear_search(int search_key,int array[100],int n)
{
int i,location;
for(i=1;i<=n;i++)
{
if(search_key == array[i])
{
location = i;
printf("______________________________________\n");
printf("The location of Search Key = %d is %d\n",search_key,location);
printf("______________________________________\n");
}
}
}
Output:
#include<stdio.h>
#include<conio.h>
void DFS(int);
int main()
{
int n,i,j,G[100][100],visited[100];
clrscr();
printf("\n Enter The Number Of Vertices : ");
scanf("%d",&n);
printf("\n Enter The Adjacency Matrix : ");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf(" The DFS Traversal Is : ");
for(i=0;i<n;i++)
{
visited[1]=0;
DFS(i);
}
getch();
return 0;
}
void DFS(int i)
{
int n,j,visited[100],G[100][100];
printf("%d\t",i);
visited[i]=1;
for(j=0;j<n;j++)
{
if(!visited[j]&&G[i][j]==1)
{
DFS(i);
}
}
}
Output :-
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 10 B
#include <stdio.h>
#define MAX 10
queue[++rear] = start;
visited[start] = 1;
while(rear != front)
start = queue[++front];
if(start == 4)
printf("5\t");
else
queue[++rear] = i;
visited[i] = 1;
void main()
clrscr();
scanf("%d", &adj[i][j]);
breadth_first_search(adj,visited,0);
getch();
OUTPUT:-
NAME : Nitesh Patel
CLASS : SE COMPS ‘B’ ROLL NO : 49
PRACTICAL 11
#include<stdio.h>
void create(int []);
void down_adjust(int [],int);
void main()
{
int heap[30],n,i,last,temp;
clrscr();
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
//create a heap
heap[0]=n;
create(heap);
//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
//print sorted data
printf("\nArray after sorting:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
getch();
}
void create(int heap[])
{
int i,n;
n=heap[0]; //no. of elements
for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}
void down_adjust(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];