Ds&oops Lab Manual 2023
Ds&oops Lab Manual 2023
a LINEAR SEARCH
AIM:
To write a C Program to search the given element which is present in the list or not using
Linear Search.
ALGORITHM:
Step 1. Start
Step 2. Read the value of n
Step 3. for i=1 to n increment in steps of 1
Read the value of ith element into array
Step 4. Read the element(x) to be searched
Step 5. search<--linear(a,n,x)
Step 6. if search equal to 0 goto step 7 otherwise goto step 8
Step 7. print unsuccessful search
Step 8. print successful search
Step 9. stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,x,flag=0;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the numbers:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("Enter the number to be searched:");
scanf("%d",&x);
for(i=1;i<=n;i++)
{
if(x==a[i])
{
printf("The number %d is found in %d position",x,i);
flag=1;
break;
}
}
if(flag==0)
printf("The number is not found");
getch();
}
OUTPUT:
Enter the value of n : 6
Enter the number :
20
15
28
31
56
18
Enter the number to be searched : 15
The number is found in 2 position
Enter the number to be searched: 12
The number is not found
RESULT:
Thus the C Program to search the given element which is present in the list or not using Linear
Search is implemented and verified successfully.
EX NO : 1.b BINARY SEARCH
AIM:
To write a C Program to search the given element this is present in the list or not using
Binary Search.
ALGORITHM:
Step 1. Start
Step 2. Read the value of n
Step 3. for i=1 to n increment in steps of 1
Read the value of ith element into array
Step 4. Read the element(x) to be searched
Step 5. search<--binary(a,n,x)
Step 6. if search equal to 0 goto step 7 otherwise goto step 8
Step 7.print unsuccessful search
Step 8. print successful search
Step 9. stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],m,n,i,j,temp;
void binary(int a[100],int m,int n);
clrscr();
printf("Enter the size of array");
scanf("%d",&m);
printf("Enter array element");
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=m;i++)
{
for(j=i+1;j<=m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Sorted array");
for(i=1;i<=m;i++)
{
printf("\n%d",a[i]);
}
printf("\nEnter the search element");
scanf("%d",&n);
binary(a,m,n);
getch();
}
void binary(int a[100],int m,int n)
{
int low=1,high=m,mid,flag=0;
while(low<=high&&flag==0)
{
mid=(low+high)/2;
if(n==a[mid])
{
flag=1;
printf("The number %d is found in:%d",n,mid);
}
else if(n>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(flag==0)
{
printf("The number is not found");
}
}
OUTPUT:
Enter the size of array : 5
Enter array element :
31
15
20
18
10
Sorted array :
10
15
18
20
31
Enter the search element: 18
The number 18 is found in 3
AIM
To write a C Program to sort the given list of elements in increasing order using Insertion sort
Technique.
ALGORITHM
Step1. start
Step2. for i= 1 to n increment in steps of 1
begin
assign a[i] to temp
Step3. forj=i-1 down to j>=0 and temp<a[j]
begin
assign a[j] to a[j+1]
end inner for loop
Step4. assign temp to a[j+1]
end for loop
Step5. Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int n;
void main()
{
int i,a[10];
void insertion(int a[10]);
clrscr();
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Enter the number:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertion(a);
getch();
}
void insertion(int a[10])
{
int i,j,temp;
for(i=0;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while ((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("The sorted elements are:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
OUTPUT
Enter the number of elements : 4
Enter the elements :
23
45
56
32
The sorted elements are :
23
32
45
56
RESULT:
Thus C Program to sort the given list of elements in increasing order using Insertion sort
Technique is implemented and verified successfully.
EX NO : 2.b SELECTION SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Selection sort
Technique.
ALGORITHM
Step1. start
Step2. for i= 1 to n increment in steps of 1
begin
assign min = a[i]
Step3. forj=i+1down to j<=n and a[j]<min
begin
assign min = a[j]
end inner for loop
Step4. assign a[k] to a[i]
end for loop
Step5. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],min,k=0,n,i,j;
clrscr();
printf("\nEnter the number of elements:");
scanf("%d",&n);
printf("Enter the number:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
{
min=a[i];
k=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 elements are:");
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();
}
OUTPUT:
Enter the number of elements : 4
Enter the elements :
23
45
56
32
The sorted elements are :
23
32
45
56
RESULT:
Thus C Program to sort the given list of elements in increasing order using Selection sort
Technique is implemented and verified successfully.
EX NO : 2.c BUBBLE SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Bubble
sort Technique.
ALGORITHM
Step 1. start
Step 2. read the value of n
Step 3. for i= 1 to n increment in steps of 1
Read the value of ith element into array
Step 4. call function to sort (bubble_sort(a,n))
Step 5. for i= 1 to n increment in steps of 1
print the value of ith element in the array
Step 6. stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int n;
void main()
{
int i,a[10];
void bubble(int a[10]);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a);
getch();
}
void bubble(int a[10])
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
OUTPUT:
RESULT:
Thus C Program to sort the given list of elements in increasing order using Bubble sort
Technique is implemented and verified successfully.
EX NO : 2.d SHELL SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Shell sort
Technique.
ALGORITHM
Step1. start
Step2. for k= n/2 to k/2 increment in steps of 1
begin
Step3. for i=k down to i<N and increment i value
begin
assign temp=a[i]
Step4.for j=i to (j>=k && a[j-k]>temp) to j=j-k
begin
assign a[j] = a[j-k]
end Inner for loop
else assign a[j]=temp
end for loop
Step5. Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int n;
void shell(int a[10],int n);
void main()
{
int i,a[10];
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell(a,n);
printf("\n The sorted elements are:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void shell(int a[10],int n)
{
int k,i,j,temp;
for(k=n/2;k>0;k=k/2)
for(i=k;i<n;i++)
{
temp=a[i];
for(j=i;(j>=k)&&(a[j-k]>temp);j=j-k)
{
a[j]=a[j-k];
}
a[j]=temp;
}
}
OUTPUT
RESULT:
Thus C Program to sort the given list of elements in increasing order using Shell sort Technique is
implemented and verified successfully.
EX NO : 2.e QUICK SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Quick
sort Technique.
ALGORITHM:
Step 1. start
Step 2. if lowerbound < upperbound repeat through steps 3 to 13 otherwise
goto step 14
begin
Step 3. assign lowerbound to i,upperbound to j, i to pivot
Step 4. if i<j repeat through steps 5 to 10 otherwise goto step2
Begin
Step 5. if a[i]<=a[pivot] and i< upperbound repeat through step 6 otherwise
goto step 7
begin
Step 6. assign i+1 to i
end if
Step 7. if a[j] > a[pivot] repeat through step 8 otherwise goto step 9
begin
Step 8. assign j-1 to j
end if
Step 9. if i< j goto step 10 other wise goto step 4
Begin
Step10.call function to swap k[i] and k[j]
end if
end if
Step 11.call function to swap k[pivot] and k[j]
Step 12.call function qsort(x,lowerbound,j-1)
Step 13. call function qsort(x,j+1,upperbound)
end if
Step14. stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n;
void quick(int a[10], int low,int high);
int partition(int a[10],int low, int high);
void swap(int a[10],int *low, int *high);
void main()
{
int i,a[10];
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quick(a,0,n-1);
printf("\n The sorted elements are:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void quick(int a[10],int low,int high)
{
int m,i;
if(low<high)
{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
int partition(int a[10],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
{
j--;
}
if(i<=j)
swap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[10],int*i,int*j)
{
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}
OUTPUT
RESULT:
Thus C Program to sort the given list of elements in increasing order using Quick sort
Technique is implemented and verified successfully.
EX NO : 2.f MERGE SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Merge sort
Technique.
Algorithm:
MAIN PROGRAM
Step1: Start
Step2: declare the merge sort function
Step3: Declare the array and their size and initailaze the j=0
Step4: read the array elements and then sort these elements.
Step5: read the array elements before the merge sort and then display the
elements.
Step6: call the merge sort function
Step7: display the array elements after merge sort by using the following stament.
for( j=0;j<Max_ary;j++)
Step8: Stop
SUBPROGRAM
Step1:initialize the array excuting[MAX_ARY] and
j=0,mid=0,mrg1=0,mrg2=0,size=start-end+1
Step2: check the condition if(end==start) then return
Step3: calculate the mid value
Mid=(end+start)/2
Step4: call themerge_sort(a,end,mid)
Step5:merge_sort(a,mid+1,start)
Step6: performing the looping operation
For(j=0;j<SIZE;j++) then its true
Executing[j]=a[end+1]
Mrg1=0;
Step7: calculate the mrg2=mid-end+1
Step8: performing the looping operation
For(j=0;j<SIZE;j++) then its true then goto step9
Step9: check the condition
i) if(mrg2<=start-end) is true goto ii). If not goto Step12.
ii) If(mrg1<=mid-end) is true goto iii). If not goto step11
iii) If(executing[mrg1]>executing[mrg2]) is true then follows. If not goto step10.
a[j+end]= executing[mrg2++]
Step10: a[j+end]=executing[mrg1++]. If not goto Step11
Step11: a[j+end]= executing[mrg2++]
Step12: a[j+end]=executing[mrg1++]
Step13: return to main program
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int n;
void merge(int a[30],int low,int high);
int combine(int a[30],int low,int mid,int high);
void main()
{
int i,a[30];
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
merge(a,0,n-1);
printf("The sorted elements are:");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void merge(int a[30],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge(a,low,mid);
merge(a,mid+1,high);
combine(a,low,mid,high);
}
}
int combine(int a[30],int low,int mid,int high)
{
int k=low,i=low,j=mid+1;
int temp[30];
while((i<=mid) && (j<=high))
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)
{
temp[k]=a[j];
j++;
k++;
}
for(i=low;i<=high;i++)
a[i]=temp[i];
}
OUTPUT
RESULT:
Thus C Program to sort the given list of elements in increasing order using Merge sort
Technique is implemented and verified successfully.
EX NO : 2.g RADIX SORT
AIM
To write a C Program to sort the given list of elements in increasing order using Radix sort
Technique.
ALGORITHM
Step1: Start
Step2: Initialize all values to 0
Step3: Declare the array and their size and initailaze the i=0
Step4: read the array elements and then sort these elements.
Step5: read the array elements before the radix sort and then display the
elements.
Step6: Use the temp variable sort the values. Display the array elements after radix sort by
using the following statement.
for (j = 0; j < count; j++)
Step7: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int min = 0, count = 0, array[100] = {0}, array1[100] = {0};
void main()
{
int k, i, j, temp, t, n;
printf("Enter the number of elements :");
scanf("%d", &count);
printf("Enter the elements :");
for (i = 0; i < count; i++)
{
scanf("%d", &array[i]);
array1[i] = array[i];
}
for (k = 0; k < 3; k++)
{
for (i = 0; i < count; i++)
{
min = array[i] % 10; /* To find minimum lsd */
t = i;
for (j = i + 1; j < count; j++)
{
if (min > (array[j] % 10))
{
min = array[j] % 10;
t = j;
}
}
temp = array1[t];
array1[t] = array1[i];
array1[i] = temp;
temp = array[t];
array[t] = array[i];
array[i] = temp;
}
for (j = 0; j < count; j++) /*to find MSB */
array[j] = array[j] / 10;
}
printf("The sorted elements are: ");
for (i = 0; i < count; i++)
printf("%d ", array1[i]);
getch();
}
OUTPUT
RESULT:
Thus C Program to sort the given list of elements in increasing order using Radix
sort Technique is implemented and verified successfully.
EX NO : 3.a SINGLY LINKED LIST
AIM
To write a C Program that uses functions to perform the following operations on single linked
lists
ALGORITHM
Step 1: Start
Step 2: Declare the variables and functions
Step 3: Using While loop display options: Insertion, Add at the beginning, Add after, Deletion,
Display, Count, Reverse, Search,Quit
Step 4: Read the option using switch statement then the particular function will be done
Step 4: Stop
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void create_list(int);
void addatbeg(int);
void addafter(int,int);
void del(int);
void display();
void rev();
void search(int);
void count();
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int choice=0,n,m,position,i;
start=NULL;
clrscr();
while(1)
{
printf("\n1.INSERTION");
printf("\n2.ADD AT BEGINNING");
printf("\n3.ADD AFTER");
printf("\n4.DELETION");
printf("\n5.DISPLAY");
printf("\n6.COUNT");
printf("\n7.REVERE");
printf("\n8.SEARCH");
printf("\n9.QUIT");
printf("\nENTER THE CHOICE...");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nNODES YOU NEED...");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE ELEMENT...");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("\nENTER THE ELEMENT...");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("\nENTER THE ELEMENT...");
scanf("%d",&m);
printf("\nENTER POSTION TO INSERT...");
scanf("%d",&position);
addafter(m,position);
break;
case 4:
if(start==NULL)
{
printf("\nLIST IS EMPTY");
continue;
}
printf("\nENTER THE ELEMENT FOR DELETION...");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
count();
break;
case 7:
rev();
break;
case 8:
printf("\nENTER THE ELEMENT TO BE SEARCHED...");
scanf("%d",&m);
search(m);
break;
case 9:
exit(0);
default:
printf("\nTHE INVALID CHOICE");
}
}
}
void create_list(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
{
start=temp;
}
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
void addatbeg(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}
void addafter(int data,int pos)
{
struct node *q,*temp;
int i;
q=start;
for(i=0;i<(pos-1);i++)
{
q=q->link;
if(q==NULL)
{
printf("\nTHERE ARE LESS THAN %d ELEMENT.",pos);
return;
}
}
temp=malloc(sizeof(struct node));
temp->link=q->link;
temp->info=data;
q->link=temp;
}
void del(int data)
{
struct node *temp,*q;
if(start->info==data)
{
temp=start;
start=start->link;
free(temp);
return;
}
q=start;
while(q->link->link!='\0')
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
return;
}
q=q->link;
}
if(q->link->info==data)
{
temp=q->link;
free(temp);
q->link='\0';
return;
}
printf("\nELEMENT %d NOT FOUND.",data);
}
void display()
{
struct node *q;
if(start=='\0')
{
printf("\nTHE LIST IS EMPTY");
return;
}
q=start;
while(q!='\0')
{
printf("\nTHE ELEMENT IS...%d",q->info);
q=q->link;
}
printf("\n");
}
void count()
{
struct node *q=start;
int cnt=0;
while(q!='\0')
{
q=q->link;
cnt++;
}
printf("\nNUMBER OF ELEMENT ARE %d",cnt);
}
void rev()
{
struct node *p1,*p2,*p3;
if(start->link=='\0')
{
return;
}
p1=start;
p2=p1->link;
p3=p2->link;
p1->link='\0';
p2->link=p1;
while(p3!='\0')
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
printf("\nTO CHECK REVERSED LIST PLEASE CHOOSE OPTION 5");
}
void search(int data)
{
struct node *ptr=start;
int pos=1;
while(ptr!='\0')
{
if(ptr->info==data)
{
printf("\nITEM %d FOUND AT POSITON...%d",data,pos);
return;
}
ptr=ptr->link;
pos++;
}
if(ptr=='\0')
{
printf("\nITEM %d NOT FOUND IN LIST",data);
}
}
OUTPUT:
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….1
NODES YOU NEED…3
ENTER THE ELEMET…32
ENTER THE ELEMET…5
ENTER THE ELEMET…7
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….2
ENTER THE ELEMET…3
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….3
ENTER THE ELEMET…78
ENTER POSITION TO INSERT…2
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….5
ELEMENT IS…3
ELEMENT IS…32
ELEMENT IS…78
ELEMENT IS…5
ELEMENT IS…7
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….4
ENTER THE ELEMENT FOR DELETION…32
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….6
NUMBER OF ELEMENTS ARE…4
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….7
ELEMENT IS…7
ELEMENT IS…5
ELEMENT IS…78
ELEMENT IS…3
1.INSERTION
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….8
ENTER ELEMENT TO BE SEARCHED…78
ITEM 78 FOUND AT POSITION 3
RESULT:
Thus the C program to perform Single linked list is implemented and verified successfully.
EX NO : 3.b DOUBLY LINKED LIST
AIM
To write a C Program that uses functions to perform the operations on doubly linked lists
DESCRIPTION
In this program we have to create a doubly linked list, insert the elements in to a doubly
linked list, delete the elements from that list and finally perform the traversal operation
ALGORITHM
Step 1: Start
Step 2: Declare the variables and functions
Step 3: Using While loop display options:Create a list, Add at the beginning, Add after, Deletion,
Display, Count, Reverse, Search,Quit
Step 4: Read the option using switch statement then the particular function will be done
Step 4: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create_list(int);
void addatbeg(int);
void addafter(int,int);
void del(int);
void display();
void rev();
void count();
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
void main()
{
int choice,n,m,po,i;
clrscr();
start=NULL;
while(1)
{
printf("\n 1.CREATE A LIST");
printf("\n 2.ADD AT BEGINNING");
printf("\n 3.ADD AFTER");
printf("\n 4.DELETE");
printf("\n 5.DISPLAY");
printf("\n 6.COUNT");
printf("\n 7.REVERSE");
printf("\n 8.QUIT");
printf("\nENTER YOUR CHOICE...");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER NODES YOU NEED...");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE ELEMENT...");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("\nENTER THE ELEMENT...");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
OUTPUT:
1.CREATE A LIST
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….1
ENTER NODES YOU NEED… 2
ENTER THE ELEMENT…12
ENTER THE ELEMENT 43
1.CREATE A LIST
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….2
ENTER THE ELEMENT…4
1.CREATE A LIST
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….3
ENTER THE ELEMENT…23
ENTER THE POSITION…2
1.CREATE A LIST
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….5
LIST IS….43 23 12 4
1.CREATE A LIST
2.ADD AT BEGINNING
3.ADD AFTER
4.DELETION
5.DISPLAY
6.COUNT
7.REVERSE
8.SEARCH
9.QUIT
ENTER THE CHOICE….
LIST IS….4 12 23 43
RESULT:
Thus the C program to perform Double linked list is implemented and verified successfully.
EX NO : 4.a STACK IMPLEMENTATION
AIM
To write a C Program for stack implementation.
ALGORITHM
Step 1: Start the Program
Step 2: Using Switch case perform operation like create, push, pop, display
Step 3: In push operation, element is inserted , top value is incremented and value is inserted
Step 4: In pop operation, element is deleted , top value is decremented and value is deleted
Step 5: All the operation is viewed using display using display function
Step 6: Stop the Program
PROGRAM
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int front=-1,ch,stack[SIZE];
int push();
int pop();
int display();
void main()
{
clrscr();
while(1)
{
printf("\n\t\t\t\tTHE OPERATION OF STACK");
printf("\n1.)PUSH");
printf("\n2.)POP");
printf("\n3.)DISPLAY");
printf("\n4.)EXIT");
printf("\nENTER THE CHOICE...");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nTHE INVALID CHOICE");
}
}
getch();
}
int push()
{
int stack_item;
if(front==(SIZE-1))
{
printf("\nTHE STACK IS OVERFLOW");
}
else
{
printf("\nENTER THE STACK ELEMENT...");
scanf("%d",&stack_item);
front++;
stack[front]=stack_item;
}
}
int pop()
{
if(front==-1)
{
printf("\nTHE STACK IS UNDERFLOW");
}
else
{
printf("\nTHE STACK ELEMENT %d IS DELETED",stack[front]);
front--;
}
}
int display()
{
int i;
if(front==-1)
{
printf("\nTHE STACK IS UNDERFLOW");
}
else
{
for(i=front;i>=0;i--)
{
printf("\nTHE ELEMENT PRESENT...%d",stack[front]);
}
}}
OUTPUT:
RESULT:
AIM
To write a C Program that uses the stack operation to convert infix expression into postfix expression
ALGORITHM:
Step 1. start
Step 2. first initialize the stack to be empty
Step 3. for each character in the input string
If input string is an operand, append to the output
if the input string is a left paranthesis , push it onto the stack
else
if stack is empty or the operator has higher priority than the operator on
the topof stack or
the top of the stack is opening parenthesis
then
push the operator onto the stack
else
pop the operator from the stack and append to the output
Step 4. if the input string is a closing parenthesis , pop operators from the stack
and append the operators
to the output until an opening parenthesis is encountered. Pop the
opening parenthesis from the stack
and discard it.
Step 5. if the end of the input string is encountered , then iterate the loop until the
stack is not empty. Pop
the stack and append the remaining input string to the output.
Step 6. stop
PROGRAM
#include<stdio.h>
#include<ctype.h>
#include<string.h>
static char str[20];
int top=-1;
main()
{
char in[20],post[20],ch;
int i,j,l;
clrscr();
printf("enter the string");
gets(in);
l=strlen(in);
for(i=0,j=0;i<l;i++)
if(isalpha(in[i]))
post[j++]=in[i];
else
{
if(in[i]=='(')
push(in[i]);
else if(in[i]==')')
while((ch=pop())!='(')
post[j++]=ch;
else
{
while(priority(in[i])<=priority(str[top]))
post[j++]=pop();
push(in[i]);
}
}
while(top!=-1)
post[j++]=pop();
post[j]='\0';
printf("\n equivalent infix to postfix is:%s",post);
getch();
}
priority (char c)
{
switch(c)
{
case'+':
case'-': return 1;
case'*':
case'/':
return 2;
case'$': return 3;
}
return 0;
}
push(char c)
{
str[++top]=c;
}
pop()
{
return(str[top--]);
}
OUTPUT
AIM
To write a C Program to
i) create a binary tree of integers
ii) Traversing the above binary tree in preorder, inorder and post order
ALGORITHM
Step 1: Start
Step 2: Declare the variable need and the structure of node
Step 3: Get the node number of node n in tree and the item are inserted for n times using the
given function
Step 4: Call the function inorder, preorder,postorder to performs respective functions
Step 5: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *right,*left;
}*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=y;
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r,int x)
{
if(r->left!=NULL)
{
printf("\nINVALID");
}
else
{
r->left=make(x);
}
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
{
printf("\nINVALID");
}
else
r->right=make(x);
}
OUTPUT:
RESULT:
Thus the C program for tree traversal has been implemented and verified successfully.
EX NO : 6.a GRAPH TRAVERSAL - BREADTH FIRST SEARCH
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
# define MAX 10
# define TRUE 1
# define FALSE 0
typedef struct node
{
int vertex;
struct node *next;
}node;
node *head[10];
int visited[MAX];
int Queue[MAX];
int front,rear;
int n;
void add(int vertex1, int vertex2)
{
node *New, *first;
New=(node *)malloc(sizeof(node));
if(New==NULL)
printf("\n Insuffucient memory");
New->vertex=vertex2;
New->next=NULL;
first=head[vertex1];
if(first==NULL)
head[vertex1]=New;
else
{
while(first->next!=NULL)
first=first->next;
first->next=New;
}
New=(node *)malloc(sizeof(node));
if(New==NULL)
printf("\n Insufficient memory");
New->vertex=vertex1;
New->next=NULL;
first=head[vertex2];
if(first==NULL)
head[vertex2]=New;
else
{
while(first->next!=NULL)
first=first->next;
first->next=New;
}
}
void create()
{
int V1,V2;
char ans='y';
for(V1=0;V1<MAX;V1++)
head[V1]=NULL;
printf("\n enter the vertices no. beginning witn 0");
do
{
printf("\n Enter the edge of the graph");
scanf("%d %d",&V1,&V2);
if(V1>=MAX || V2>=MAX)
printf("\n Invalid Vertex value");
else
add(V1,V2);
printf("\n Want to add more edges(y/n)");
ans=getche();
}while(ans=='y');
}
void bfs(int V1)
{
int i;
node *first;
front=-1;
rear=-1;
Queue[++rear]=V1;
while(front!=rear)
{
i=Queue[++front];
if(visited[i]==FALSE)
{
printf("\n %d",i);
visited[i]=TRUE;
}
first=head[i];
while(first!=NULL)
{
if(visited[first->vertex]==FALSE)
Queue[++rear]=first->vertex;
first=first->next;
}}}
void main()
{
int V1,V2;
char ans;
clrscr();
create();
do
{
for(V1=0;V1<n;V1++)
visited[V1]=FALSE;
clrscr();
printf("\n Enter the vertex from which u want to traverse");
scanf("%d",&V1);
if(V1>=MAX)
printf("\n Invalid vertex");
else
{
printf("\n The breadth first search of the Graph is\n");
front=rear=-1;
bfs(V1);
getch();
}
printf("\n Do u want to continue");
ans=getche();
}
while(ans=='y');
exit(0);
}
OUTPUT
RESULT:
Thus the C program of Breadth First Search is implemented and verified successfully.
EX NO : 6.b GRAPH TRAVERSAL - DEPTH FIRST SEARCH
AIM
ALGORITHM
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
# define MAX 10
# define TRUE 1
# define FALSE 0
typedef struct node
{
int vertex;
struct node *next;
}node;
node *head[10];
int visited[MAX];
int Queue[MAX];
int front,rear;
int n;
void add(int vertex1, int vertex2)
{
node *New, *first;
New=(node *)malloc(sizeof(node));
if(New==NULL)
printf("\n Insuffucient memory");
New->vertex=vertex2;
New->next=NULL;
first=head[vertex1];
if(first==NULL)
head[vertex1]=New;
else
{
while(first->next!=NULL)
first=first->next;
first->next=New;
}
New=(node *)malloc(sizeof(node));
if(New==NULL)
printf("\n Insufficient memory");
New->vertex=vertex1;
New->next=NULL;
first=head[vertex2];
if(first==NULL)
head[vertex2]=New;
else
{
while(first->next!=NULL)
first=first->next;
first->next=New;
}
}
void create()
{
int V1,V2;
char ans='y';
for(V1=0;V1<MAX;V1++)
head[V1]=NULL;
printf("\n enter the vertices no. beginning witn 0");
do
{
printf("\n Enter the edge of the graph");
scanf("%d %d",&V1,&V2);
if(V1>=MAX || V2>=MAX)
printf("\n Invalid Vertex value");
else
add(V1,V2);
printf("\n Want to add more edges(y/n)");
ans=getche();
}while(ans=='y');
}
void dfs(int V1)
{
int V2;
node *first;
printf("\n %d",V1);
visited[V1]=TRUE;
first=head[V1];
while(first!=NULL)
if(visited[first->vertex]==FALSE)
dfs(first->vertex);
else
first=first->next;
}
void main()
{
int V1,V2;
char ans='y';
clrscr();
create();
do
{
for(V1=0;V1<n;V1++)
visited[V1]=FALSE;
clrscr();
printf("\n Enter the vertex from which u want to traverse");
scanf("%d",&V1);
if(V1>=MAX)
printf("\n Invalid vertex");
else
{
printf("\n The depth first search of the Graph is\n");
dfs(V1);
getch();
}
printf("\n Do u want to continue");
ans=getche();
}
while(ans=='y');
exit(0);
}
OUTPUT
0
1
3
2
Do u want to continue
RESULT:
Thus the C program of Depth First Search is implemented and verified successfully.
EX NO : 7 SPANNING TREE - PRIMS ALGORITHM
AIM
To write a C program to implement the concept of spanning tree using Prims Algorithm
ALGORITHM
Step 1: Start the program
Step 2: Enter the number of nodes
Step 3: Enter the adjacent nodes enter the cost of the each path
Step 4: Take one node as source check adjacent node of source node
Step 5: Take minimum cost of all adjacent nodes and take that node as source
Step 6: Take minimum cost of all adjacent nodes until all the nodes have been visited
Step 7: Finally connect all the maximum cost path and form spanning tree
Step 8: Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\nEnter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");while(ne < n) {
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
OUTPUT
RESULT:
Thus the C program for prims algorithm is implemented and verified successfully.
EX NO : 8 SHORTEST PATH - DIJKSTRA’S ALGORITHM
AIM
To write a c program to perform shortest path algorithm for a graph using dijkstra’s
algorithm.
ALGORITHM
Step1: Start
Step 2: set for loop for i, j upto n
Step 3: assign cost[i][j]=infinity,dist[i] = INFINITY,
visit[i] = 0;
Step 4: Check if(visit[i] == 0 &&dist[i] < weight)
assign weight = dist[i], node=i
Step 5: Check if(cost[u][v] == INFINITY)
Step 6: alt = dist[u] + cost[u][v]
Step 7: check if(alt <dist[v]), assign dist[v] = alt
Step 8: Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define INFINITY 1000
int n,cost[10][10],dist[10],visit[10],front,rear,v;
void Initial(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cost[i][j] = INFINITY;
}
}
for(i=1;i<=n;i++)
{
dist[i] = INFINITY;
visit[i] = 0;
}
}
int nearest()
{
int i,node, weight;
weight = INFINITY;
for(i=1;i<=n;i++)
{
if(visit[i] == 0 &&dist[i] < weight)
{
weight = dist[i];
node=i;
}
}
return node;
}
void Dijkstra(int v)
{
int i,u,q,alt;
dist[v]=0;
for(i=1;i<=n;i++)
{
u=nearest();
for(v=1;v<=n;v++)
{
if(cost[u][v] == INFINITY)
{
continue;
}
alt = dist[u] + cost[u][v];
if(alt <dist[v])
{
dist[v] = alt;
}
}
visit[u] = 1;
}
}
void display()
{
int i,j;
printf("\nCOST MATRIX\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",cost[i][j]);
}
printf("\n");
}
}
void main()
{
int i,j,k;
clrscr();
printf("ENTER THE NUMBER OF VERTCIES...");
scanf("%d",&n);
Initial(n);
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
printf("ENTER THE SHORTEST PATH BETWEEN %d AND %d",i,j);
printf("\nIF EDGE NOT EXIST ENTER 1000...");
scanf("%d",&cost[i][j]);
cost[j][i] = cost[i][j];
}
}
display();
printf("\nENTER THE SOURCE VERTEX...");
scanf("%d",&v);
Dijkstra(v);
printf("\nRESULT\n");
for(i=1;i<=n;i++)
{
printf("\nDISTANCE OF %d IS...%d",i,dist[i]);
}
getch();
}
OUTPUT:
RESULT:
Thus the c program to perform shortest path algorithm for a graph using dijkstra’s algorithm is
done and verified successfully.
EX NO : 9 CONSTRUCTOR AND DESTRUCTOR
AIM
AIM
PROGRAM
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Humen Resource Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitablity Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<
<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
OUTPUT
RESULT
Thus the C++ program of Single Inheritance is implemented and verified successfully.
EX NO : 10.b MULTIPLE INHERITANCE
AIM
AIM
AIM
OUTPUT
RESULT
Thus the C++ program of hybrid inheritance is implemented and verified successfully.
EX NO : 11 VIRTUAL FUNCTION
AIM
PROGRAM
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show";
}
void display()
{
cout<<"\n Base class display" ;
}};
class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display";
}
void show()
{
cout<<"\n Drive class show";
}};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
OUTPUT
RESULT
Thus the C++ program of Virtual function is implemented and verified successfully.
EX NO : 12 TEMPLATE
AIM
PROGRAM
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}
void main()
{
int a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
OUTPUT
RESULT
Thus the C++ program of template is implemented and verified successfully.
EX NO : 13 EXCEPTION HANDLING
AIM
PROGRAM
#include<iostream.h>
#include<conio.h>
#include<exception.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the values of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"Answer is infinite because a-b is:"<<i;
}
getch();
}
OUTPUT
Enter the value for a: 20
Enter the value for b:20
Enter the value for c: 40
Answer is infinite because a-b is :0
RESULT
Thus the C++ program of exception handling is implemented and verified successfully.
EX NO : 14 QUEUE
AIM
PROGRAM
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int remove()
{
int x;
if(front == NULL)
{
cout<<"empty queue\n";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}
int main()
{
int n,c = 0,x;
clrscr();
cout<<"Enter the number of values to be pushed into queue\n";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queue\n";
cin>>x;
push(x);
c++;
}
cout<<"\n\nRemoved Values\n\n";
while(1)
{
if (front != NULL)
cout<<remove()<<endl;
else
break;
}
getch();
}
OUTPUT
RESULT
Thus the C++ program of queue is implemented and verified successfully.