0% found this document useful (0 votes)
84 views

C Lab Programs

The C program implements operations on a singly linked list to store student details ordered by rank. It defines a node structure with name, id, rank and next pointer. The insert function allocates a new node, takes input for id and name and inserts the node in ascending order of rank by comparing ranks. Display prints the list. Delete removes a node by id. The main function takes user input to perform insert, display and delete operations on the list.

Uploaded by

sowmya8929
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

C Lab Programs

The C program implements operations on a singly linked list to store student details ordered by rank. It defines a node structure with name, id, rank and next pointer. The insert function allocates a new node, takes input for id and name and inserts the node in ascending order of rank by comparing ranks. Display prints the list. Delete removes a node by id. The main function takes user input to perform insert, display and delete operations on the list.

Uploaded by

sowmya8929
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Ex.No.

1: Write a C program to perform all stack operations using array


#include<stdio.h>
#include<conio.h>
#define MAX 5
int stack[MAX];
void push();
void pop();
void disp();
int top = -1;
void main()
{
int item, choice;
for(;;)
{
printf("\n1.Push 2.Pop 3.Display 4.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch ( choice )
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printf("\nThe elements in stack are:\n");
disp();
break;
Page | 1

case 4:
exit(0);
}
}
}
void push()
{
int item;
if(top==MAX - 1 )
{
printf(" stack is full");
return;
}
printf("\nEnter item to insert :");
scanf("%d",&item);
top=top+1;
stack[top] = item;
}
void pop()
{
if ( top == -1 )
{
printf("stack is empty");
return;
}
printf("\nItem to delete is : %d", stack[top]);
top=top-1;
}
void disp()
{
int i;
Page | 2

if(top==-1)
{
printf("no elements");
return;
}
for(i=top;i>=0;i--)
{
printf("\t%d", stack[i]);
}
}

OUTPUT
1.Push 2.Pop 3.Display 4.Exit
Enter your choice: 1
Enter item to insert: 10
1.Push 2.Pop 3.Display 4.Exit
Enter your choice: 1
Enter item to insert: 20
1.Push 2.Pop 3.Display 4.Exit
Enter your choice: 3
The elements in stack are:20

10

1.Push 2.Pop 3.Display 4.Exit


Enter your choice: 2
Item to delete is : 20
1.Push 2.Pop 3.Display 4.Exit
Enter your choice: 3
The elements in stack are:10
Page | 3

Ex.No.2: Write a C program to convert infix to postfix notation using simple binary
arithmetic operators.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
int top=-1;
char stack[20];
int precedence(char ch)
{
if(ch=='(') return 1;
else if(ch=='+' || ch=='-') return 2;
else if(ch=='*' || ch=='/') return 3;
else if(ch=='$' || ch=='^') return 4;
return 0;
}
void main()
{
char infix[20],postfix[20];
int i,j=0;
printf("Enter infix expression:");
scanf("%s",infix);
for(i=0;i<strlen(infix);i++)
{
if(isalnum(infix[i]))
postfix[j++]=infix[i];
else if(infix[i]=='(')
stack[++top]=infix[i];
else if(infix[i]==')')
{
while(stack[top]!='(')
{
Page | 4

postfix[j++]=stack[top--];
}
top=top-1;
}
else
{
while(precedence(stack[top])>=precedence(infix[i]))
{
postfix[j++]=stack[top--];
}
stack[++top]=infix[i];
}
}
while(top!=-1)
{
postfix[j++]=stack[top--];
}
postfix[j++]='\0';
printf("Postfix expression is:");
puts(postfix);
getch();
}

OUTPUT
Enter infix expression: 3+4
Postfix expression is : 34+

Page | 5

Ex.No.3: Write a C program to evaluate postfix expression for simple binary


arithmetic operations using stack.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int stack[20];
char post[20];
int top=-1;
void compute(char c);
void main()
{
int i;
clrscr();
printf("Enter postfix expression : ");
gets(post);
for(i=0;i<strlen(post);i++)
{
if(isdigit(post[i]))
{
top++;
stack[top]=(int)(post[i]-48);
}
else
compute(post[i]);
}
printf("\n\nResult : %d",stack[top]);
getch();
}
void compute(char c)
{
Page | 6

int a,b,ans;
a=stack[top];
top--;
b=stack[top];
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':
ans=pow(b,a);
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
OUTPUT
Enter postfix expression : 23+
Result : 5

Page | 7

Ex.No.4: Write a C program for the following: [use recursive functions]

a.

a)

Calculate GCD & LCM

b)

Tower of Hanoi Problem

c)

Binary Search

To calculate GCD and LCM

#include<stdio.h>
#include<conio.h>
int gcd(int m,int n)
{
if(n==0)
return m;
return gcd(n,m%n);
}
void main()
{
int m,n;
clrscr();
printf("Enter values for m,n:");
scanf("%d%d",&m,&n);
printf("GCD=%d",gcd(m,n));
printf("LCM=%d",((m*n)/gcd(m,n)));
getch();
}

OUTPUT
Enter values for m,n: 10

GCD=2 LCM=30
Page | 8

b.Tower of Hanoi Problem

#include<stdio.h>
#include<conio.h>
void tower(int n,char source, char destination,char aux)
{
if(n==1)
{
printf("Move disc %d from %c to %c\n",n,source,destination);
return;
}
tower(n-1,source,aux,destination);
printf("Move disc %d from %c to %c\n",n,source,destination);
tower(n-1,aux,destination,source);
}
void main()
{
int n;
printf("Enter the no.of discs:");
scanf("%d",&n);
tower(n,'A','B','C');
getch();
}

OUTPUT
Enter the no.of discs:2
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B

Page | 9

c. Binary Search

#include<stdio.h>
#include<conio.h>

int bins(int arr[],int key,int low,int high);

void main()
{
int arr[20],i,n,pos,key;
clrscr();
printf("\nEnter the no.of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<=n-1;i++)
scanf("%d",&arr[i]);
printf("\nEnter the search element:");
scanf("%d",&key);
pos=bins(arr,key,0,n-1);
if(pos==-1)
printf("\nElement is not found");
else
printf("\nElement found at position: %d",pos);
getch();
}

int bins(int arr[],int key,int low,int high)


{

Page | 10

int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(key==arr[mid])
return mid;
if(key<arr[mid])
return bins(arr,key,low,mid-1);
else
return bins(arr,key,mid+1,high);
}

OUTPUT
Enter the no. of elements: 4
Enter the elements:
2
5
6
8
Enter the search element: 5
Element found at position 1

Page | 11

Ex.No.5: Write a C program to simulate the functions of Queue


#include<stdio.h>
#include<conio.h>
#define QUE_SIZE 5
int front=0,rear=-1;q[QUE_SIZE],item,i;
void insert();
void delet();
void disp();
void main()
{
int choice;
clrscr();
for(;;)
{
printf("\n1:Insert;2:Delete;3:Display;4:Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert:");
scanf("%d",&item);
insert();
break;
case 2:
delet();
break;
case 3:
printf("\nElements in Queue are:");
disp();
break;
Page | 12

default:
exit(0);
}
}
}
void insert()
{
if(rear==QUE_SIZE-1)
printf("queue overflow");
else
q[++rear]=item;
}
void delet()
{
if(front>rear)
{
printf("queue underflow");
return;
}
printf("\nElement deleted is %d:",q[front++]);
if(front>rear)
{
front=0;
rear=-1;
}
}
void disp()
{
int i;
Page | 13

if(front>rear)
{
printf("queue is empty");
return;
}
for(i=front;i<=rear;i++)
{
printf("%d",q[i]);
}
}

OUTPUT
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 1
Enter element to insert: 10
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 1
Enter element to insert : 20
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 3
Elements in queue are:10

20

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 2
Element deleted is 10
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 3
Elements in queue are:20
Page | 14

Ex.No.6: Write a C program to simulate the functions of Circular Queue


#include<stdio.h>
#include<conio.h>
#define MAX 5
int q[MAX],front=-1,rear=-1,count=0,foriginal;
void insert();
void dele();
void disp();
void main()
{
int choice;
for(;;)
{
printf("\n1:Insert;2:Delete;3:Display;4:Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
dele();
break;
case 3:
printf("\nElements in Queue are: ");
disp();
break;
default:
exit(0);
}
Page | 15

}
}
void insert()
{
int item;
printf("\nEnter item to insert: ");
scanf("%d",&item);
if((rear+1)%MAX==front)
{
printf("Queue is full");
return;
}
rear=(rear+1)%MAX;
q[rear]=item;
count=count+1;
if(front==-1)
front=front+1;
}
void dele()
{
if(front==-1)
{
printf("Queue is empty");
return;
}
printf("\nItem deleted:%d",q[front]);
count=count-1;
if(front==rear)
front=rear=-1;
else
Page | 16

front=(front+1)%MAX;
}
void disp()
{
int i;
foriginal=front;
for(i=0;i<count;i++)
{
printf("\t%d",q[front]);
front=(front+1)%MAX;
}
front=foriginal;
}

Page | 17

OUTPUT
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:1
Enter element to insert : 10

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:1
Enter element to insert : 20

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 3
Elements in queue are:10

20

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 2
Item deleted : 10

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice : 3
Elements in queue are:20

Page | 18

Ex.No.7: Write a C program to simulate the functions of Priority Queue


#include<stdio.h>
#include<conio.h>
#define QUE_SIZE 5
int front=0,rear=-1;q[QUE_SIZE],item,i;
void insert();
void delet();
void disp();
void main()
{
int choice;
for(;;)
{
printf("\n1:Insert;2:Delete;3:Display;4:Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter element to insert:");
scanf("%d",&item);
insert();
break;
case 2:
delet();
break;
case 3:
printf("\nElements in Queue are:");
disp();
break;
default:
Page | 19

exit(0);
}
}
}
void insert()
{
int j;
if(rear==QUE_SIZE-1)
{
printf("queue overflow");
return;
}
j=rear;
while(j>=0 && item <q[j])
{
q[j+1]=q[j];
j--;
}
q[j+1]=item;
rear=rear+1;
}
void delet()
{
if(front>rear)
{
printf("queue underflow");
return;
}
printf("\nElement deleted is %d:",q[front++]);
if(front>rear)
Page | 20

{
front=0;
rear=-1;
}
}
void disp()
{
int i;
if(front>rear)
{
printf("queue is empty");
return;
}
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
}

Page | 21

OUTPUT

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:1
Enter element to insert : 50
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:1
Enter element to insert : 20
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:1
Enter element to insert : 30

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:3
Elements in Queue are:20

30

50

1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:2
Element deleted is: 20
1:Insert;2:Delete;3:Display;4:Exit
Enter your choice:3
Elements in Queue are: 30

50

Page | 22

Ex.No.8: Write a C program to construct singly linked list which contains student
details in ascending order according to their rank information and
perform all possible operations
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct Node
{
char name[20];
int id;
int rank;
struct Node *next;
}node;
void insert(node *pointer)
{
node *temp;
temp = (node *)malloc(sizeof(node));
printf("\nEnter id:");
scanf("%d",&temp->id);
printf("\nEnter name:");
scanf("%s",temp->name);
printf("\nEnter rank:");
scanf("%d",&temp->rank);
if(pointer->next==NULL)
{
pointer->next=temp;
temp->next=NULL;
}
Page | 23

else
{
while(pointer->next!=NULL&&(pointer->next)->rank <= temp->rank)
{
pointer = pointer -> next;
}
temp->next=pointer->next;
pointer->next=temp;
}
return;
}

void delete(node *pointer, int id)


{
node *temp;
while(pointer->next!=NULL && (pointer->next)->id != id)
{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",id);
return;
}
temp = pointer -> next;
pointer->next = temp->next;
free(temp);
return;
Page | 24

}
void print(node *pointer)
{
node *p;
if(pointer==NULL)
{
return;
}
else
{
for(p=pointer;p!=NULL;p=p->next)
{
printf("\nId:%d",p->id);
printf("\tName:%s",p->name);
printf("\tRank:%d",p->rank);
}
}
}
void main()
{
node *start;
int choice,id;
clrscr();
start = (node *)malloc(sizeof(node));
start->next=NULL;
for(;;)
{
printf("\n1.Insert 2. Delete 3. Print 4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
Page | 25

switch(choice)
{
case 1:
insert(start);
break;
case 2:
printf("\nEnter id to delete:");
scanf("%d",&id);
delete(start,id);
break;
case 3:
printf("\nThe list is: \n");
if(start->next==NULL)
{
printf("EMPTY!");
}
else
{
print(start->next);
}
break;
case 4:
exit(0);
break;
}
}
}

Page | 26

OUTPUT
1.Insert 2. Delete 3. Print 4. Exit\n");
Enter your choice: 1
Enter id : 10
Enter name : Adhithi
Enter rank : 2

1.Insert 2. Delete 3. Print 4. Exit\n");


Enter your choice: 1
Enter id : 20
Enter name : Suprithi
Enter rank : 1

1.Insert 2. Delete 3. Print 4. Exit\n");


Enter your choice: 3
The list is :
Id:20 Name:Suprithi Rank:1
Id:10 Name:Adhithi Rank:2

1.Insert 2. Delete 3. Print 4. Exit\n");


Enter your choice: 2
Enter id to delete: 10

1.Insert 2. Delete 3. Print 4. Exit\n");


Enter your choice: 3
The list is :
Id:20 Name:Suprithi Rank:1

Page | 27

Ex.No.9: Write a C program to implement stack operation using linked list.


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node *pointer, int data)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->data=data;
temp->next=pointer->next;
pointer->next=temp;
}
void delete(node *pointer)
{
node *temp;
temp=pointer->next;
pointer->next=temp->next;
free(temp);
}
void print(node *pointer)
{
if(pointer==NULL)
Page | 28

{
return;
}
else
{
printf("%d",pointer->data);
printf("\t");
print(pointer->next);
}
}
void main()
{
node *start;
int choice,data;
clrscr();
start = (node *)malloc(sizeof(node));
start->next=NULL;
for(;;)
{
printf("\n1.Insert 2.Delete 3. Print 4. Exit\n");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter data:");
scanf("%d",&data);
insert(start,data);
break;
case 2:
delete(start);
Page | 29

break;
case 3:
printf("\nThe list is: ");
if(start->next==NULL)
{
printf("EMPTY!");
}
else
{
print(start->next);
}
break;
case 4:
exit(0);
break;
}
}
}

Page | 30

OUTPUT
1.Insert 2.Delete 3. Print 4. Exit\n");
Enter your choice: 1
Enter data: 20

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 1
Enter data: 50

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 3
The list is: 50

20

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 2

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 3
The list is: 20

Page | 31

Ex.No.10: Write a C program to implement doubly linked list and perform all
possible operations.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
}node;
void insert(node *pointer, int data)
{
node *temp;
while(pointer->next!=NULL)
{
pointer = pointer -> next;
}
temp = (node *)malloc(sizeof(node));
temp->data=data;
temp->next=NULL;
pointer->next=temp;
temp->prev=pointer;
}
void delete(node *pointer, int data)
{
node *temp,*nn;
while(pointer->next!=NULL && (pointer->next)->data != data)
Page | 32

{
pointer = pointer -> next;
}
if(pointer->next==NULL)
{
printf("Element %d is not present in the list\n",data);
return;
}
temp = pointer -> next;
nn=temp->next;
pointer->next = temp->next;
nn->prev=temp->prev;
free(temp);
return;
}
void disp(node *pointer)
{
node *p;
for(p=pointer->next;p!=NULL;p=p->next)
{
printf("\t%d",p->data);
}
return;
}
void main()
{
node *start;
int choice,data;
clrscr();
start = (node *)malloc(sizeof(node));
start->next=NULL;
Page | 33

for(;;)
{
printf("\n1.Insert 2.Delete 3.Print 4. Exit\n");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter data:");
scanf("%d",&data);
insert(start,data);
break;
case 2:
printf("\nEnter data to delete:");
scanf("%d",&data);
delete(start,data);
break;
case 3:
printf("\nThe list is: ");
if(start->next==NULL)
{
printf("EMPTY!");
}
else
{
disp(start);
}
break;
case 4:
exit(0);
break;
}
Page | 34

}
}

OUTPUT
1.Insert 2.Delete 3. Print 4. Exit\n");
Enter your choice: 1
Enter data: 50

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 1
Enter data: 20

1.Insert 2.Delete 3. Print 4. Exit\n");


Enter your choice: 3
The list is: 50 20
1.Insert 2.Delete 3. Print 4. Exit\n");
Enter your choice: 2
Enter data to delete: 20
1.Insert 2.Delete 3. Print 4. Exit\n");
Enter your choice: 3
The list is: 50

Page | 35

Ex.No.11: Write a C program to construct a binary tree and perform all traversal
operations.
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST
{
int data;
struct BST *lchild,*rchild;
}node;
void insert(node *root,node *new_node)
{
if(new_node->data < root->data)
{
if(root->lchild==NULL)
root->lchild = new_node;
else
insert(root->lchild,new_node);
}
if(new_node->data > root->data)
{
if(root->rchild==NULL)
root->rchild=new_node;
else
insert(root->rchild,new_node);
}
}
void inorder(node *temp)
{
if(temp!=NULL)
{
Page | 36

inorder(temp->lchild);
printf("%d",temp->data);
inorder(temp->rchild);
}
}
void preorder(node *temp)
{
if(temp!=NULL)
{
printf("%d",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder(node *temp)
{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d",temp->data);
}
}
void main()
{
int choice;
node *root,*new_node;
root=NULL;
clrscr();
for(;;)
{
Page | 37

printf("\n1.Insertion 2.Traversal 3.Exit");


printf("\nEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
new_node=(node *)malloc(sizeof(node));
new_node->lchild=NULL;
new_node->rchild=NULL;
printf("\nEnter The Element:");
scanf("%d",&new_node->data);
if(root==NULL)

/* Tree is not Created */

root=new_node;
else
insert(root,new_node);
break;
case 2:
if(root==NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3:
exit(0);
}} }
Page | 38

OUTPUT for the following tree:


20
10

30

1.Insertion 2.Traversal 3.Exit


Enter your choice :1
Enter The Element:20
1.Insertion 2.Traversal 3.Exit
Enter your choice :1
Enter The Element:10

1.Insertion 2.Traversal 3.Exit


Enter your choice :1
Enter The Element:30
Enter your choice
2
The Inorder Display : 10
The Preorder Display : 20

20

The Postorder Display : 10

30

10
30

30
20

Page | 39

Ex.No.12: Write a C program to search the given key using Binary search & Linear
search.

a. LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[10],n,key,found;
found=0;
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter search element:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==arr[i])
{
printf("Element found");
found=1;
}
}
if(found==0)
printf("Element not found");
getch();
}

Page | 40

OUTPUT
Enter the no.of elements:5
Enter the elements:6

Enter the search element:8


Element found
Enter the no.of elements:5
Enter the elements:6

Enter the search element:1


Element not found

Page | 41

b. Binary Search
#include<stdio.h>
#include<conio.h>
void bins(int arr[],int n,int key);
void main()
{
int arr[20],i,n,key;
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<=n-1;i++)
scanf("%d",&arr[i]);
printf("\nEnter the search element:");
scanf("%d",&key);
bins(arr,n,key);
getch();
}
void bins(int arr[20],int n,int key)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==arr[mid])

Page | 42

{
printf("Element found");
return;
}
if(key<arr[mid])
high=mid-1;
if(key>arr[mid])
low=mid+1;
}
printf("Element not found");
return;
}

OUTPUT
Enter the no.of elements:3
Enter the elements:8

Enter the search element:3


Element found
Enter the no.of elements:3
Enter the elements:8

Enter the search element:


9
Element not found

Page | 43

Ex.No.13: Write a C program to perform the Quick sort operation

#include<stdio.h>
#include<conio.h>
void qsort(int arr[20],int,int);
void main()
{
int arr[20],n,i;
clrscr();
printf("Enter no.of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
qsort(arr,0,n-1);
printf("Sorted array:");
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
getch();
}

void qsort(int arr[20],int low,int high)


{
int pivot,j,temp,i;
if(low<high)
{
pivot=low;
i=low;
Page | 44

j=high;
while(i<j)
{
while(arr[i]<=arr[pivot])
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
qsort(arr,low,j-1);
qsort(arr,j+1,high);
}
}

OUTPUT
Enter the no.of elements: 5
Enter the elements: 10
Sorted array: 10

20

60
30

30
60

20

90

90

Page | 45

Ex.No.14: Write a C program to perform the Heap sort operation.


#include<stdio.h>
#include<conio.h>
void adjust(int a[],int n);
void heapsort(int a[],int n);
void heapify(int a[],int n);
void main()
{
int a[20],n,temp,i;
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\nSorted array:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}
void heapsort(int a[],int n)
{
int i,temp;
heapify(a,n);
for(i=n-1;i>0;i--)
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
Page | 46

adjust(a,i);
}
}
void adjust(int a[],int n)
{
int i,j,item;
j=0;
item=a[j];
i=2*j+1;
while(i<=n-1)
{
if(i+1<=n-1)
if(a[i]<a[i+1])
i++;
if(item<a[i])
{
a[j]=a[i];
j=i;
i=2*j+1;
}
else
break;
}
a[j]=item;
}
void heapify(int a[],int n)
{
int i,j,k,item;
for(k=1;k<n;k++)
{
Page | 47

item=a[k];
i=k;
j=(i-1)/2;
while(i>0 && item >a[j])
{
a[i]=a[j];
i=j;
j=(i-1)/2;
}
a[i]=item;
}
}

OUTPUT
Enter the no.of elements:5
Enter the elements : 10

Sorted array : 6

25

10

25

35

35

Page | 48

Ex.No.15: Write a C program to perform the BFS in a Graph


#include<stdio.h>
#include<conio.h>
int n,i,j,a[20][20],start,q[20],visited[20];

void bfs(int);

void main()
{
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("Enter adjacency matrix:");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
visited[i]=0;
}
}
printf("Enter source vertex:");
scanf("%d",&start);
bfs(start);
getch();
}

void bfs(int node)


{
Page | 49

int f=0,r=-1;
q[++r]=node;
visited[node]=1;
printf("Order in which nodes are reached:");
while(1)
{
for(i=1;i<=n;i++)
if(a[node][i]==1 && visited[i]==0)
{
q[++r]=i;
visited[i]=1;
}
printf("%d",q[f++]);
if(f>r)
break;
else
node=q[f];
}
}

OUTPUT
for the following graph:

Page | 50

Enter no.of nodes: 3


Enter adjacency matrix:
0 1 0
0 0 1
0 0 0
Enter source vertex: 2
Order in which nodes are reached : 2

Page | 51

You might also like