Data Structure and Algorithm Lab Manual
Data Structure and Algorithm Lab Manual
Department of MCA
Data Structures with Algorithms Lab (20MCA16)
1. GENERAL INFORMATION:
2. FACULTY DETAILS:
DETAILS
Department MCA
Program List
Program 1
1.Write a C program to Implement the following searching techniques
a. Linear Search
#include<stdio.h>
int main ()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?\n");
scanf("%d",&n);
printf("Enter %d elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Enter element to search:\n");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch();
return 0;
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
1.b.Binary Search
#include<stdio.h>
#include<conio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
clrscr();
printf("Enter number of elements \n");
scanf("%d",&n);
printf("Enter %d integers in ascending order\n", n);
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
printf("Enter value to find\n");
Data Structures with Algorithms Lab 20MCA16
scanf("%d", &key);
low = 0; high =n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key)
{
printf("%d found at location %d\n", key, mid);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list\n", key);
getch();
return 0;
}
OUTPUT
Data Structures with Algorithms Lab 20MCA16
Program 2
#include<stdio.h>
#include<conio.h>
}
}
}
void main()
{
int a[20],n,i, opt;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
for(;;)
{
printf("\n ***** Sorting *****\n");
printf("\n 1. Selection Sort (Descending Order) \n 2. Bubble Sort
(Ascending Order) \n 3. Exit ");
printf("\n Enter your
option:"); scanf("%d",&opt);
switch(opt)
{
case 1: printf("\nEnter array
elements:\n"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection(a, n);
printf("\nElements in Descending order:\n");
for(i=0;i<n;i++)
printf("%d \t", a[i]);
break;
case 2: printf("\nEnter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a, n);
printf("\nElements in ascending order:\n");
for(i=0;i<n;i++)
printf("%d \t", a[i]);
break;
case 3:
default: exit(0);
}
}
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
Program 3
Algorithm:
Data Structure: An Array with TOP as the pointer.
• Start
• Initialize the array of 10 elements and name it as stack
• Initialize other variables like top in the beginning of the program
• Provide the choice to the users for the different operations on stack like
Push(insert),Pop(delete),Display and Exit
• If the choice= push then call the function push()
• If the choice= pop then call the function pop()
• If the choice= display then call the function display()
• If the choice= exit then exit from the program
• End
Function push()
• Check for the overflow condition of the stack
• if (TOP>= SIZE) then Print “Stack is full”
• If not overflow, increment the value of top
• Get the element to be inserted onto the stack from the user
• Assign it as the topmost value, stack[top]
Function pop ()
• Check for the underflow(empty) condition of the stack
• if TOP<0 Print “Stack is Empty”
• If not empty, Output the element to be deleted from the stack
• Decrement the value of top.
Function display()
• Display all the elements of the stack
Data Structures with Algorithms Lab 20MCA16
#include<stdio.h>
int stack[10],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=10]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING
ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
Data Structures with Algorithms Lab 20MCA16
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
Data Structures with Algorithms Lab 20MCA16
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
OUTPUT
Stack is overflow.
Program 4
#include<stdio.h>
#include<conio.h>
#include<string.h>
char infix[50],postfix[50],stack[25];
int top=-1;
void convert_fun();
int precedence(char);
void push(char);
char pop();
void main()
{
clrscr();
printf("Enter the infix expression\n");
scanf("%s",infix);
convert_fun();
printf("\n\t THE INFIX EXPRESSION :%s",infix);
printf("\n\t THE POSTFIX EXPRESSION:%s",postfix);
getch();
}
void convert_fun()
{
int i=0,j=0;
char c,tmp;
push('#');
Data Structures with Algorithms Lab 20MCA16
while(i<strlen(infix))
{
c=infix[i++];
switch(c)
{
case '(' :push(c);
break;
case ')' :tmp=pop();
while(tmp!='(')
{
postfix[j++]=tmp;
tmp=pop();
}
break;
case'+':
case'-':
case'*':
case'/':
case'^':
while(precedence(stack[top])>=precedence(c))
{
tmp=pop();
postfix[j++]=tmp;
}
push(c);
break;
default: postfix[j++]=c;
break;
}
}
while(top>0)
Data Structures with Algorithms Lab 20MCA16
{
tmp=pop();
postfix[j++]=tmp;
}
}
void push(char c)
{
stack[++top]=c;
}
char pop()
{
return(stack[top--]);
}
int precedence(char c)
{
switch(c)
{
case'^':return (3);
case'*':
case'/': return(2);
case '+':
case'-':return(1);
case'(':
case')':return(0);
case'#':return(-1);
}
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
Program 5
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int top,s[100];
int cal(char sym,int op1,int op2)
{
switch(sym)
{
case'+' : return(op1+op2);
case'-' : return(op1-op2);
case'*' : return(op1*op2);
case'/' : return(op1/op2);
case'^' : return(pow(op1,op2));
}
return 0;
}
void main()
{
int i,op1,op2;
char pf[100],sym;
clrscr();
top=-1;
printf("enter the postfix expression:");
Data Structures with Algorithms Lab 20MCA16
gets(pf);
for(i=0;i<strlen(pf);i++)
{
sym=pf[i];
if(isdigit (sym))
{
s[++top]=sym-'0';
}
else
{
op2=s[top--];
op1=s[top--];
s[++top]=cal(sym,op1,op2);
}
}
printf("\n value of %s is %d",pf,s[top--]);
getch();
}
OUTPUT
Program 6
#include <stdio.h>
#include<stdlib.h>
void display();
void insert();
void delete_begin();
void delete_end();
void delete_pos();
struct node
{
int info;
struct node *next;
};
int main()
{
int choice;
clrscr();
while(1){
Data Structures with Algorithms Lab 20MCA16
printf("\n MENU\n");
printf(" 1.Insert\n");
printf(" 2.Display\n");
printf(" 3.Delete from beginning\n");
printf(" 4.Delete from the end\n");
printf(" 5.Delete from specified position\n");
printf(" 6.Exit\n");
printf("--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: display();
break;
case 3: delete_begin();
break;
case 4: delete_end();
break;
case 5: delete_pos();
break;
case 6: exit(0);
break;
default: printf("\n Wrong Choice:\n");
break;
}
}
}
Data Structures with Algorithms Lab 20MCA16
void insert()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
Data Structures with Algorithms Lab 20MCA16
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
Data Structures with Algorithms Lab 20MCA16
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
Data Structures with Algorithms Lab 20MCA16
exit(0);
}
else
{
printf("\nEnter the position of the node to be
deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info
);
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 3
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
Data Structures with Algorithms Lab 20MCA16
6.Exit
Enter your choice: 1
Enter the data value for the node: 2
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 5
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 1
Enter the data value for the node: 6
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Data Structures with Algorithms Lab 20MCA16
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 3
The deleted element is
:6
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 4
The deleted element is
:2
MENU
1.Insert
2.Display
3.Delete from beginning
Data Structures with Algorithms Lab 20MCA16
MENU
1.Insert
2.Display
3.Delete from beginning
4.Delete from the end
5.Delete from specified position
6.Exit
Enter your choice: 6
Data Structures with Algorithms Lab 20MCA16
Program 7
#include<stdio.h>
#include<conio.h>
void main()
{
int n, count =0, am[10][10], indeg[10], flag[10], i, j, k;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
indeg[i]=0;
flag[i]=0;
}
printf("\nEnter adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&am[i][j]);
printf("\nMatrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",am[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
Data Structures with Algorithms Lab 20MCA16
for(j=0;j<n;j++)
indeg[i] += am[j][i];
printf("\nThe topological ordering is:\n");
while(count<n)
{
for(k=0;k<n;k++)
if((indeg[k]==0) && (flag[k]==0))
{
printf("%d\n",k);
flag[k]=1;
count++;
for(i=0;i<n;i++)
if(am[k][i]==1)
indeg[i]--;
}
}
getch();
}
OUTPUT:
Enter number of vertices: 6
Enter adjacency matrix:
011000
000100
000010
001001
000000
000010
The topological ordering is:
013524
Data Structures with Algorithms Lab 20MCA16
Program 8
#include<stdio.h>
#include<conio.h>
void dfs(int n, int a[10][10], int u, int visited[])
{
int v;
visited[u]=1;
for(v=0;v<n;v++)
if((a[u][v]==1)&& (visited[v]==0))
dfs(n,a,v,visited);
}
void main()
{
int n, i, j, a[10][10], visited[10],flag, connected;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
connected=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
visited[j]=0;
dfs(n,a, i, visited);
flag=0;
Data Structures with Algorithms Lab 20MCA16
for(j=0;j<n;j++)
if(visited[ j]==0)
flag=1;
if(flag==0)
connected=1;
}
if(connected==1)
printf("\nGraph is connected");
else
printf("\nGraph is not connected");
getch();
}
OUTPUT
1. Enter number of vertices: 6
Enter adjacency matrix:
010100
101100
010100
111000
000001
000010
Graph is not connected
Program 9
#include<stdio.h>
#include<conio.h>
void dijkstra(int n, int v, int cost[10][10],int dist[10])
{
int count, u, i, w, visited[10], min;
for(i=0;i<n;i++)
{
visited[i]=0;
dist[i]=cost[v][i];
}
visited[v]=1;
dist[v]=1;
count=2;
while(count<=n)
{
min=999;
for(w=0;w<n;w++)
if((dist[w]<min) && (visited[w]!=1))
{
min=dist[w];
u=w;
}
visited[u]=1;
count++;
for(w=0;w<n;w++)
if((dist[u]+cost[u][w]<dist[w]) && (visited[w]!=1))
Data Structures with Algorithms Lab 20MCA16
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n, v, cost[10][10], dist[10], i, j;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost matrix (for infinity, enter 999):\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
printf("\nEnter source vertex:");
scanf("%d",&v);
dijkstra(n,v,cost,dist);
printf("\nShortest path from \n");
for(i=0;i<n;i++)
if(i!=v)
printf("\n%d -> %d = %d", v, i, dist[i]);
getch();
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
Program 10
#include<stdio.h>
#include<conio.h>
void main()
{
int n, v, u,cost[10][10], parent[10]={0}, i, j;
int count=1, mincost=0, min, a, b;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter cost 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;
}
while(count<n)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
{
Data Structures with Algorithms Lab 20MCA16
min=cost[i][j];
a=u=i;
b=v=j;
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
count++;
printf("\nEdge(%d, %d) = %d", a, b, min);
mincost+=min;
parent[v]=u;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost = %d", mincost);
getch();
}
Data Structures with Algorithms Lab 20MCA16
OUTPUT
Edge(2, 5) = 1
Edge(1, 2) = 2
Edge(1, 4) = 3
Edge(3, 6) = 3
Edge(2, 6) = 4
Edge(5, 7) = 4
Minimum cost = 17