Data Structures C
Data Structures C
BACHELOR OF TECHNOLOGY
1
Department of Computer Science & Engineering
Amity School of Engineering & Technology
Amity University, Lucknow Campus
Index
S.No. PROGRAMS DATE Page Remark
No
2
10. Write a program to merge to given sorted
arrays.
3
21. Write a program to implement deletion of
a node in binary search tree.
PROGRAM-1
4
#include <stdio.h>
#include <conio.h>
void main(){
int n,i,f,k=0,A[100];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
scanf("%d",&f);
for(i=0;i<n;i++)
{ if(A[i]==f){k=1;break; }}
getch();
5
PROGRAM-2
#include <stdio.h>
#include <conio.h>
int main(){
clrscr();
scanf("%d",&n);end=n-1;
for(i=0;i<n;i++)
scanf("%d",&A[i]);
scanf("%d",&f);
while(start<=end)
mid=(start+end)/2;
if(A[mid]==f)
k=1;
break;
6
else if(f>A[mid])
start=mid+1;
else end=mid-1;
if(k==1)
printf("%d is at %d position.",f,mid+1);
else
printf("Not found.");
getch();
return 0;
7
PROGRAM-3
#include <stdio.h>
#include <conio.h>
int main()
int n, i, j, t,A[100];
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&A[i]);
t=A[j];A[j]=A[j+1];A[j+1]=t;
8
}
printf("%d\n",A[i]);
getch();
return 0;
}
9
PROGRAM-4
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&A[i]);
pos = i;
pos= j;
10
}
if (pos!= i)
t=A[i];A[i]=A[pos];A[pos]=t;
printf("%d\n",A[i]);
getch();
return 0;
}
11
PROGRAM-5
#include <stdio.h>
#include <conio.h>
int main()
clrscr();
fflush(stdin);
scanf("%d",&n);
printf("\n");
fflush(stdin);
scanf("%d",&a[i]);
temp=a[i];
j=i-1 ;
a[j+1]=a[j];
j--;
12
}
a[j+1]=temp;
printf("%d ",a[i]);
getch();
return 0;
}
13
PROGRAM-6
#include <stdio.h>
#include <conio.h>
int piv=a[lb],t,s=lb,e=ub;
while(s<e)
while(a[s]<=piv)s++;
while(a[e]>piv)e--;
if(s<e){swap(&a[s],&a[e]);}
}swap(&a[lb],&a[e]);
return e;
{int piv;
if(lb<ub)
piv=partition(a,lb,ub);
QuickSort(a,lb,piv-1);
14
QuickSort(a,piv+1,ub);
void main()
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&a[i]);
QuickSort(a,0,n-1);
printf("%d ",a[i]);
getch();
15
16
PROGRAM-7
#include <stdio.h>
#include <conio.h>
int b[10],i=lb,j=mid+1,k=lb;
while(i<=mid&&j<=ub)
if(a[i]>=a[j])b[k++]=a[j++];
if(a[i]<a[j])b[k++]=a[i++];
if(i>mid) {while(j<=ub)b[k++]=a[j++];}
if(j>ub){while(i<=mid)b[k++]=a[i++];}
for(i=lb;i<=ub;i++)
a[i]=b[i];
{int mid;
if(lb<ub)
mid=(lb+ub)/2;
MergeSort(a,lb,mid);
17
MergeSort(a,mid+1,ub);
merge(a,lb,mid,ub);
void main()
int n,i,j,a[10];
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&a[i]);
MergeSort(a,0,n-1);
printf("%d ",a[i]);}
getch(); }
18
PROGRAM-8
8. Write a program to insert a new element in the given unsorted array at kth position.
#include <stdio.h>
#include <conio.h>
int i,k,ele,n,A[100];
printf("%d ",A[i]);
for(i=n-1;i>=k;i--)
A[i+1]=A[i];
A[k]=ele;
void main()
19
{
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&A[i]);
printf("Enter the position at which you want to add the element :\n");
fflush(stdin); scanf("%d",&k);
fflush(stdin);scanf("%d",&ele);
display(A,n);
insert(A,k,ele);n++;
display(A,n);
getch();
20
PROGRAM-9
#include <stdio.h>
#include <conio.h>
int main()
int i,k,n,A[100];
clrscr();
fflush(stdin);
scanf("%d",&n);
fflush(stdin);
scanf("%d",&A[i]);
21
printf("Entered Array elements are :\n");
printf("%d ",A[i]);
fflush(stdin); scanf("%d",&k);
for(i=k;i<n;i++){
A[i]=A[i+1];
printf("%d ",A[i]);
getch();
return 0;
22
PROGRAM-10
#include<stdio.h>
#include<conio.h>
int A[100],B[100],C[200];
int m,n,i,j;
void accept()
fflush(stdin);
scanf("%d",&m);
for(i=0;i<m;i++)
fflush(stdin);
scanf("%d",&A[i]);
23
}
fflush(stdin);
scanf("%d",&n);
for(i=0;i<n;i++)
fflush(stdin);
scanf("%d",&B[i]);
void merge()
for(i=0;i<m;i++)
C[i]=A[i];
j=i;
for(i=0;i<n;i++)
C[j++]=B[i];
}}
void display()
24
for(i=0;i<(m+n);i++)
printf("%d ",C[i]);
int main()
clrscr();
accept();
merge();
display();
getch();
return 0;
}
25
PROGRAM-11
11. Write a program to implement Stack using array, also show overflow and underflow
in respective push and pop operations.
#include <stdio.h>
#include <conio.h>
int stk[100],size,ptr=-1,i,ele,del;
else stk[++ptr]=ele;
void delete()
else {
del=stk[ptr--];
26
printf("\nElement deleted is : %d\n",del);
}}
void display()
for(i=0;i<=ptr;i++)
printf("%d ",stk[i]);
void main()
int ch=1,switch_choice;
clrscr();
fflush(stdin);
scanf("%d",&size);
while(ch==1){
printf("\nPress :\n 1-To insert an element into the stack. \n 2-To delete an element from
the stack.\n " );
fflush(stdin);scanf("%d",&switch_choice);
switch(switch_choice)
case 1:
scanf("%d",&ele);
insert(ele);
break;
case 2:
27
delete();
break;
display();
fflush(stdin);scanf("%d",&ch);clrscr();
}}
28
PROGRAM-12
12. Write a program to implement Queue using array, which shows insertion and
deletion operations.
#include <stdio.h>
#include <conio.h>
int que[100],size,i,del,ele,front=0,rear=-1;
else que[++rear]=ele;
void delete()
29
else {
del=que[front++];
void display()
for(i=front;i<=rear;i++)
printf("%d ",que[i]);
void main()
int ch=1,switch_choice;
clrscr();
fflush(stdin);
scanf("%d",&size);
while(ch==1)
printf("\nPress :\n 1-To insert an element into the queue. \n 2-To delete an element from the
queue.\n " );
fflush(stdin);scanf("%d",&switch_choice);
switch(switch_choice)
30
{
case 1:
scanf("%d",&ele);
insert(ele);
break;
case 2:
delete();
break;
display();
fflush(stdin);scanf("%d",&ch);
clrscr();
}}
31
PROGRAM-13
13. Write a program to implement Circular Queue using array, which shows insertion
and deletion operations.
#include <stdio.h>
#include <conio.h>
int cque[100],N,i,del,ele,front=-1,rear=-1;
if(front==-1&&rear==-1)
{front=rear=0;
cque[rear]=ele;
32
else if((rear+1)%N==front)
{printf("Overflows!");
else
rear=(rear+1)%N;
cque[rear]=ele;
}}
void delete()
if(front==-1&&rear==-1)
printf("Underflows.");
else if(front==rear)
{del=cque[front];
front=rear=-1;
else
del=cque[front];
front=(front+1)%N;
}}
void display()
33
if(front==-1&&rear==-1){
else{
i=front;
while(i!=rear)
printf("%d ",cque[i]);
i=(i+1)%N;
printf("%d ",cque[rear]);
}}
void main()
int ch=1,switch_choice;
clrscr();
fflush(stdin);
scanf("%d",&N);
while(ch==1)
printf("\nPress :\n 1-To insert an element into the circular queue. \n 2-To delete an element
from the circular queue.\n " );
fflush(stdin);scanf("%d",&switch_choice);
switch(switch_choice)
34
case 1:
scanf("%d",&ele);
insert(ele);
break;
case 2:
delete();
break;
display();
fflush(stdin);scanf("%d",&ch);
clrscr();
}}
35
PROGRAM-14
14. Write a program to implement Linear Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
#include <stdio.h>
#include <conio.h>
int size;
struct node
int data;
}*head,*newnode,*temp;
36
void create()
scanf("%d",&newnode->data);
newnode->next=0;
void display()
temp=head;
while(temp!=0)
{printf("%d ",temp->data);
temp=temp->next;
void insert()
int ch,pos,c=0;
temp=head;
create();
printf("\nPress \n1-To insert the new node in the beginning of the linked list.\n2-To insert the
new node at the end of the linked list.\n3-To insert the new node after a particular node of the
linked list.\n");
scanf("%d",&ch);
switch(ch)
37
{
case 1: newnode->next=head;head=newnode;break;
case 2:
while(temp->next!=0)temp=temp->next;
temp->next=newnode;
newnode->next=0;
temp=newnode;break;
case 3:
printf("\nEnter the node number after which you want to insert a node : \n");
scanf("%d",&pos);
if(pos>size)printf("\nInvalid Position\n");
else{
while(++c!=pos)
{temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
break;
default:printf("\nWrong Choice");break;
}}
void delete()
int ch,i=1,pos;
38
temp=head;
printf("\nPress \n1-To delete a node from the beginning of the linked list.\n2-To delete a node
from the end of the linked list.\n3-To delete a node from a particular position of the linked
list.\n");
scanf("%d",&ch);
switch(ch)
case 1: head=temp->next;free(temp);break;
case 2:
while(temp->next!=0){
prev_node=temp;
temp=temp->next;
prev_node->next=0;
free(temp);
break;
case 3:
printf("\nEnter the position of the node which you want to delete : \n");
scanf("%d",&pos);
if(pos>size)printf("\nInvalid Position\n");
else{
while(i++<pos-1)
temp=temp->next;
next_node=temp->next;
39
temp->next=next_node->next;
}free(next_node);
break;
default:printf("\nWrong Choice");break;
}}
void search()
int i=1,c=0,find;
temp=head;
scanf("%d",&find);
while(temp!=0)
if(temp->data==find)
c=1;
break;
++i;temp=temp->next;
if(c==0)
void main()
40
{
int i,choice;head=0;clrscr();
scanf("%d",&size);
for(i=1;i<=size;i++){
create();
if(head==0) head=temp=newnode;
else{
temp->next=newnode;temp=newnode;
printf("\nPress \n1-To insert an element in the list.\n2-To delete an element from the list.\n3-
To search an element in the list.\n");
scanf("%d",&choice);
switch(choice){
case 1 : insert();break;
case 2: delete();break;
case 3: search();break;
display();
getch();
41
PROGRAM-15
15. Write a program to implement Stack, using Linked List. Implement Push, Pop and
display operations.
#include <stdio.h>
#include <conio.h>
int del,ele;
struct node
int data;
42
}*newnode,*ptr,*temp;
newnode->data=ele;
newnode->next=ptr;
ptr=newnode;
void delete()
else
prev_node=ptr;
del=ptr->data;
ptr=ptr->next;
free(prev_node);
}}
void display()
temp=ptr;
while(temp!=0){
43
printf("%d ",temp->data);
temp=temp->next;
void main()
int choice,ch=1;ptr=0;clrscr();
while(ch==1){
printf("\nPress \n1-To insert an element in the stack.\n2-To delete an element from the stack.\
n");
scanf("%d",&choice);
switch(choice){
case 1 :
scanf("%d",&ele);
insert(ele);
break;
case 2:
delete();
break;
display();
scanf("%d",&ch);
clrscr();
44
}
PROGRAM-16
16. Write a program to implement Queue, using Linked List. Implement Insertion,
deletion and display operations.
#include <stdio.h>
#include <conio.h>
int del,ele,c=1;
struct node
int data;
45
struct node *next;
}*newnode,*front,*rear,*temp;
newnode->data=ele;
newnode->next=0;
if(c++==1)front=rear=newnode;
else{
rear->next=newnode;
rear=newnode;
}}
void delete()
else
temp=front;
del=temp->data;
front=front->next;
free(temp);
c--;
}}
void display()
46
{
temp=front;
while(temp!=0){
printf("%d ",temp->data);
temp=temp->next;
}}
void main()
int choice,ch=1;front=0;clrscr();
while(ch==1){
printf("\nPress \n1-To insert an element in the queue.\n2-To delete an element from the
queue.\n");
scanf("%d",&choice);
switch(choice){
case 1 :
scanf("%d",&ele);
insert(ele);
break;
case 2:
delete();
break;
47
display();
scanf("%d",&ch);
clrscr();
}
}
48
PROGRAM-17
17. Write a program to count the number of times an item is present in a linked list.
#include <stdio.h>
#include <conio.h>
int size;
struct node
int data;
}*head,*newnode,*temp;
void create()
scanf("%d",&newnode->data);
newnode->next=0;
void display()
temp=head;
while(temp!=0)
{printf("%d ",temp->data);
temp=temp->next;
49
void frequency()
int c;
for(i=head;i!=0;i=i->next)
{c=0;
for(j=head;j!=0;j=j->next)
if(j->data==i->data)
c++;
if(c>0)
} }
int main()
int i,choice;head=0;clrscr();
scanf("%d",&size);
for(i=1;i<=size;i++){
create();
if(head==0) head=temp=newnode;
else{
50
temp->next=newnode;temp=newnode;
display();
frequency();
getch();
return 0;
51
PROGRAM-18
18. Write a program to increment the data part of every node present in a linked list by
10. Display the data both before incrimination and after.
#include <stdio.h>
#include <conio.h>
int size;
struct node
int data;
}*head,*newnode,*temp;
void create()
scanf("%d",&newnode->data);
newnode->next=0;
void display()
temp=head;
while(temp!=0)
{printf("%d ",temp->data);
temp=temp->next;
52
}
void inc_data()
for(temp=head;temp!=0;temp=temp->next)
temp->data=temp->data+10;
void main()
int i,choice;head=0;clrscr();
scanf("%d",&size);
for(i=1;i<=size;i++)
create();
if(head==0) head=temp=newnode;
else{
temp->next=newnode;temp=newnode;
display();
inc_data();
display();
53
getch();
54
PROGRAM-19
19. Write a program to implement Doubly Linked List, showing all the operations, like
creation, display, insertion, deletion and searching.
#include <stdio.h>
#include <conio.h>
int size;
struct node
int data;
}*head,*tail,*newnode,*temp;
void create()
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
void display()
temp=head;
while(temp!=0)
{printf("%d ",temp->data);
55
temp=temp->next;
void insert()
int ch,pos,c=0;
temp=head;
create();
printf("\nPress \n1-To insert the new node in the beginning of the linked list.\n2-To insert the
new node at the end of the linked list.\n3-To insert the new node after a particular node of the
linked list.\n");
scanf("%d",&ch);
switch(ch)
case 1: newnode->next=head;head->prev=newnode;head=newnode;break;
case 2:
newnode->prev=tail;
tail->next=newnode;
tail=newnode;
break;
case 3:
printf("\nEnter the node number after which you want to insert a node : \n");
scanf("%d",&pos);
if(pos>size)printf("\nInvalid Position\n");
else{
56
while(++c!=pos)
{temp=temp->next;
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
break;
default:printf("\nWrong Choice");break;
}}
void delete()
int ch,i=1,pos;
temp=head;
printf("\nPress \n1-To delete a node from the beginning of the linked list.\n2-To delete a node
from the end of the linked list.\n3-To delete a node from a particular position of the linked
list.\n");
scanf("%d",&ch);
switch(ch)
case 1: head=temp->next;head->prev=0;free(temp);break;
case 2:
temp=tail;
tail=tail->prev;
tail->next=0;
57
free(temp);
break;
case 3:
printf("\nEnter the position of the node which you want to delete : \n");
scanf("%d",&pos);
if(pos>size)printf("\nInvalid Position\n");
else{
while(i++<pos)
temp=temp->next;
next_node=temp->next;
prev_node=temp->prev;
prev_node->next=next_node;
next_node->prev=prev_node;
}free(temp);
break;
default:printf("\nWrong Choice");break;
}}
void search()
int i=1,c=0,find;
temp=head;
scanf("%d",&find);
58
while(temp!=0)
if(temp->data==find)
c=1;
break;
++i;temp=temp->next;
if(c==0)
void main()
int i,choice;head=0;clrscr();
scanf("%d",&size);
for(i=1;i<=size;i++){
create();
if(head==0){ head=temp=newnode;}
else{
newnode->prev=temp;
temp->next=newnode;
59
temp=newnode;
}}
tail=temp;
printf("\nPress \n1-To insert an element in the list.\n2-To delete an element from the list.\n3-
To search an element in the list.\n");
scanf("%d",&choice);
switch(choice){
case 1 : insert();break;
case 2: delete();break;
case 3: search();break;
display();
getch();
}
60
61
PROGRAM-20
20. Write a program to create a Binary Search Tree and display its contents using
recursive preorder, postorder and inorder traversal.
#include<stdio.h>
#include<conio.h>
struct node
char data;
};
if(new==0)
new->data=x;
new->left=0;
new->right=0;
return new;
else if(x>new->data)
new->right=insert(new->right,x);
else
new->left=insert(new->left,x);
return new;
62
}
if(root==0) return;
printf("%c ",root->data);
preorder(root->left);
preorder(root->right);
if(root==0)return;
inorder(root->left);
printf("%c ",root->data);
inorder(root->right);
if(root==0)return;
postorder(root->left);
postorder(root->right);
printf("%c ",root->data);
void main()
63
clrscr();
fflush(stdin);
scanf("%d",&size);
for(i=0;i<size;i++)
fflush(stdin);
scanf("%c",&x);
root=insert(root,x);
printf("\nPreorder :\n");
preorder(root);
printf("\nInorder :\n");
inorder(root);
printf("\nPostorder :\n");
postorder(root);
getch();
}
64
65
PROGRAM-21
#include<stdio.h>
#include<conio.h>
struct node
int data;
};
if(new==0)
new->data=x;
new->left=0;
new->right=0;
return new;
else if(x>new->data)
new->right=insert(new->right,x);
else
new->left=insert(new->left,x);
66
return new;
if(root==0)return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
else
if(root->left==0&&root->right==0)
temp=root;root=0;free(temp);
else if(root->right==0)
temp=root;
root=root->left;
67
free(temp);
else if(root->left==0)
temp=root;
root=root->right;
free(temp);
else
temp=findMin(root->right);
root->data=temp->data;
root->right=delete(root->right,temp->data);
}}
return root;
if(root->left==0&&root->right==0)
return root;
return findMin(root->left);
void main()
68
clrscr();
fflush(stdin);
scanf("%d",&size);
for(i=0;i<size;i++)
fflush(stdin);
scanf("%d",&a);
root=insert(root,a);
inorder(root);
scanf("%d",&x);
root=delete(root,x);
inorder(root);
getch();
69
70
PROGRAM-22
22. Write a program to implement Binary tree and display the contents using non-
recursive preorder, postorder and inorder traversal techniques.
#include<stdio.h>
#include<conio.h>
struct node
char data;
}*stk[100];
int top=-1;
if(new==0)
new->data=x;
new->left=0;
new->right=0;
return new;
else if(x>new->data)
new->right=insert(new->right,x);
else
new->left=insert(new->left,x);
71
return new;
stk[++top]=new;
int isEmpty()
if(top<0)
return 1;
return 0;
if(isEmpty()==0)
return stk[top--];
while(1)
while(root)
printf("%c ",root->data);
push(root);
72
root=root->left;
if(isEmpty()==1)
break;
root=pop();
root=root->right;
while(1)
while(root)
push(root);
root=root->left;
if(isEmpty()==1)
break;
root=pop();
printf("%c ",root->data);
root=root->right;
73
{
while(current!=0||isEmpty()==0)
if(current!=0)
push(current);
current=current->left;
else
temp=stk[top]->right;
if(temp==0)
temp=pop();
printf("%c ",temp->data);
while(isEmpty()==0&&temp==stk[top]->right)
temp=pop();
printf("%c ",temp->data);
}}
else
current=temp;
}}}
74
void main()
clrscr();
fflush(stdin);
scanf("%d",&size);
for(i=0;i<size;i++)
fflush(stdin);
scanf("%c",&x);
root=insert(root,x);
printf("\nPreorder :\n");
preorder(root);
printf("\nInorder :\n");
inorder(root);
printf("\nPostorder :\n");
postorder(root);
getch();
75
76
PROGRAM-23
#include<stdio.h>
#include<conio.h>
int i;
for(i=1;i<=n;i++)
printf("%d ",A[i]);
printf("\n");
int t=*a;
*a=*b;
*b=t;
int l=2*i,r=l+1,max=i,t;
if(l<=n&&A[l]>A[max]) max=l;
if(r<=n&&A[r]>A[max]) max=r;
if(i!=max){
swap(&A[i],&A[max]);
maxHeap(A,n,i+1);
77
}
{int i;
for(i=n/2;i>=1;i--)
maxHeap(A,n,i);
for(i=n;i>=1;i--)
swap(&A[1],&A[i]);
maxHeap(A,i-1,1);
void main()
int A[100],n,i,x=0;
clrscr();
fflush(stdin);
scanf("%d",&n);
for(i=1;i<=n;i++)
fflush(stdin);
scanf("%d",&A[i]);
78
printf("\nArray elements are : ");
display(A,n);
heapSort(A,n);
display(A,n);
getch();
79
PROGRAM-24
24. Write a program of Graph Traversal-Depth first search and Breadth first search.
// DFS algorithm
#include <stdio.h>
#include <conio.h>
struct node {
int vertex;
};
struct Graph {
int numVertices;
int* visited;
};
int connectedVertex ;
graph->visited[vertex] = 1;
connectedVertex = temp->vertex;
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
80
}
temp = temp->next;
}}
newNode->vertex = v;
newNode->next = NULL;
return newNode;
int i;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
81
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
temp = graph->adjLists[v];
while (temp) {
temp = temp->next;
printf("\n");
}}
int main() {
clrscr();
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
printf("\n");
82
DFS(graph, 2);
getch();
return 0;
// BFS algorithm
#include <stdio.h>
#include<conio.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
83
int isEmpty(struct queue* q);
struct node {
int vertex;
};
struct Graph {
int numVertices;
int* visited;
};
int currentVertex;
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
currentVertex = dequeue(q);
temp = graph->adjLists[currentVertex];
while (temp) {
84
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
temp = temp->next;
}}}
newNode->vertex = v;
newNode->next = NULL;
return newNode;
int i;
graph->numVertices = vertices;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
return graph;
85
}
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
q->front = -1;
q->rear = -1;
return q;
if (q->rear == -1)
return 1;
else
return 0;
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
86
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}}
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
}}
return item;
int i = q->front;
if (isEmpty(q)) {
printf("Queue is empty");
} else {
87
for (i = q->front; i < q->rear + 1; i++) {
}}}
int main() {
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
getch();
return 0;
88
PROGRAM-25
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
void main() {
clrscr();
scanf("%d",&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) {
89
min=cost[i][j];
a=u=i;
b=v=j;
if(visited[u]==0 || visited[v]==0) {
mincost+=min;
visited[b]=1;
cost[a][b]=cost[b][a]=999;
getch();
}
90
PROGRAM-26
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
clrscr();
scanf("%d",&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;
91
}
while(ne < n)
for(i=1,min=999;i<=n;i++)
min=cost[i][j];
a=u=i;
b=v=j;
}}}
u=find(u);
v=find(v);
if(uni(u,v))
mincost +=min;
cost[a][b]=cost[b][a]=999;
92
getch();
int find(int i)
while(parent[i])
i=parent[i];
return i;
if(i!=j)
parent[j]=i;
return 1;
return 0;
93
94