0% found this document useful (0 votes)
25 views94 pages

Data Structures C

Uploaded by

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

Data Structures C

Uploaded by

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

LAB RECORD

BACHELOR OF TECHNOLOGY

B.Tech. CS&E Semester (3)

(Academic Session: 2021-25)

Course Title : Data Structures Using C


Course Code : CSIT124
Enrollment No. : A7605221104
Name of Student : JUHI DWIVEDI
Date of Submission :
Signature of Student :
Grade/Marks Obtained :
Faculty Name & Signature: Dr. Namrata Dhanda

Department of Computer Science & Engineering


Amity School of Engineering & Technology
Amity University, Lucknow Campus

1
Department of Computer Science & Engineering
Amity School of Engineering & Technology
Amity University, Lucknow Campus

Index
S.No. PROGRAMS DATE Page Remark
No

1. Write a program to search an element


using Linear Search.

2. Write a program to search an element


using Binary Search.

3. Write a program to sort the given array


using Bubble Sort.

4. Write a program to sort the given array


using Selection Sort.

5. Write a program to sort the given array


using Insertion Sort.

6. Write a program to sort the given array


using QuickSort.

7. Write a program to sort the given array


using MergeSort.

8. Write a program to insert a new element in


the given unsorted array at kth position.

9. Write a program to delete an element from


given sorted array.

2
10. Write a program to merge to given sorted
arrays.

11. Write a program to implement Stack using


array, also show overflow and underflow in
respective push and pop operations.

12. Write a program to implement Queue


using array, which shows insertion and
deletion operations.

13. Write a program to implement Circular


Queue using array, which shows insertion
and deletion operations.

14. Write a program to implement Linear


Linked List, showing all the operations,
like creation, display, insertion, deletion
and searching.

15. Write a program to implement Stack,


using Linked List. Implement Push, Pop
and display operations.

16. Write a program to implement Queue,


using Linked List. Implement Insertion,
deletion and display operations.

17. Write a program to count the number of


times an item is present in a linked list.

19. Write a program to implement Doubly


Linked List, showing all the operations,
like creation, display, insertion, deletion
and searching.
20. Write a program to create a Binary Search
Tree and display its contents using
recursive preorder, postorder and inorder
traversal.

3
21. Write a program to implement deletion of
a node in binary search tree.

22. Write a program to implement Binary tree


and display the contents using non-
recursive preorder, postorder and inorder
traversal techniques.

23. Write a program to sort the given array


using Heap Sort.

24. Write a program of Graph traversal-Depth


first search and Breadth first search.

25. Write a program to implement Prim’s


algorithm.

26. Write a program to implement Kruskal


algorithm.

PROGRAM-1

1. Write a program to search an element using Linear Search.

4
#include <stdio.h>

#include <conio.h>

void main(){

int n,i,f,k=0,A[100];

clrscr();

printf("Enter size of the array : ");

scanf("%d",&n);

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

printf("Enter element %d of the array : ",i+1);

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

printf("Enter element to find : ");

scanf("%d",&f);

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

{ if(A[i]==f){k=1;break; }}

if(k==1) printf("%d is at %d position.",f,i+1);

else printf("%d is not found.",f);

getch();

5
PROGRAM-2

2. Write a program to search an element using Binary Search.

#include <stdio.h>

#include <conio.h>

int main(){

int n,i,f,k=0 ,A[100],start=0,end=n-1,mid=0;

clrscr();

printf("Enter size of the array : ");

scanf("%d",&n);end=n-1;

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

printf("Enter element %d of the array : ",i+1); fflush(stdin);

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

printf("Enter element to find : "); fflush(stdin);

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

3. Write a program to sort the given array using Bubble Sort.

#include <stdio.h>

#include <conio.h>

int main()

int n, i, j, t,A[100];

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

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

printf("Enter element %d of the array:\n",i);

fflush(stdin);

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

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

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

if (A[j] > A[j+1])

t=A[j];A[j]=A[j+1];A[j+1]=t;

8
}

printf("Sorted list in ascending order:\n");

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

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

getch();

return 0;
}

9
PROGRAM-4

4. Write a program to sort the given array using Selection Sort.

#include <stdio.h>

#include <conio.h>

int main()

int n, i, j, pos, t, A[100];

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

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

printf("Enter element %d of the array:\n",i);

fflush(stdin);

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

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

pos = i;

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

if (A[pos] > A[j])

pos= j;

10
}

if (pos!= i)

t=A[i];A[i]=A[pos];A[pos]=t;

printf("Sorted list in ascending order:\n");

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

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

getch();

return 0;
}

11
PROGRAM-5

5. Write a program to sort the given array using Insertion Sort.

#include <stdio.h>

#include <conio.h>

int main()

int n,i,j, temp,a[100];

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

printf("\n");

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

printf("Enter element %d of the array : ",i);

fflush(stdin);

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

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

temp=a[i];

j=i-1 ;

while ( j >= 0 && temp < a[j]) {

a[j+1]=a[j];

j--;

12
}

a[j+1]=temp;

printf("\nSorted list in ascending order:\n");

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

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

getch();

return 0;
}

13
PROGRAM-6

6. Write a program to sort the given array using QuickSort.

#include <stdio.h>

#include <conio.h>

void swap(int *a, int *b){int t=*a;*a=*b;*b=t;}

int partition(int a[],int lb,int ub)

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;

void QuickSort(int a[],int lb,int ub)

{int piv;

if(lb<ub)

piv=partition(a,lb,ub);

QuickSort(a,lb,piv-1);

14
QuickSort(a,piv+1,ub);

void main()

int n,i,j, temp,a[10];

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

printf("\nEnter array elements :\n");

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

fflush(stdin);

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

QuickSort(a,0,n-1);

printf("\nSorted list in ascending order:\n");

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

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

getch();

15
16
PROGRAM-7

7. Write a program to sort the given array using MergeSort.

#include <stdio.h>

#include <conio.h>

void merge(int a[],int lb,int mid,int ub)

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];

void MergeSort(int a[],int lb,int ub)

{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();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

printf("\nEnter array elements :\n");

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

fflush(stdin);

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

MergeSort(a,0,n-1);

printf("\nSorted list in ascending order:\n");

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

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];

void display(int A[100],int n)

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

printf("%d ",A[i]);

void insert(int A[100],int k, int ele)

for(i=n-1;i>=k;i--)

A[i+1]=A[i];

A[k]=ele;

void main()

19
{

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

printf("Enter array elements :\n");

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

fflush(stdin);

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

printf("Enter the position at which you want to add the element :\n");

fflush(stdin); scanf("%d",&k);

printf("Enter the element to be added :\n");

fflush(stdin);scanf("%d",&ele);

printf("Entered Array elements are :\n");

display(A,n);

insert(A,k,ele);n++;

printf("\nArray elements after insertion are :\n");

display(A,n);

getch();

20
PROGRAM-9

9. Write a program to delete an element from given sorted array.

#include <stdio.h>

#include <conio.h>

int main()

int i,k,n,A[100];

clrscr();

printf("Enter number of elements:\n");

fflush(stdin);

scanf("%d",&n);

printf("Enter array elements :\n");

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

fflush(stdin);

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

21
printf("Entered Array elements are :\n");

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

printf("%d ",A[i]);

printf("\nEnter the position of the element you want to delete :\n");

fflush(stdin); scanf("%d",&k);

for(i=k;i<n;i++){

A[i]=A[i+1];

printf("\nArray elements after deletion are :\n");

for (i = 0; i <n-1; i++) {

printf("%d ",A[i]);

getch();

return 0;

22
PROGRAM-10

10. Write a program to merge to given sorted arrays.

#include<stdio.h>

#include<conio.h>

int A[100],B[100],C[200];

int m,n,i,j;

void accept()

printf("Enter size of array 1 :\n");

fflush(stdin);

scanf("%d",&m);

printf("Enter elements of array 1 :\n");

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

fflush(stdin);

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

23
}

printf("Enter size of array 2 :\n");

fflush(stdin);

scanf("%d",&n);

printf("Enter elements of array 2 :\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()

printf("Merged Sorted Array is :\n");

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;

void insert(int ele)

if(ptr>=size-1) printf("Stack Full ! Overflows.");

else stk[++ptr]=ele;

void delete()

if(ptr<0) printf("Stack Empty ! Underflows.");

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();

printf("Enter size of stack :\n");

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:

printf("\nEnter element to insert into the stack:\n");

scanf("%d",&ele);

insert(ele);

break;

case 2:

27
delete();

break;

default: printf("\nWrong choice !\n");break;

printf("\nStack elements after the entered operation :\n");

display();

printf("\nIf you want to continue ,press 1.\n");

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;

void insert(int ele)

if(rear==size-1) printf("Queue Overflows.");

else que[++rear]=ele;

void delete()

if(front>rear) printf("Queue Underflows.");

29
else {

del=que[front++];

printf("\nElement deleted is : %d\n",del);

void display()

for(i=front;i<=rear;i++)

printf("%d ",que[i]);

void main()

int ch=1,switch_choice;

clrscr();

printf("Enter size of queue :\n");

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:

printf("\nEnter element to insert into the queue:\n");

scanf("%d",&ele);

insert(ele);

break;

case 2:

delete();

break;

default: printf("\nWrong choice !\n");break;

printf("\nQueue elements after the entered operation :\n");

display();

printf("\nIf you want to continue ,press 1.\n");

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;

void insert(int ele)

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;

printf("Element deleted is : %d\n",del);

else

del=cque[front];

printf("Element deleted is : %d\n",del);

front=(front+1)%N;

}}

void display()

33
if(front==-1&&rear==-1){

printf("No elements to display.");

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();

printf("Enter size of cqueue :\n");

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:

printf("\nEnter element to insert into the circular queue:\n");

scanf("%d",&ele);

insert(ele);

break;

case 2:

delete();

break;

default: printf("\nWrong choice !\n");break;

printf("\nCircular Queue elements after the entered operation :\n");

display();

printf("\nIf you want to continue ,press 1.\n");

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;

struct node *next;

}*head,*newnode,*temp;

36
void create()

newnode=(struct node *)malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->next=0;

void display()

temp=head;

printf("\nElements in the linked list are :\n");

while(temp!=0)

{printf("%d ",temp->data);

temp=temp->next;

void insert()

int ch,pos,c=0;

temp=head;

printf("\nEnter element to insert in the linked list :\n");

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;

struct node *prev_node,*next_node;

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;

printf("\nEnter the element to search in a linked list :\n");

scanf("%d",&find);

while(temp!=0)

if(temp->data==find)

printf("\n%d is at %d position in the linked list.\n",find,i);

c=1;

break;

++i;temp=temp->next;

if(c==0)

printf("\n%d is not in the list.",find);

void main()

40
{

int i,choice;head=0;clrscr();

printf("\nEnter number of nodes you want to create :\n");

scanf("%d",&size);

printf("\nEnter elements of linked list one by one :\n");

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;

default:printf("Wrong Choice !");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;

struct node *next;

42
}*newnode,*ptr,*temp;

void insert(int ele)

newnode=(struct node *)malloc(sizeof(struct node));

newnode->data=ele;

newnode->next=ptr;

ptr=newnode;

void delete()

{struct node *prev_node;

if(ptr==0)printf("\nStack Underflows !\n");

else

prev_node=ptr;

del=ptr->data;

printf("\nElement deleted is %d.\n",del);

ptr=ptr->next;

free(prev_node);

}}

void display()

temp=ptr;

if(temp==0){printf("\nNo element to display.\n");return;}

printf("\nElements in the stack are :\n");

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 :

printf("\nElement to insert into the stack :\n");

scanf("%d",&ele);

insert(ele);

break;

case 2:

delete();

break;

default:printf("Wrong Choice !");break;

display();

printf("\nIf you want to continue, press 1.\n");

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;

void insert(int ele)

newnode=(struct node *)malloc(sizeof(struct node));

newnode->data=ele;

newnode->next=0;

if(c++==1)front=rear=newnode;

else{

rear->next=newnode;

rear=newnode;

}}

void delete()

if(front==0)printf("\nQueue Underflows !\n");

else

temp=front;

del=temp->data;

printf("\nElement deleted is %d.\n",del);

front=front->next;

free(temp);

c--;

}}

void display()

46
{

temp=front;

if(temp==0){printf("\nNo element to display.\n");return;}

printf("\nElements in the queue are :\n");

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 :

printf("\nElement to insert into the queue :\n");

scanf("%d",&ele);

insert(ele);

break;

case 2:

delete();

break;

default:printf("Wrong Choice !");break;

47
display();

printf("\nIf you want to continue, press 1.\n");

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;

struct node *next;

}*head,*newnode,*temp;

void create()

newnode=(struct node *)malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->next=0;

void display()

temp=head;

printf("\nElements in the linked list are :\n");

while(temp!=0)

{printf("%d ",temp->data);

temp=temp->next;

49
void frequency()

int c;

struct node *i,*j;

printf("\nFrequency of elements is: \n");

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)

printf("%d is present %d times in the list.\n",i->data,c);

} }

int main()

int i,choice;head=0;clrscr();

printf("\nEnter number of nodes you want to create :\n");

scanf("%d",&size);

printf("\nEnter elements of linked list one by one :\n");

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;

struct node *next;

}*head,*newnode,*temp;

void create()

newnode=(struct node *)malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->next=0;

void display()

temp=head;

printf("\nElements in the linked list are :\n");

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();

printf("\nEnter number of nodes you want to create :\n");

scanf("%d",&size);

printf("\nEnter elements of linked list one by one :\n");

for(i=1;i<=size;i++)

create();

if(head==0) head=temp=newnode;

else{

temp->next=newnode;temp=newnode;

printf("\nBefore incrementing ,\n");

display();

inc_data();

printf("\n\nAfter incrementing ,\n");

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;

struct node *next;

struct node *prev;

}*head,*tail,*newnode,*temp;

void create()

newnode=(struct node *)malloc(sizeof(struct node));

scanf("%d",&newnode->data);

newnode->prev=0;

newnode->next=0;

void display()

temp=head;

printf("\nElements in the linked list are :\n");

while(temp!=0)

{printf("%d ",temp->data);

55
temp=temp->next;

void insert()

int ch,pos,c=0;

temp=head;

printf("\nEnter element to insert in the linked list :\n");

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;

struct node *prev_node,*next_node;

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;

printf("\nEnter the element to search in a linked list :\n");

scanf("%d",&find);

58
while(temp!=0)

if(temp->data==find)

printf("\n%d is at %d position in the linked list.\n",find,i);

c=1;

break;

++i;temp=temp->next;

if(c==0)

printf("\n%d is not in the list.",find);

void main()

int i,choice;head=0;clrscr();

printf("\nEnter number of nodes you want to create :\n");

scanf("%d",&size);

printf("\nEnter elements of linked list one by one :\n");

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;

default:printf("Wrong Choice !");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;

struct node *left,*right;

};

struct node* insert(struct node *new,char x)

if(new==0)

new=(struct node*)malloc(sizeof(struct node));

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
}

void preorder(struct node* root)

if(root==0) return;

printf("%c ",root->data);

preorder(root->left);

preorder(root->right);

void inorder(struct node* root)

if(root==0)return;

inorder(root->left);

printf("%c ",root->data);

inorder(root->right);

void postorder(struct node* root)

if(root==0)return;

postorder(root->left);

postorder(root->right);

printf("%c ",root->data);

void main()

struct node* root=0;int size,i;char x;

63
clrscr();

printf("\nEnter the number of elements you want to insert in BST :\n");

fflush(stdin);

scanf("%d",&size);

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

printf("\nEnter data to insert : ");

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

21. Write a program to implement deletion of a node in binary search tree.

#include<stdio.h>

#include<conio.h>

struct node

int data;

struct node *left,*right;

};

struct node* findMin(struct node*);

struct node* insert(struct node *new,int x)

if(new==0)

new=(struct node*)malloc(sizeof(struct node));

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;

void inorder(struct node* root)

if(root==0)return;

inorder(root->left);

printf("%d ",root->data);

inorder(root->right);

struct node* delete(struct node *root,int x)

struct node *temp;

if(root==0) return root;

else if(x>root->data) root->right=delete(root->right,x);

else if(x<root->data) root->left=delete(root->left,x);

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;

struct node* findMin(struct node *root)

if(root->left==0&&root->right==0)

return root;

return findMin(root->left);

void main()

struct node* root=0;int size,i,a,x;

68
clrscr();

printf("\nEnter the number of elements you want to insert in BST :\n");

fflush(stdin);

scanf("%d",&size);

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

printf("\nEnter data to insert : ");

fflush(stdin);

scanf("%d",&a);

root=insert(root,a);

printf("\nInorder traversal of tree before deletion :\n");

inorder(root);

printf("\nEnter the element you want to delete : ");

scanf("%d",&x);

root=delete(root,x);

printf("\nInorder traversal of tree after deletion :\n");

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;

struct node *left,*right;

}*stk[100];

int top=-1;

struct node* insert(struct node *new,char x)

if(new==0)

new=(struct node*)malloc(sizeof(struct node));

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;

void push(struct node *new)

stk[++top]=new;

int isEmpty()

if(top<0)

return 1;

return 0;

struct node* pop()

if(isEmpty()==0)

return stk[top--];

void preorder(struct node* root)

while(1)

while(root)

printf("%c ",root->data);

push(root);

72
root=root->left;

if(isEmpty()==1)

break;

root=pop();

root=root->right;

void inorder(struct node* root)

while(1)

while(root)

push(root);

root=root->left;

if(isEmpty()==1)

break;

root=pop();

printf("%c ",root->data);

root=root->right;

void postorder(struct node* root)

73
{

struct node *current=root,*temp;

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()

struct node* root=0;int size,i;char x;

clrscr();

printf("\nEnter the number of elements you want to insert in BST :\n");

fflush(stdin);

scanf("%d",&size);

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

printf("\nEnter data to insert : ");

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

23. Write a program to sort the given array using HeapSort.

#include<stdio.h>

#include<conio.h>

void display(int A[],int n)

int i;

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

printf("%d ",A[i]);

printf("\n");

void swap(int *a,int* b)

int t=*a;

*a=*b;

*b=t;

void maxHeap(int A[],int n,int i)

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
}

void heapSort(int A[],int n)

{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();

printf("\nEnter the size of array :\n");

fflush(stdin);

scanf("%d",&n);

printf("\nEnter elements of array : \n");

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

fflush(stdin);

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

78
printf("\nArray elements are : ");

display(A,n);

heapSort(A,n);

printf("\nArray elements after sorting are : ");

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 node* next;

};

struct node* createNode(int v);

struct Graph {

int numVertices;

int* visited;

struct node** adjLists;

};

void DFS(struct Graph* graph, int vertex) {

int connectedVertex ;

struct node* adjList = graph->adjLists[vertex];

struct node* temp = adjList;

graph->visited[vertex] = 1;

printf("Visited %d \n", vertex);

while (temp != NULL) {

connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {

DFS(graph, connectedVertex);

80
}

temp = temp->next;

}}

struct node* createNode(int v) {

struct node* newNode =(struct node*) malloc(sizeof(struct node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

struct Graph* createGraph(int vertices) {

int i;

struct Graph* graph =(struct Graph*) malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists =(struct Graph*)malloc(vertices * sizeof(struct node*));

graph->visited = malloc(vertices * sizeof(int));

for (i = 0; i < vertices; i++) {

graph->adjLists[i] = NULL;

graph->visited[i] = 0;

return graph;

void addEdge(struct Graph* graph, int src, int dest) {

struct node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

81
newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

void printGraph(struct Graph* graph) {

int v;struct node* temp;

for (v = 0; v < graph->numVertices; v++) {

temp = graph->adjLists[v];

printf("\n Adjacency list of vertex %d\n ", v);

while (temp) {

printf("%d -> ", temp->vertex);

temp = temp->next;

printf("\n");

}}

int main() {

struct Graph* graph = createGraph(4);

clrscr();

addEdge(graph, 0, 1);

addEdge(graph, 0, 2);

addEdge(graph, 1, 2);

addEdge(graph, 2, 3);

printf("\nDepth First Traversal of the graph is :\n");

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;

};

struct queue* createQueue();

void enqueue(struct queue* q, int);

int dequeue(struct queue* q);

void display(struct queue* q);

83
int isEmpty(struct queue* q);

void printQueue(struct queue* q);

struct node {

int vertex;

struct node* next;

};

struct node* createNode(int);

struct Graph {

int numVertices;

struct node** adjLists;

int* visited;

};

void bfs(struct Graph* graph, int startVertex) {

int currentVertex;

struct node* temp;

struct queue* q = createQueue();

graph->visited[startVertex] = 1;

enqueue(q, startVertex);

while (!isEmpty(q)) {

printQueue(q);

currentVertex = dequeue(q);

printf("Visited %d\n", currentVertex);

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;

}}}

struct node* createNode(int v) {

struct node* newNode = malloc(sizeof(struct node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

struct Graph* createGraph(int vertices) {

int i;

struct Graph* graph = malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));

graph->visited = malloc(vertices * sizeof(int));

for (i = 0; i < vertices; i++) {

graph->adjLists[i] = NULL;

graph->visited[i] = 0;

return graph;

85
}

void addEdge(struct Graph* graph, int src, int dest) {

struct node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

struct queue* createQueue() {

struct queue* q = malloc(sizeof(struct queue));

q->front = -1;

q->rear = -1;

return q;

int isEmpty(struct queue* q) {

if (q->rear == -1)

return 1;

else

return 0;

void enqueue(struct queue* q, int value) {

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 dequeue(struct queue* q) {

int item;

if (isEmpty(q)) {

printf("Queue is empty");

item = -1;

} else {

item = q->items[q->front];

q->front++;

if (q->front > q->rear) {

printf("Resetting queue ");

q->front = q->rear = -1;

}}

return item;

void printQueue(struct queue* q) {

int i = q->front;

if (isEmpty(q)) {

printf("Queue is empty");

} else {

printf("\nQueue contains \n");

87
for (i = q->front; i < q->rear + 1; i++) {

printf("%d ", q->items[i]);

}}}

int main() {

struct Graph* graph = createGraph(6);

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);

printf("\nBreadth first traversal : ");

bfs(graph, 0);

getch();

return 0;

88
PROGRAM-25

25. Write a program to implement Prim’s algorithm.

#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("\n Enter the number of nodes:");

scanf("%d",&n);

printf("\n Enter 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) {

89
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();
}

90
PROGRAM-26

26. Write a program to implement Kruskal algorithm.

#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();

printf("\n\tImplementation of Kruskal's algorithm\n");

printf("\nEnter the no. of vertices:");

scanf("%d",&n);

printf("\nEnter the cost 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;

91
}

printf("The edges of Minimum Cost Spanning Tree are\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

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

if(cost[i][j] < min)

min=cost[i][j];

a=u=i;

b=v=j;

}}}

u=find(u);

v=find(v);

if(uni(u,v))

printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost);

92
getch();

int find(int i)

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

93
94

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy