Sie sind auf Seite 1von 99

List of Experiments

Sl. No Experiment
1 Searching Algorithms

1. Sequential Search
2. Binary Search
1.
2 Sorting Algorithms
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Shell Sort
5. Merge Sort
6. Quick Sort
7. Heap Sort
3 Stack Implementation
1. Stack implementation using Array
2. Stack implementation using Linked list
4 Queue Implementation
1. Queue implementation using Array
2. Queue implementation using Linked list

5 Expression evaluation

6 Polynomial addition

7 Sparse matrix addition

8 Binary Tree representation and


Traversal Techniques (In order,
Preorder, Post order)
9 Binary Search Tree

10 Graph Traversal
1. Breadth First Search
2. Depth First Search

11 Dijkstras Algorithm (single source


shortest path algorithm)

12 Hashing and collision resolution


techniques

13 AVL Tree
Sequential search:

Aim:

Page 1 of 99
To create a c program to search the element in an array using Sequential search
algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements (a []).
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the values of the elements of the array.
Step 5: Get the element to be searched in the array.
Step 6: Check whether the given element is present in the array by checking
each position one by one.
Step 7: If the element is present in the array, the position of the element is printed.
Step 8: If the element is not present in the array, it prints Element not present.
Step 9: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,m,flag=0,position;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
printf("\n\n Enter the number:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\n Enter the value to be found:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
if(a[i]==m)
{
position=i+1;
flag=1;
}
}
if (flag==1)

Page 2 of 99
{
printf("\n\n The element is present ");
}
else
{
printf("\n\n The element is not present");
}
getch();
}

Output:
Enter the value of n:5

Enter the number:1


2
3
8
7

Enter the value to be found:0

The element is not present

Result:
Thus the c program for sequential search is compiled and executed successfully.

2. Binary search

Page 3 of 99
Aim:
To create a c program to search the element in an array using Binary search
algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements.
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the values of the elements of the array.
Step 5: Divide the array of elements into two which gives a middle element.
Step 6: Get the element to be searched in the array.
Step 7: Compare the element with the middle element. If the element to be
searched is the middle element stop searching and print the position.
Step 8: If the element is lesser than the middle element the searching proceeds with
the first half of the array.
Step 9: If the element is greater than the middle element the searching proceeds
with the second half of the array.
Step 10: If the element is not present in the array print Unsuccessful search.
Step 11: If the element is present in the array, print the position of the searched
element.
Step 12: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],k,x,l,m,h,n,flag;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("enter the elements:");
for(k=0;k<n;k++)

Page 4 of 99
scanf("%d",&a[k]);
printf("enter the element to search:");
scanf("%d",&x);
l=0;h=n-1;
while(l<=n)
{
m=(l+h)/2;
if(x==a[m])
{
printf("successful search");
printf(" \n the elementis present at %d",m+1);
printf("\n the element is %d",m+1);
break;
}
if(x<a[m])
{
h=m-1;
continue;
}
if(x>a[m])
{
l=m+1;
continue;
}
}
while(l>h)
{
printf("unsuccessful search");
break;
}
getch();
}

Page 5 of 99
Output:
enter the value of n:5
enter the elements:
2
8
6
7
2
enter the element to search:6
successful search
the elementis present at 3
the element is 3

Result:

Thus the c program for binary search is compiled and executed successfully.

II Sorting: 1. Bubble sort:


Aim:

Page 6 of 99
To create a c program to sort the unsorted array using Bubble sort algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Declare the variables and an array of elements.

Step 3: Read the value of number of elements to be stored in the array.

Step 4: Read the value of the elements of the array.

Step 5: Select the first element of the array and compare it with second element, if

the first element is greater than second, the positions are interchanged.

Step 6: Now, the first element is compared with third element if the first element

is lesser than the third element. The positions are interchanged.

Step 7: Repeat the process till the first element is compared with rest of the

elements in the array.

Step 8: Print the sorted list.

Step 9: Stop the program.

Program coding:

#include<stdio.h>

#include<conio.h>

void main()

int a[5],t,i,n,j;

clrscr();

printf("bubble sort");

printf("\n\n enter the no of value");

scanf("%d",&n);

printf("enter the value");

Page 7 of 99
for(i=0;i<=n;i++)

scanf("%d",&a[i]);

for(i=0;i<=n;i++)

for(j=i+1;j<n;j++)

if(a[i]>a[j])

t=a[i];

a[i]=a[j];

a[j]=t;

printf("\n the sorted list are");

for(i=0;i<=n;i++)

printf("\n%d",a[i]);

getch();

Output:

Enter the number of elements:8

Page 8 of 99
Enter the elements:
5
2
8
6
9
1
7
3

The sorted list are:

1
2
3
5
6
7
8
9
Result:
Thus the c program for bubble sorting is compiled and executed successfully.

2. Selection sort:
Aim:
To create a c program to sort the unsorted array using Selection sort algorithm.

Page 9 of 99
Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements.
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the values of the elements of the array.
Step 5: Select the first element from the array and then compare with the next
elements. If any element is found to be lesser interchange the elements.
Step 6: Select the next element and compare with the other elements, if any of
the elements is lesser than the selected element, exchange the positions.
Step 7: Repeat Step 5 and 6 until all the elements are sorted.
Step 8: Print the sorted list.
Step 9: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],i,j,k,n,min;
clrscr();
printf("\t\t\t\tSELECTION SORT");
printf("\nEnter the number of terms:");
scanf("%d",&n);
printf("Enter the numbers to be sorted:");
for(i=0;i<n;i++)
{
scanf("\n%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
k=i;
min=a[i];

Page 10 of 99
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
k=j;
}
}
a[k]=a[i];
a[i]=min;
}
printf("\nThe sorted list is:\n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
printf("\t");
}
getch();
}
Output:
Enter the number of terms:5
Enter the numbers to be sorted:2
4
6
4
1

The sorted list is:


1 2 4 4 6

Result:

Thus the c program for selection sorting is compiled and executed successfully.

3. Insertion sort:

Page 11 of 99
Aim:
To create a c program to sort the unsorted array using Insertion sort algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Declare the variables and an array of elements.

Step 3: Read the value of number of elements to be stored in the array.

Step 4: Read the values of the elements of the array.

Step 5: The first iteration is to compare the first element with the second element.

The second iteration is to compare the first element with the third element

and goes on.

Step 6: While comparing, if it is found that any element can be inserted at suitable

position a space is created for it by shifting the other elements one position

up and inserts the desired element at suitable position.

Step 7: Finally sort the array.

Step 8: Stop the program.

Program coding:

#include<stdio.h>

#include<conio.h>

void main()

int i,j,k,n,save;

int a[30];

clrscr();

printf("enter the no of elements to sort");

scanf("%d",&n);

printf("enter element:");

Page 12 of 99
for (i=0;i<n;i++)

scanf("%d",&a[i]);

k=-1;

for(j=1;j<n;j++)

k=a[j];

i=j-1;

while(i>=0)

if(k<a[i])

a[i+1]=a[i];

save=i;

i--;

if(save!=-1)

a[save]=k;

printf("\n the sorted list is");

for(i=0;i<n;i++)

printf("\n%d",a[i]);

Page 13 of 99
getch();

Output:

enter the no of elements to sort5


enter element:9
8
7
6
5

the sorted list is


5
6
7
8
9

Result:

Thus the c program for insertion sorting is compiled and executed successfully.

4. Shell sort:
Aim:
To create a c program to sort the unsorted array using Shell sort algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements.
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the values of the elements of the array.
Step 5: In the first iteration, the element are divided with a gap say (S, 3) etc.

Page 14 of 99
Step 6: The sub list obtained after the division is sorted by comparing the elements
swapping it.
Atep7: Repeat Step 5 for the next iteration.
Step 8: The sorted sub lists are recombined.
Step 9: Print the sorted array of elements.
Step 10: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
void shell(int a[ ],int n);
int i,j,k,n,t,a[25];
void main()
{
clrscr();
printf("\n\n enter the number to be sorted");
scanf("\n%d",&n);
printf("\n\n enter the element to be sorted");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell(a,n);
printf("\n the sorted list are ");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
getch();

Page 15 of 99
}
void shell(int a[],int n)
{
for(i=(n+1)/2;i>=1;i/=2)
{
for(j=1;j<=n-1;j++)
{
t=a[j];
k=j-1;
while(k>=0&&t<a[k])
{
a[k+1]=a[k];
k=k-1;
}
a[k+1]=t;
}
}
}

Output:
enter the number to be sorted6

enter the element to be sorted9


8
8
7
6
5

the sorted list are


5
6

Page 16 of 99
7
8
8
9
Result:

Thus the c program for shell sorting is compiled and executed successfully.

5. Merge sort:

Aim:
To create a c program to sort the unsorted array using Merge sort algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and two arrays of elements.
Step 3: Read the value of number of elements to be stored in the two arrays.
Step 4: Read the values of the elements of two arrays.
Step 5: Compare the first element with the last element, if the first is lesser than the
last element compute the middle value sort and split the array as first,
middle, middle+1,and the last.

Step 6: Compare two arrays, If the 0th element of the first array (a[i]) is smaller
than the 0th element of the second (b[i]) array then put the 0th element of the
first array in the third array to be merged.
Step 7: Then compare the 1st element of a[i] with the 0th element of b[i] and repeat
the steps.
Step 8: Print the sorted third array.
Step 9: Stop the program.
Program coding:

#include<stdio.h>

#include<conio.h>

void main()

int i,n,j,k,min,a[10];

Page 17 of 99
clrscr();

printf("merge sort");

printf("enter the limit");

scanf("\n\n%d",&n);

printf("enter the element");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n;i++)

k=i;

min=a[i];

for(j=i+1;j<n;j++)

if(a[j]<min)

min=a[j];

k=j;

a[k]=a[i];

a[i]=min;

printf("the sorted list are");

for(i=0;i<n;i++)

Page 18 of 99
{

printf("%d\n\n",a[i]);

getch();

Output:

enter the limit: 5


enter the element:
3
9
8
7
3
the sorted list are3

3
7
8
9
Result:

Thus the c program for merge sorting is compiled and executed successfully.
6. Quick sort:

Aim:
To create a c program to sort the unsorted array using Quick sort algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements.
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the values of the elements of the array.
Step 5: Select the first element and the last element called pivot from the array and
compare with the other elements from the both sides of the array.
Step 6: The elements which are lesser than the pivot element is placed before the
pivot element and greater placed after the pivot element.

Page 19 of 99
Sterp7: Then the array is divided into two. Step 5 and Step 6 is repeated on both
the halves of the array until the sorting is done completely.
Step 8: Print the sorted array.
Step 9: Stop the program.

Program coding:

#include<stdio.h>

#include<conio.h>

void quick(int a[],int,int);

void split(int a[],int,int);

void main()

int i,n,a[20];

clrscr();

printf("\n enter the limit:");

scanf("%d",&n);

printf("\n enter the elements to be sorted\n");

for(i=0;i<n;i++)

printf("\n\t a[%d]=",i);

scanf("%d",&a[i]);

quick(a,0,n-1);

printf("\n the sorted elements are:");

for(i=0;i<n;i++)

printf("%d\t",a[i]);

Page 20 of 99
getch();

void quick(int a[],int first,int last)

int x,i,j;

if(first<last)

x=a[first];

i=first;

j=last;

while(i<j)

while(a[i]<=x&&i<last)

i++;

while(a[j]>=x&&j>first)

j--;

if(i<j)

split(a,i,j);

split(a,first,j);

quick(a,first,j-1);

quick(a,j+1,last);

void split(int a[],int i,int j)

Page 21 of 99
int temp;

temp=a[i];

a[i]=a[j];

a[j]=temp;

Output:

Enter the limit of element:7

Enter the elements:


5
8
9
6
2
7
1

The sorted list is:

1
2
5
6
7
8
9

Result:

Thus the c program for quick sorting is compiled and executed successfully.

7. Heap sort:
Aim:
To create a c program to sort the unsorted array using Heap sort algorithm.

Algorithm:

Step 1: Start the program.


Step 2: Declare the variables and an array of elements.

Page 22 of 99
Step 3: Read the value of number of elements to be stored in the array.
Step 4: Read the value of the elements of the array.
Step 5: Create the heap (tree) with the given set of elements.
Step 6: Compare the root element with the last element of the heap.
Step 7: If the last element is greater than the root element, swapping is done and
rearrange the elements to get balanced.
Step 8: Repeat the Step 6 and Step 7 until all the elements are sorted.
Step 9: Print the sorted array of elements
Step 10: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
void heapsort(int k[],int n);
void createheap(int k[],int n);
void main()
{
int k[25],i,n;
clrscr();
printf("Enter the limit:");
scanf("%d",&n);
printf("Enter the elements to be sorted:");
for(i=1;i<=n;i++)
scanf("%d",&k[i]);
heapsort(k,n);
getch();
}
void heapsort(int k[],int n)
{

Page 23 of 99
int q,key,i,j,temp;
createheap(k,n);
for(q=n;q>=2;q--)
{
temp=k[1];
k[1]=k[q];
k[q]=temp;
i=1;
key=k[i];
j=1;
if((j+1)<q)
{
if(k[j+1]>k[j])
j++;
while((j<=q-1)&&(k[j]>key))
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
i=j;
j=2*i;
if((j+1)<q)
{
if(k[j+1]>k[j])
j=j+1;
if(j>n)
{
j=n;

Page 24 of 99
k[i]=key;
}}}}}
printf("\n\n The elements after sorting:\n");
for(i=1;i<=n;i++)
printf("\t%d",k[i]);
}
void createheap(int k[],int n)
{
int i,j,q,key,temp;
for(q=2;q<=n;q++)
{
i=q;
key=k[q];
j=i/2;
while((i>1)&&(key>k[j]))
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
i=j;
j=i/2;
if(j<1)
j=1;
k[i]=key;
}
}
printf("\n\n Heap generated for the elements as follows:\n");
for(q=1;q<=n;q++)

Page 25 of 99
printf("\t%d",k[q]);
}
Output:
Enter the limit:5
Enter the elements to be sorted:12
11
14
16
17

Heap generated for the elements as follows:


17 16 12 11 14

The elements after sorting:


11 12 14 16 17

Result:

Thus the c program for heap sorting is compiled and executed successfully.

III Stack Implementation :

1. Implementation of Stack using array:


Aim:
To create a c program to implement a Stack data structure using array.
Algorithm:
Step 1: Start the program.
Step 2: Declare variables and an array to perform stack operations (push
and pop).
Step 3: Read the maximum size of the stackn.
Step 4: Read the choice of operation.
Step 5: If the size of the stack is full (i.e) the top of the stack is equal or greater
than max size, print stack is full.
Step 6: If the top of the stack is found to be lesser than the max value, enter the

Page 26 of 99
elements (push).
Step 7: If the top of the stack is found to be equal or greater than the max value,
pop out the Elements.
Step 8: If the top of the stack is not equal to zero display the stack.
Step 9: Display the contents of the stack.
Step 10: Exit Stack
Step 11: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int choice,n,a[30],top=0,i;
clrscr();
printf("\n stack using array");
do
{
printf("Main menu");
printf("\n1.stack max");
printf("\n2.push");
printf("\n3.pop");
printf("\n4.display");
printf("\n5.size");
printf("\n6.exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)

Page 27 of 99
{
case 1:
printf("\nEnter the stack max size:");
scanf("%d",&n);
break;
case 2:
if (top>=n)
{
printf("The stack is full");
}
else
{
printf("Enter the element:");
top++;
scanf("%d",&a[top]);
}
break;
case 3:
if (top==0)
{
printf("\nthe stack is empty");
}
else
{
printf("\npoped out element is%d\n",a[top]);
top--;
}
break;

Page 28 of 99
case 4:
if (top!=0)
{
printf("\nStack contains\n");
for(i=1;i<=top;i++)
printf("\t%d",a[i]);
}
else
{
printf("\nStack is empty");
}
break;
case 5:
printf("\nThe size of stack is%d",top);
break;
default:
exit(1);
}
}
while(choice<6);
getch();
}
Output:
Main menu
1.stack max
2.push
3.pop
4.display
5.size
6.exit

Page 29 of 99
Enter your choice:1
Enter the stack max size:2
Main menu
1.stack max
2.push
3.pop
4.display
5.size
6.exit
Enter your choice:2
Enter the element:5
Main menu
1.stack max
2.push
3.pop
4.display
5.size
6.exit
Enter your choice:2
Enter the element:9
Main menu
1.stack max
2.push
3.pop
4.display
5.size
6.exit
Enter your choice:3
The poped out element is : 9
Main menu
1.stack max
2.push
3.pop
4.display
5.size
6.exit
Enter your choice:4
Stack contains: 5

Result:

Thus the c program to implement stack using array is compiled and executed
successfully.

Page 30 of 99
2. Stack implementation using Linked list:
Aim:
To create a c program to implement a Stack data structure using Linked list.
Algorithm:

Step 1: Start the program.


Step 2: Declare the Stack data structure using pointer.
Step 3: Implement stack and read the choice of operation.
Step 4: Enter the elements to push into the stack.
Step 5: If the next space of the stack is found to be NULL then declare top of the
stack is empty, create a new node and make the top of the as new
node.
Step 6: If the top of the stack is found to be full pop out the element from the stack.
Step 7: If the top of the stack is found to be empty, print the stack is empty
otherwise display the stack.
Step 8: Exit the stack.
Step 9: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;

Page 31 of 99
struct node*next;
}*list,*temp,*newnode,*prev,*t;
void add()
{
printf("\nEnter the data to be added:");
newnode=(struct node*)malloc(sizeof (struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
if (list->next==NULL)
{
list->next=newnode;
return;
}
for(temp=list->next;temp->next!=NULL;temp=temp->next);
temp->next=newnode;
}
void delete()
{
int val;
printf("Enter the data to be removed");
scanf("%d",&val);
if(list->next->data==val)
{
t=list->next;
list->next=list->next->next;
list=t;
return;
}

Page 32 of 99
for(temp=t=list->next;temp->next!=NULL;temp=temp->next)
{
if(temp->data==val)
{
t->next=temp->next;
temp->next=NULL;
return;
}
t=temp;
}
if(temp->data==val)
{
t->next=NULL;
return;
}
}
void display()
{
if(list->next==NULL)
{
printf("Listis empty");
return;
}
for(temp=list->next;temp->next!=NULL;temp=temp->next)
printf("%d\t",temp->data);
printf("%d\n",temp->data);
}
main()

Page 33 of 99
{
int option;
clrscr();
list=(struct node*)malloc(sizeof(struct node));
list->data=0;
list->next=NULL;
prev=list;
while(1)
{
printf("\n\tMAIN MENU\n1.Add\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&option);
switch(option)
{
case 1:add();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:return 0;
}
}
}
Output:
MAIN MENU
1.Add
2.Delete
3.Display

Page 34 of 99
4.Exit
Enter your choice:1

Enter the data to be added:5

MAIN MENU
1.Add
2.Delete
3.Display
4.Exit
Enter your choice:1

Enter the data to be added:6

MAIN MENU
1.Add
2.Delete
3.Display
4.Exit
Enter your choice:1

Enter the data to be added:4

MAIN MENU
1.Add
2.Delete
3.Display
4.Exit
Enter your choice:2
Enter the data to be removed 4

MAIN MENU
1.Add
2.Delete
3.Display
4.Exit
Enter your choice:3
5 6

MAIN MENU
1.Add
2.Delete
3.Display
4.Exit
Enter your choice:

Result:

Page 35 of 99
Thus the c program to implement stack using linked list is compiled and
executed successfully.

IV Queue Implementation:
1. Queue implementation using Array:
Aim:
To create a c program to implement a Queue data structure using array.
Algorithm:
Step 1: Start the program.
Step 2: Declare the variables and an array to perform queue operations.
Step 3: Read the maximum size of the queue n.
Step 4: Read the choice of operation.
Step 5: If the front of the queue is equal or rear of the queue is greater than the max
size n, print the queue is full otherwise add an element to the queue.
Step 6: If the front and the rear of the queue is equal, print the queue is empty
otherwise delete an element from the queue.
Step 7: If the front and the rear of the queue is equal, print the queue is empty
otherwise display the queue.
Step 8: If the front and the rear of the queue is equal, print the queue is empty
otherwise display the size of the queue(rear-front).
Step 9: Exit the queue.
Step 10: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int ch,n,queue[50],rear=0,front=0,i,item;
clrscr();

Page 36 of 99
printf("\n implementetion of queue using array");
do
{
printf("\n main menu");
printf("\n 1.queue max");
printf("\n 2.add");
printf("\n 3.delete");
printf("\n 4.display");
printf("\n 5.queue size");
printf("\n 6.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\n enter the queue size");
scanf("%d",&n);
break;
}
case 2:
{
if((front==n)||(rear>n))
printf("\n the queue is full");
else
{
printf("\n enter the element");
scanf("%d",&item);

Page 37 of 99
rear=rear+1;
queue[rear]=item;
printf("\n the element is added");
}
break;
}
case 3:
{
if(rear==front)
{
printf("\n the queue is empty");
}
else
{
front=front+1;
item=queue[front];
printf("\n the deleted element is %d",item);
}
break;
}
case 4:
{
if(rear==front)
printf("\n the queue is empty");
else
{
printf("the queue contains");
for(i=front+1;i<=rear;i++)

Page 38 of 99
{
printf("\t %d",queue[i]);
}
}
break;
}
case 5:
{
if(rear==front)
printf("\n the queue is empty");
else
printf("\n the size of queue is %d",(rear-front));
}
break;
case 6:
{
exit(1);
}
}
}
while(ch<7);
getch();
}
Output:
main menu
1.queue max
2.add
3.delete
4.display
5.queue size

Page 39 of 99
6.exit
enter your choice 2
enter the element 6
the element is added
main menu
1.queue max
2.add
3.delete
4.display
5.queue size
6.exit
enter your choice 2
enter the element 9
the element is added
main menu
1.queue max
2.add
3.delete
4.display
5.queue size
6.exit
enter your choice 3
the deleted element is 6
main menu
1.queue max
2.add
3.delete
4.display
5.queue size
6.exit
enter your choice 4
the queue contains 9
main menu
1.queue max
2.add
3.delete
4.display
5.queue size
6.exit
enter your choice:

Result:

Thus the c program to implement queue using array is compiled and executed
successfully.

Page 40 of 99
2. Queue implementation using Linked list:
Aim:
To create a c program to implement a Queue data structure using Linked List.
Algorithm:

Step 1: Start the program.


Step 2: Declare the Queue data structure using pointer.
Step 3: Create a node and read the first element along with the link to another
node.
Step 4: Read the element to be inserted by allocating memory. If the new
element is inserted, print The element is inserted .
Step 5: Perform delete operation and print the node is deleted.
Step 6: Display the elements in the list.
Step 7: Exit the Linked queue.
Step 8: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
#define NULL 0
struct node
{
int info;
struct node*next;
}*start,*ptr,*temp,*size;
void create();
void insert();
void delete();

Page 41 of 99
void display();
void main()
{
int n;
clrscr();
while(1)
{
printf("\nMAIN MENU\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit");
printf("\nEnter your choice");
scanf("%d",&n);
switch(n)
{
case 1:create();
break;
case 2:insert();
break;
case 3:delete();
break;
case 4:display();
break;
case 5:exit();
break;
default:printf("\nInvalid choice");
}
}
getch();
}
void create()

Page 42 of 99
{
int a;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the element:");
scanf("%d",&a);
temp->info=a;
temp->next=NULL;
if(start==NULL)
start=temp;else
{
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=temp;
}
}
void insert()
{
int b;
printf("\nEnter the element to be inserted:");
scanf("%d",&b);
temp=(struct node*)malloc(sizeof(struct node));
temp->info=b;
temp->next=NULL;
for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
ptr->next=temp;
printf("The element is inserted");
}
void delete()
{

Page 43 of 99
ptr=start;
start=ptr->next;
printf("\nThe node is deleted");
}
void display()
{
printf("\nThe element in the list are:");
for(ptr=start;ptr!=NULL;ptr=ptr->next)
printf("\n%d",ptr->info);
}
Output:
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice 1
Enter the element : 5

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice 2
Enter the element to be inserted: 5
The element is inserted
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice 2
Enter the element to be inserted: 6
The element is inserted
MAIN MENU

Page 44 of 99
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice3
The node is deleted
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice 4
The element in the list are:
5
6
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter your choice:

Result:

Thus the c program to implement queue using linked list is compiled and
executed successfully.

V Expression evaluation:

Aim:
To create a c-program to implement the evaluation of postfix expression.

Algorithm:

Step 1: Start the program


Step 2: Declare the variables

Page 45 of 99
Step 3: Read the expression.
Step 4: While the expression of the value expr_ind is not equal to zero, enters the
statement of If expression of expr_ind is greater than or equal to zero and
if expression of expr_ind is lesser than or equal to zero, increments the
value of the top.
Step 5: If the Step 4 fails, decrement the value of the top and store it.
Step 6: Read the current value of the top and store it.
Step 7: Evaluate the expression with the help of switch statements
Step 8: Print the value of the expression.
Step 9: Stop the program.

Program coding:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
char expr[100];
int expr_ind=0;
int stack[100];
int top=-1;
int a=0,b=0;
clrscr();
printf("\nEnter the expression:");
scanf("%s",expr);
while(expr[expr_ind]!='\0')
{
if (expr[expr_ind]>='0'&&expr[expr_ind]<='9')
{

Page 46 of 99
top++;
stack[top]=expr[expr_ind]-'0';
}
else
{
a=stack[top--];
b=stack[top];
switch(expr[expr_ind])
{
case'+':
stack[top]=a+b;
break;
case'-':
stack[top]=a-b;
break;
case'*':
stack[top]=a*b;
break;
case'/':
stack[top]=a/b;
break;
case'%':
stack[top]=a%b;
break;
}
}
expr_ind++;
}

Page 47 of 99
printf("\nThe value of expression is%d",stack[top]);
getch();
return 0;
}
Output:

Enter the expression:123*/


The value of expression is 6

Result:

Thus the c program to implement the evaluation of postfix expression is


compiled and executed successfully.

VI Polynomial addition:

Aim:
To write program to implement Polynomial Addition.
Algorithm:
Step 1 : Start the program.
Step 2 : Declare and Initialize the variables.
Step 3 : Read the number of terms in Polynomial A and B.
Step 4 : Read the coefficient and exponent of polynomial A and B.
Step 5 : Compare the exponent of each term in two polynomials ,
i. If the exponents are equal then add the coefficients and store it in
cin[].
ii. If the exponent of A polynomial is greater than the exponent of B
then store the coefficient in cin [].
iii. If the exponent of A polynomial is lesser than the exponent of B
then store the coefficient in cin [].
Step 6 : Print the sum of two polynomials.
Step 7 : Stop the program.

Page 48 of 99
Program coding:
#include<stdio.h>
#include<conio.h>
unsigned int enter(int size,int a[])
{
unsigned int temp=0;
unsigned int expo=0;
int i,exp,coef;
for(i=1;i<=size;i++)
{
printf("exponent and coefficient of %d term:",i);
scanf("%d%d",&exp,&coef);
a[exp]=coef;
expo=0;expo|=1;expo<<=exp;temp|=expo;
}
return temp;
}
void prin(int a[],unsigned int temp)
{
unsigned int test=0;
int j;j=15;
do
{
test=temp & 0x8000;
if(test!=0)
{
if(a[j]<0)

Page 49 of 99
{
printf("%d",a[j]);
printf("x^%d",j);
}
else
{
printf("+%d",a[j]);
printf("x^%d",j);
}
}
j--;
temp<<=1;
}
while(temp!=0);
printf("\n");
}
char compare(unsigned exp1,unsigned exp2)
{
char comp;
if(exp1==1&&exp2==1)
comp='=';
else if(exp1<exp2)
comp='<';
else
comp='>';
return comp;
}
void add(int ain[],int bin[],unsigned int exp1,unsigned int exp2,unsigned int exp3)

Page 50 of 99
{
unsigned int temp1=0;
unsigned int temp2=0;
unsigned int temp=0;
int j;
int cin[16];
char car,cn=0;
j=0;
temp=exp1|exp2;
while(exp1!=0||exp2!=0)
{
temp1=exp1&1;temp2=exp2&1;
car=compare(temp1,temp2);
switch(car)
{
case'<':
cin[cn]=bin[j];
cn++;
break;
case'>':
cin[cn]=ain[j];
cn++;
break;
case'=':
cin[cn]=ain[j]+bin[j];
cn++;
break;
}

Page 51 of 99
exp1>>=1;
exp2>>=1;
j++;
}
printf("\n The sum of the polynomials is:\n");
prin(cin,temp);
}
void main()
{
unsigned int expa=0;
unsigned int expb=0;
unsigned int expc=0;
int an,bn;
int a[16],b[16];
clrscr();
printf("\n no of terms in polynomial A is :");
scanf("%d",&an);
expa=enter(an,a);
printf("\n polynomial A :\n");
prin(a,expa);
printf("\n no of terms in polynomial B:");
scanf("%d",&bn);
expb=enter(bn,b);
printf("\n polynomial Bis:\n");
prin(b,expb);
add(a,b,expa,expb,expc);
getch();
}

Page 52 of 99
Output:

No of terms in polynomial A is :2
exponent and coefficient of 1 term:1
3
exponent and coefficient of 2 term:0
12

polynomial A :
+3x^1+12x^0

No of terms in polynomial B:2


exponent and coefficient of 1 term:1
5
exponent and coefficient of 2 term:0
12

polynomial Bis:
+5x^1+12x^0

The sum of the polynomials is:


+8x^1+24x^0

Result:

Thus the c program to implement the polynomial addition is compiled and


executed successfully.

VII Sparse matrix addition :

Aim:
To write a program to implement Sparse Matrix Addition.
Algorithm:
Step 1: Start the program.
Step 2: Declare the matrices with the number of rows and columns.
Step 3: Read the values of the elements of the matrix.
Step 4: Represent the matrix.

implement(a,row,col,b);

Page 53 of 99
Step 5: Read the values of the elements of next matrix to perform sparse matrix
manipulation.
Step 6: Perform Sparse matrix addition and print the resultant matrix.
Step 7: Exit the matrix addition operation.
Step 8: Stop the program

Program coding:
#include<stdio.h>
#include<conio.h>
void implement(int a[][10],int row,int col,int b[][10]);
void add(int b[][10],int row,int col,int d[][10],int e[][10]);
void main()
{
int a[10][10];
int b[10][10];
int c[10][10];
int d[10][10];
int e[50][10];
int i,row,col,k,l,ch;
char che;
clrscr();
printf("Enter the no. of rows\t:");
scanf("%d",&row);
printf("\n Enter the no. of columns\t:");
scanf("%d",&col);
printf("\n Enter the elements of sparse matrix\t:");
for(k=0;k<row;k++)
{

Page 54 of 99
for(l=0;l<col;l++)
{
printf("\n Element at[%d][%d]\t",k,l);
scanf("%d",&a[k][l]);
}
}
printf("Sparse matrix is\n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%d\t",a[k][l]);
}
printf("\n");
}
do
{
printf("\n CYNET MENU\n");
printf("\n 1.Representation\n");
printf("\n 2.Addition\n");
printf("\n 3.Exit\n");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
implement(a,row,col,b);
break;

Page 55 of 99
case 2:
printf("\n Enter the elements of second sparse matrix\t:");
for(k=0;k<col;k++)
{
for(l=0;l<col;l++)
{
printf("\n Element at[%d][%d]\t",k,l);
scanf("%d",&c[k][l]);
}
}
printf("Sparse matrix is\n");
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
printf("%d\t",c[k][l]);
}
printf("\n");
}
implement(a,row,col,b);
implement(c,row,col,d);
add(b,row,col,d,e);
break;
case 3:
exit();
default:
printf("You entered wrong choice\n");
}

Page 56 of 99
printf("Do you want to continue(Y/N):");
che=getche();
}
while(che=='y'||che=='Y');
getch();
}
void implement(int a[][10],int row,int col,int b[][10])
{
int g=1,nz=0,k,l;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{
nz=nz+1;
}
}
}
b[0][0]=row;
b[0][1]=col;
b[0][2]=nz;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
if(a[k][l]!=0)
{

Page 57 of 99
b[g][0]=k;
b[g][1]=l;
b[g][2]=a[k][l];
g++;
}
}
}
printf("Implementation of sparse matrix is:\n");
for(k=0;k<g;k++)
{
for(l=0;l<3;l++)
{
printf("%d\t",b[k][l]);
}
printf("\n");
}
}
void add(int b[][10],int row,int col,int d[][10],int e[][10])
{
int p1=1,p2=1,i=1;
int k,l;
if(b[0][0]!=d[0][0])
{
printf("Addition is not possible\n");
}
else
{
while(p1<=b[0][2] && p2<=d[0][2])

Page 58 of 99
{
if(b[p1][0]==d[p2][0])
{
if(b[p1][1]<d[p2][1])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][1]>d[p2][1])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
else if(b[p1][1]==d[p2][1])
{
e[i][0]=d[p1][0];
e[i][1]=d[p1][1];
e[i][2]=b[p1][2]+d[p2][2];
if(e[i][2]!=0)
{
i++;
}

Page 59 of 99
p1++;
p2++;
}
}
else if(b[p1][0]=d[p2][0])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];
i++;
p1++;
}
else if(b[p1][0]>d[p2][0])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
if(p1!=b[0][2])
{
while(p1<=b[0][2])
{
e[i][0]=b[p1][0];
e[i][1]=b[p1][1];
e[i][2]=b[p1][2];

Page 60 of 99
i++;
p1++;
}
}
else if(p2!=d[0][2])
{
while(p2<=d[0][2])
{
e[i][0]=d[p2][0];
e[i][1]=d[p2][1];
e[i][2]=d[p2][2];
i++;
p2++;
}
}
e[0][0]=row;
e[0][1]=col;
e[0][2]=i-1;
printf("Matrix after addition\n");
for(k=0;k<i;k++)
{
for(l=0;l<3;l++)
{
printf("%d\t",e[k][l]);
}
printf("\n");
}
}

Page 61 of 99
}

Output:

Enter the elements of sparse matrix :


Element at[0][0] 2

Element at[0][1] 0

Element at[1][0] 1

Element at[1][1] 6
Sparse matrix is
2 0
1 6

CYNET MENU

1.Representation

2.Addition

3.Exit

Enter your choice:2

Enter the elements of second sparse matrix :


Element at[0][0] 8

Element at[0][1] 0

Element at[1][0] 6

Element at[1][1] 1
Sparse matrix is
8 0
6 1
Implementation of sparse matrix is:
2 2 3
0 0 2
1 0 1

Page 62 of 99
1 1 6
Implementation of sparse matrix is:
2 2 3
0 0 8
1 0 6
1 1 1
Matrix after addition
2 2 3
0 0 10
1 0 7
1 1 7
Do you want to continue(Y/N):
Result:

Thus the c program to implement the sparse matrix addition is compiled and
executed successfully.

VIII Binary Tree representation and Traversal Techniques (In order,


Preorder, Post order)

Aim:
To write a program to implement Binary Tree Traversal.
Algorithm:

Step 1: Start the program.


Step 2: Implement the binary tree structure.
Step 3: Read the value of the root node.
newnode->data=y;
Step 4: Check if the left of the root node is empty, print invalid, else read the value
of the left node.
Step 5: Check if the right of the root node is empty, print invalid, else read the
value of the right node.

Step 6: To perform the preorder traversal, visit the root node, traverse the left
subtree and traverse the right subtree.
Step 7: To perform the inorder traversal, traverse the left
subtree, visit the root node and traverse the right subtree.

Page 63 of 99
Step 8: To perform the postorder traversal, traverse the left
subtree, traverse the right subtree and visit the root node.
Step 9: Exit the binary tree.
Step 10: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node*left;
struct node*right;
}tree;
tree*getnode();
void readnode(tree*);
void releasenode(tree*head);
tree*createbinarytree();
tree*insertnode(tree*btree,tree*temp);
void preorder(tree*btree);
void inorder(tree*btree);
void postorder(tree*btree);
tree*queue[30];
int front,rear;
int isempty();
void main()
{

Page 64 of 99
int choice;
tree*btree;
clrscr();
printf("\ncreate a new binary tree");
btree=createbinarytree();
printf("\n tree traversal");
printf("\n\t 1.preorder traversal");
printf("\n\t 2.in order traversal");
printf("\n\t 3.postorder traversal");
printf("\n ?");
scanf("%d",&choice);
if(btree==NULL)
{
printf("binary tree is empty");
return;
}
switch(choice)
{
case 1:
printf("preorder binary tree traversal is....\n");
preorder(btree);
break;
case 2:
printf("inorder binary tree traversl is....\n");
inorder(btree);
break;
case 3:
printf("post order binary tree traversal is...\n");

Page 65 of 99
postorder(btree);
break;
}
releasenode(btree);
}
tree*getnode()
{
int size;
tree*newnode;
size=sizeof(tree);
newnode=(tree*)malloc(size);
return(newnode);
}
void readnode(tree*newnode)
{
printf("\n enter the data:");
scanf("%d",&newnode->data);
newnode->left=NULL;
newnode->right=NULL;
}
void releasenode(tree*head)
{
free(head);
}
tree*createbinarytree()
{
char ch;
tree*btree=NULL,*temp;

Page 66 of 99
do
{
temp=getnode();
readnode(temp);
btree=insertnode(btree,temp);
fflush(stdin);
printf("do u wish to add in the tree(y/n?");
scanf("%c",&ch);
}
while((ch=='Y')||(ch=='y'));
return btree;
}
tree*insertnode(tree*btree,tree*temp)
{
if(btree==NULL)
return temp;
else if(temp->data<btree->data)
btree->left=insertnode(btree->left,temp);
else if(temp->data>btree->data)
btree->right=insertnode(btree->right,temp);
else if(temp->data==btree->data)
{
printf("\n data is already existing...");
return btree;
}
return btree;
}
void inorder(tree*btree)

Page 67 of 99
{
if(btree!=NULL)
{
inorder(btree->left);
printf("%d",btree->data);
inorder(btree->right);
}
}
void preorder(tree*btree)
{
if(btree!=NULL)
{
printf("%d",btree->data);
preorder(btree->left);
preorder(btree->right);
}
}
void postorder(tree*btree)
{
postorder(btree->left);
postorder(btree->right);
printf("%d",btree->data);
}
int isempty()
{
if(front==-1&&rear==-1)
return 1;
else

Page 68 of 99
return 0;
getch();
}
Output:
create a new binary tree
enter the data:5
do u wish to add in the tree(y/n)?Y

enter the data:6


do u wish to add in the tree(y/n)?Y

enter the data:4


do u wish to add in the tree(y/n)?N

tree traversal
1.preorder traversal
2.in order traversal
3.postorder traversal
?1
preorder binary tree traversal is....
546
Result:

Thus the c program to implement the binary tree traversal is compiled and
executed successfully.

IX Binary Search Tree

Aim:
To create a c program to create a binary search tree and insert elements, delete the
elements and find an element.
Algorithm:
Step 1: Start the program.
Step 2: Declare the structure for binary search tree.
Step 3: To insert an element,
a) Get the value to be inserted.
b) Insert the value in the order of left bottom to the right bottom of the tree.
c) Check each element with the root node for each time.

Page 69 of 99
d) If value is less than root insert it in the left position.
e) If value is greater than root insert it in the right.
Step 4: To perform deletion
a) Get the values to be deleted.
b) If it is leaf, delete is as such.
c) If it has one child, swap the pointers and delete the node.
d) If it has both left and right child take the minimum value at the right most
subtree and swap it with the node to be deleted.
e) Repeat the Process.
Step 5: To search an element
a) If root is NULL, return NULL.
b) If the value of the element is less than root search the element in the left
subtree.
c) If the value of the element is greater than root, search the element in the right
subtree
Step 6: Display all the elements of the tree.
Step 7: Exit the tree.
Step 8: Stop the Program.
Program coding:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node*left;
struct node*right;
}*root,*newnode,*temp;
void insert()
{

Page 70 of 99
int x;
printf("\nenter the data to insert:");
scanf("%d",&x);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
{
root=newnode;
return;
}
temp=root;
for(;;)
{
if(x<temp->data)
{
if(temp->left==NULL)
{
temp->left=(struct node*)malloc(sizeof(struct node));
temp->left->data=x;
temp->left->left=NULL;
temp->left->right=NULL;
break;
}
else
temp=temp->left;
}

Page 71 of 99
if(x>=temp->data)
{
if(temp->right==NULL)
{
temp->right=(struct node*)malloc(sizeof(struct node));
temp->right->data=x;
temp->right->left=NULL;
temp->right->right=NULL;
break;
}
else
temp=temp->right;
}}}
void inorder(struct node*root)
{
if(root!=NULL)
{
inorder(root->left);
printf("\n%d",root->data);
inorder(root->right);
}}
main()
{
int optain;
clrscr();
while(1)
{
printf("\n1.insert\n2.display\n3.exit\n");

Page 72 of 99
printf("\nenter your choice:");
scanf("%d",&optain);
switch(optain)
{
case 1:
insert();
break;
case 2:
inorder(root);
break;
case 3:
return 1;
}}}

Output:
1.insert
2.display
3.exit
enter your choice:1
enter the data to insert:5

1.insert
2.display
3.exit
enter your choice:1
enter the data to insert:6

1.insert
2.display
3.exit

enter your choice:1


enter the data to insert:7

Page 73 of 99
1.insert
2.display
3.exit

enter your choice:2


5
6
7
1.insert
2.display
3.exit
enter your choice:

Result:

Thus the c program to implement the binary search tree is compiled and
executed successfully.

X Graph Traversal:
1. Breadth First Search
Aim:
To write a program to implement the concepts of Breadth First Search.
Algorithm:

Step 1: Start the program


Step 2: Get the total number of nodes from the directed graph.
Step 3: Construct the adjacency matrix.
Step 4: Read the starting vertex and mark it as visited.
Step 5: Proceed visiting the next neighbour vertex adjacent to the starting vertex.
Step 6: Expand from source to its entire adjacent vertex.
Step 7: Backtracking is not possible in Breadth First Search.
Step 8: Repeat Step 5, 6 until visiting all the nodes
Step 9: Stop the program.
Program coding:

Page 74 of 99
#include<stdio.h>
#include<conio.h>
int bfs(int);
int i,j,k=0,n,v,w,u,adj[25][25],a[25],queue[25],visited[25];
int rear=-1,front=0;
void main()
{
clrscr();
printf("\t\t\tBreadth first search(BFS)");
printf("\n\nEnter the number of vertices: ");
scanf("%d",&n);
printf("\n\nEnter the cost of adjacent matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("\n\tcost[%d][%d]:",i,j);
scanf("%d",&adj[i][j]);
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n\npath:");
for(i=1;i<=n;i++)
{
printf("%d",a[i]);
if(i!=n)

Page 75 of 99
printf("-->");
}
getch();
}
int bfs(int v)
{
u=v;
visited[v]=1;
a[++k]=v;
do
{
for(w=1;w<=n;w++)
if((adj[u][w]==1)&&(visited[w]==0))
{
queue[++rear]=w;
visited[w]=1;
a[++k]=w;
}
if(rear<front)
return;
u=queue[front];
front++;
}
while(rear>=front);
return;
}
Output:
Breadth first search(BFS)

Page 76 of 99
Enter the number of vertices: 4
Enter the cost of adjacent matrix

cost[1][1]:0

cost[1][2]:1

cost[1][3]:1

cost[1][4]:1

cost[2][1]:0

cost[2][2]:0

cost[2][3]:0

cost[2][4]:1

cost[3][1]:0

cost[3][2]:0

cost[3][3]:0

cost[3][4]:0

cost[4][1]:0

cost[4][2]:0

cost[4][3]:1

cost[4][4]:0

Enter the starting vertex:1


path:1-->2-->3-->4

Result:

Thus the c program to implement the concept of breadth first search is compiled
and executed successfully.

2. Depth First Search:


Aim:

Page 77 of 99
To write a program to implement the concepts of Depth First Search.
Algorithm:

Step 1: Start the program


Step 2: Get the total number of nodes from the directed graph.
Step 3: Construct the adjacency matrix.
Step 4: Read the starting vertex and mark it as visited.
Step 5: Visit the nearby node and find whether it is marked or unmarked.
a) If marked, return to the previous node.
b) Mark the node as visited otherwise.

Step 6: If search is done in a path, backtracking is done-visiting the previous nodes


and continue the search in the next alternate path.
Step 7: Repeat Step 5, 6 until visiting all the nodes.
Step 8: Display all the visited nodes.
Step 9: Stop the program.
Program coding:
#include<stdio.h>
#include<conio.h>
int dfs(int);
int i,j,k=0,n,v,w,adj[25][25],a[25],stack[25],visited[25];
int top=-1;
void main()
{
clrscr();
printf("\t\t\tdepth first search(DFS)");
printf("\n\nEnter the number of vertices: ");
scanf("%d",&n);

Page 78 of 99
printf("\n\nEnter the cost of adjacent matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("\n\ncost[%d][%d]:",i,j);
scanf("%d",&adj[i][j]);
}
for(i=1;i<=n;i++)
{
visited[i]=0;
a[i]=0;
}
printf("\nEnter the starting vertex:");
scanf("%d",&v);
dfs(v);
printf("\n\npath:");
for(i=1;i<=n;i++)
{
printf("%d",a[i]);
if(i!=n)
printf("-->");
}
getch();
}
int dfs(int v)
{
int flag=0;
visited[v]=1;

Page 79 of 99
for(i=1;i<=n;i++)
{
if(a[i]==v)
{
flag=1;
break;
}
}
if(flag==0)
a[++k]=v;
for(w=1;w<=n;w++)
{
if((adj[v][w]==1)&&(visited[w]==0))
{
stack[++top]=v;
dfs(w);
}
if(top!=-1)
dfs(stack[top--]);
}
}

Output:
Enter the number of vertices: 4
Enter the cost of adjacent matrix

cost[1][1]:0
cost[1][2]:1
cost[1][3]:1

Page 80 of 99
cost[1][4]:1
cost[2][1]:0
cost[2][2]:0
cost[2][3]:0
cost[2][4]:1
cost[3][1]:0
cost[3][2]:0
cost[3][3]:0
cost[3][4]:0
cost[4][1]:0
cost[4][2]:0
cost[4][3]:1
cost[4][4]:0

Enter the starting vertex:1

path:1-->2-->3-->4

Result:

Thus the c program to implement the concept of depth first search is compiled
and executed successfully.

XI Dijkstras Algorithm (single source shortest path algorithm)


Aim:
To write a program to implement the concepts of Dijkstras Algorithm.
Algorithm:

Step 1: Start the program


Step 2: Get the total number of nodes from the directed graph.
Step 3: Construct the adjacency matrix.
Step 4: Use directed graph with weighted edge.
Step 5: Select source vertex and destination vertex.
Step 6: Find the minimum distance from source vertex to destination vertex
Step 7: Display the minimum vertex.
Step 8: Stop the program.

Program coding:

Page 81 of 99
#include<stdio.h>
#include<conio.h>
#define GRAPHSIZE 100
#define INFINITY GRAPHSIZE*GRAPHSIZE
#define MAX(a,b)((a>b)?(a):(b))
int e;
int n;
long dist[GRAPHSIZE][GRAPHSIZE];
long d[GRAPHSIZE];
void printD()
{
int i;
for(i=1;i<=n;++i)
printf("\t%d",i);
printf("\n");
for(i=1;i<=n;++i)
{
printf("\t%d",d[i]);
}
printf("\n");
}
void dijkstra(int s)
{
int i,k,mini;
int visited[GRAPHSIZE];
for(i=1;i<=n;++i)
{
d[i]=INFINITY;

Page 82 of 99
visited[i]=0;
}
d[s]=0;
for(k=1;k<=n;++k)
{
mini=-1;
for(i=1;i<=n;++i)
if(!visited[i]&&((mini==-1)||(d[i]<d[mini])))
mini=i;
visited[mini]=1;
for(i=1;i<=n;++i)
if(dist[mini][i])
if(d[mini]+dist[mini][i]<d[i])
d[i]=d[mini]+dist[mini][i];
}
}
int main()
{
int i,j;
int u,v,w;
clrscr();
printf("\n dijkstra algorithm\n");
printf("enter the no of edges");
scanf("%d",&e);
for(i=0;i<=e;++i)
for(j=0;j<e;j++)
dist[i][j]=0;
n=-1;

Page 83 of 99
for(i=0;i<e;++i)
{
printf("\nenter edge 'u':");
scanf("%d",&u);
printf("\nenter edge 'v':");
scanf("%d",&v);
printf("\n enter weight of%d%d:",u,v);
scanf("%d",&w);
dist[u][v]=w;
n=MAX(u,MAX(v,n));
}
dijkstra(1);
printD();
getch();
return(0);
}
Output:

enter the no of edges 5

enter edge 'u': 1


enter edge 'v':2
enter weight of12:120

enter edge 'u':2


enter edge 'v':3enter weight of23:26

enter edge 'u':3


enter edge 'v':4
enter weight of34:110

enter edge 'u':1


enter edge 'v':5
enter weight of15:160

Page 84 of 99
enter edge 'u':5
enter edge 'v':3
enter weight of53:20

1 2 3 4 5
0 120 146 256 160

Result:

Thus the c program to implement the concept of dijkstras algorithm is compiled


and executed successfully.

XIII AVL Tree

Aim:
To create a c-program to implement AVL tree using linked list method.

Algorithm:

Step 1: Declare the structure of binary search tree.


Step 2: Insert the element into the tree.
Step 3: Check whether the tree is empty.
a) Check whether the element is less than root element and then insert it to the
left.
b) If greater insert it to the right.
Step 4: To insert another element check the balance factor ( -1, 0, 1) of the tree
a) If the balance factor is -1, 0, 1 insert the element in the left if lesser or right if it
is greater.

Page 85 of 99
Step 5: If the element is inserted into the left child of the left sub tree of a node, perform
single rotation with the left.
Step 6: If the element is inserted into the right child of the right sub tree of a node,
perform single rotation with the right.
Step 7: If the element is inserted into the right child of the left sub tree of a node, perform
double rotation with the left.
Step 8: If the element is inserted into the left child of the right sub tree of a node, perform
double rotation with the right.
Step 9: Compute the balance factor along the ancestor path.
Step 10: Display the resultant height balanced tree.

Program coding:
#include<stdio.h>
#include<conio.h>
struct treenode;
typedef struct treenode *position,*avltree;
typedef int elementtype;
avltree makeempty(avltree t);
position find(elementtype x,avltree t);
avltree insert(elementtype x,avltree t);
struct treenode
{
elementtype element;
avltree left;
avltree right;
int height;
};

Page 86 of 99
int height(position p)
{
if(p!=NULL)
return p->height;
else
return 0;
}
int max(int h1,int h2)
{
if(h1>h2)
return h1;
else
return h2;
}
avltree makeempty(avltree t)
{
if(t!=NULL)
{
makeempty(t->left);
makeempty(t->right);
free(t);
}
return NULL;
}
position single_left(position k2)
{
position k1;
k1=k2->left;

Page 87 of 99
k2->left=k1->right;
k1->right=k2;
k2->height=max(height(k2->left),height(k2->right))+1;
k1->height=max(height(k1->left),height(k1->right))+1;
return k1;
}
position single_right(position k1)
{
position k2;
k2=k1->right;
k1->right=k2->left;
k2->left=k1;
k1->height=max(height(k2->left),height(k1->right))+1;
k2->height=max(height(k1->left),height(k2->right))+1;
return k2;
}
position double_left(position k3)
{
k3->left=single_right(k3->left);
return single_left(k3);
}
position double_right(position k3)
{
k3->right=single_left(k3->right);
return single_right(k3);
}
avltree insert(elementtype x,avltree t)
{

Page 88 of 99
if(t==NULL)
{
t=(avltree)malloc(sizeof(struct treenode));
if(t==NULL)
printf("out of space");
else
{
t->element=x;
t->left=NULL;
t->right=NULL;
t->height=0;
}}
else if(x<t->element)
{
t->left=insert(x,t->left);
if(height(t->left)-height(t->right)==2)
if(x<t->left->element)
t=single_left(t);
else
t=double_left(t);
}
else if(x>t->element)
{
t->right=insert(x,t->right);
if(height(t->right)-height(t->left)==2)
if(x>t->right->element)
t=single_right(t);
else

Page 89 of 99
t=double_right(t);
}
t->height=max(height(t->left),height(t->right))+1;
return t;
}
position find(elementtype x,avltree t)
{
if(t==NULL)
return NULL;
else if(x<t->element)
{
return find(x,t->left);
}
else if(x>t->element)
{
return find(x,t->right);
}
else
return t;
}
void inorder(avltree t)
{
if(t!=NULL)
{
inorder(t->left);
printf("\n%d",t->element);
inorder(t->right);
}}

Page 90 of 99
int main()
{
int opt,item;
avltree t;
position p;
t=NULL;
clrscr();
do
{
printf("\n main menu\n1.insert\n2.find\n3.display-inorder\n4.exit\n");
printf("\n enter your choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("enter the item to be inserted");
scanf("%d",&item);
t=insert(item,t);
printf("the item is inserted");
break;
case 2:
printf("enter the item to be found");
scanf("%d",&item);
p=find(item,t);
if(p)
printf("the item is found");
else
printf("the item is not found");

Page 91 of 99
break;
case 3:
if(t)
{
printf("the tree is");
inorder(t);
}
else
{
printf("the tree is empty");
}
break;
case 4:
exit(1);
}}
while(1);
}

Output:
main menu
1.insert
2.find
3.display-inorder
4.exit

Page 92 of 99
enter your choice: 1
enter the item to be inserted: 2
the item is inserted
main menu
1.insert
2.find
3.display-inorder
4.exit

enter your choice: 1


enter the item to be inserted: 6
the item is inserted
main menu
1.insert
2.find
3.display-inorder
4.exit

enter your choice: 1


enter the item to be inserted: 7
the item is inserted
main menu
1.insert
2.find
3.display-inorder
4.exit

enter your choice: 3


the tree is
2
6
7
main menu
1.insert
2.find
3.display-inorder
4.exit

enter your choice:

Result:

Thus the c program to implement the AVL tree using linked list method is
compiled and executed successfully.

XIV Hsashing technique:

Page 93 of 99
Aim:
To create a c program to implement a hashing and resolution technique.
Algorithm:

Program coding:
#include<stdio.h>
#include<conio.h>
int hash[10];
int initialise();
void insert(int);
int i;
int initialise()
{
printf("\n after initialisation\n");
for(i=0;i<10;i++)
{
hash[i]=-1;
}
return 0;
}
void insert(int key)
{
int a,b;
a=key%10;
b=a;
while(hash[a]!=-1&&((a=+1)%10!=b))
{

Page 94 of 99
a=(a+1)%10;
}
if(hash[a]==-1)
{
hash[a]=key;
printf("\n after insertion\n");
for(i=0;i<10;i++)
printf("%d\t%d\n",i,hash[i]);
}
else
return;
printf("hash table full\n");
}
void main()
{
int hash[10],count=0;
int m,k,x;
clrscr();
initialise();
while(count<10)
{
printf("\n enter the value to be insert");
scanf("%d",&m);
count++;
insert(m);
}
printf("HASH TABLE FULL\n");
}

Page 95 of 99
Output:
after initialisation

enter the value to be insert:


5

after insertion
0 -1
1 -1
2 -1
3 -1
4 -1
5 5
6 -1
7 -1
8 -1
9 -1

enter the value to be insert:


2

after insertion
0 -1
1 -1
2 2
3 -1
4 -1
5 5
6 -1
7 -1
8 -1
9 -1

enter the value to be insert:


6

after insertion
0 -1
1 -1
2 2
3 -1
4 -1
5 5
6 6
7 -1
8 -1
9 -1

Page 96 of 99
enter the value to be insert:

Result:

Thus the c program to implement a hashing and resolution technique is compiled


and executed successfully.

Page 97 of 99
Page 98 of 99
Page 99 of 99

Das könnte Ihnen auch gefallen