0% found this document useful (0 votes)
73 views58 pages

RUSHIL Combined

The document contains details about a student named Ayush Pathak studying in the third semester of B.Tech in AI & ML branch at Ajay Kumar Garg Engineering College. It lists 29 experiments related to data structures laboratory subject with program numbers and space to sign after completing each experiment.

Uploaded by

Ayush Pathak
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)
73 views58 pages

RUSHIL Combined

The document contains details about a student named Ayush Pathak studying in the third semester of B.Tech in AI & ML branch at Ajay Kumar Garg Engineering College. It lists 29 experiments related to data structures laboratory subject with program numbers and space to sign after completing each experiment.

Uploaded by

Ayush Pathak
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/ 58

AJAY KUMAR GARG ENGINEERING COLLEGE

GHAZIABAD

COURSE - B.TECH
BRANCH - AIML
SEMESTER - III
SECTION - AIML
SUBJECT - DATA STRUCTURE LAB
SUBJECT CODE- KAS -351

SUBMITTED BY:- SUBMITTED TO: -


AYUSH PATHAK
2100271640017
AJAY KUMAR GARG ENGINEERING COLLEGE
GHAZIABAD
Department of Computer Science and Engineering (AI & ML)
LIST OF EXPERIMENTS
SNO. PROGRAM DATE SIGNATURE
1 Write a program in C to implement
traversing in array.
2 Write a program in C to insert an
element in unsorted array.
3 Write a program in C to insert an
element in sorted array.
4 Write a program in C to delete an
element from array.
5 Write a program in C to implement
linear search.
6 Write a program in C to implement
binary search.
7 Write a program in C to implement
stack operation using array.
8 Write a program in C to implement
stack operation using linked list.
9 Write a program in C to implement
Fibonacci series.
10 Write a program in C to implement
tail and head recursion.
11 Write a program in C to calculate
factorial of a number using recursion
and iteration.
12 Write a program in C to implement
queue operation using array.
13 Write a program in C to implement
queue operation using linked list.
14 Write a program in C to implement
circular queue operation using array.
15 Write a program in C to implement
circular queue operation using
linked list.
16 Write a program in C to implement
bubble sort.
17 Write a program in C to implement
quick sort.
18 Write a program in C to implement
selection search.
19 Write a program in C to implement
insertion search.
20 Write a program in C to implement
Tower of Hanoi.
21 Write a program in C to implement
operations on single linked list.
22 Write a program in C to implement
operations on circular linked list.
23 Write a program in C to implement
operations on double linked list.
24 Write a program in C to implement
binary tree using linked list.
25 Write a program in C to implement
binary search tree using linked list.
26 Write a program in C to implement
tree traversal using linked list.
27 Write a program in C to implement
BFS using linked list.
28 Write a program in C to implement
DFS using linked list.
29 Write a program in C to implement
graph traversal using linked list.
Program No. 6
Objective: Write a program to implement stack using array.
Code:
#include<stdio.h>
void push();
void pop();
void peek();
void display();
void Exit();
int top=-1;
int ch;
int stack[10];
void main()
{ ch=1;
while(ch>0)
{ printf("\n\n\tSTACK BUILDER");
printf("\n\t1. Push");
printf("\n\t2. Pop");
printf("\n\t3. Peek");
printf("\n\t4. Display");
printf("\n\t5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{ case 1 : printf("Push Function Selected.");
push();
break;
case 2 : printf("Pop Function Selected.");
pop();
break;
case 3 : printf("Peek Function Selected.");
peek();
break;
case 4 : printf("Displaying All Elements.....");
display();
break;
case 5 : printf("Exiting Program...");
Exit();
break;
default : printf("INVALID INPUT...");
printf("Enter Valid Choice.");
break;
}
}
}
void push()
{ int n;
if(top==(10-1))
printf("\nOVERFLOW or Stack is FULL");
else
{ printf("\nEnter An Element to Push : ");
scanf("%d",&n);
top+=1;
stack[top]=n;
printf("\nElement Successfully Pushed into Stack.");
}
}
void pop()
{ if(top==-1)
printf("\nStack is Empty.");
else
{ printf("\n%d",stack[top]);
top-=1;
printf("\nElement Popped Successfully.");
}
}
void peek()
{ if(top==-1)
printf("\nStack is Empty.");
else
{ printf("\n%d",stack[top]);
printf("\nElement Peeked Successfully.");
}
}
void display()
{ int i;
for(i=top;i>-1;i--)
printf("\n%d",stack[i]);
printf("\nAll Elements Displayed.");
}
void Exit()
{ ch=0;
printf("\nProgram Terminated Successfully.");
}
Output :
Program No- 7
Objective : Write a program to implement stack using linked lists.
Code :

#include<stdio.h>
#include<stdlib.h>
void create(int n);
void insertstart();
void insertend();
void insertrandom();
void display();
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void main()
{
int ch=1;
while(ch>0)
{
printf("\n\t\tMAIN MENU");
printf("\n\t1. Create Link List");
printf("\n\t2. Insert Link List");
printf("\n\t3. Display Link List");
printf("\n\t4. Exit");
printf("\n\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter Number of Nodes : ");
int n;
scanf("%d",&n);
create(n);
break;

case 2:
printf("\n1. Insert at Beginning");
printf("\n2. Insert at Last");
printf("\n3. Insert at Given Position");
printf("\nEnter Your Choice : ");
int x;
scanf("%d",&x);
if(x==1)
{
insertstart();
break;
}
else if(x==2)
{
insertend();
break;
}
else if(x==3)
{
insertrandom();
break;
}
else
{
printf("Enter Valid Choice.");
break;
}

case 3:
display();
break;

case 4:
printf("\n");
ch=0;
break;
}
}
printf("EXIT!");
}
void create(int n)
{
struct node *temp,*new;
int data,i;
head = (struct node*)malloc(sizeof(struct node));
printf("Enter Data Value of Node 1 : ");
scanf("%d",&data);
head->data=data;
head->next = NULL;
temp = head;
for(i=2;i<=n;i++)
{
new = (struct node*)malloc(sizeof(struct node));
printf("Enter Data Value of Node %d : ",i);
scanf("%d",&data);
new->data = data;
new->next = NULL;
temp->next = new;
temp = temp->next;
}
}
void insertstart()
{
struct node *temp = (struct node* )malloc(sizeof(struct node));
int data;
printf("Enter Data Value : ");
scanf("%d",&data);
temp->data = data;
temp->next = head;
head = temp;

}
void insertend()
{
struct node *ptr,*temp;
int data;
ptr = (struct node*)malloc(sizeof(struct node));
printf("\nEnter Data Value : ");
scanf("%d",&data);
ptr->data = data;
if(head == NULL)
{
ptr->next = NULL;
head = ptr;
printf("\nNode Inserted.");
}
else
{
temp = head;
while (temp->next != NULL)
{
temp=temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode Inserted.");
}
}
void insertrandom()
{
int i,pos,data;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
printf("\nEnter Data Value : ");
scanf("%d",&data);
ptr->data = data;
printf("\nEnter the Position At Which You Want To Insert : ");
scanf("%d",&pos);
temp=head;
for(i=1;i<=(pos-1);i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\nData Can't be Inserted.");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode Inserted");
}
void display()
{
struct node *temp;
temp = head;
while(temp!= NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
}

Output :
Program No.-9
Objective- Write a program to implement the Fibonacci series.

CODE-

#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;

}
OUTPUT:-
Program No.-10
Objective -Write a program to implement the tail and head recursion.
CODE

//Head recursion
#include <stdio.h>
void fun(int n)
{
if (n > 0) {
fun(n - 1);
printf("%d ", n);
}
}
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

//Tail Recursion
#include <stdio.h>
// Recursion function
void fun(int n)
{
if (n > 0) {
printf("%d ", n);
fun(n - 1);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}

OUTPUT:

Tail Recursion

Head recursion
Program No.-11
Write a program to calculate the factorial of the number using recursion
and iteration.
Using Iteration
#include<stdio.h>

int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);

for(i=1;i<=number;i++)
{ fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);

return 0;

Using Recursion

#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);

fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}

OUTPUT:-
Using Iteration

Using Recursion
Program No.-14
Write a program to implement circular queue operations using array.
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
// function to delete the element from the queue
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
while(choice<4 && choice!=0)
{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
OUTPUT:-
Program No.-15
Write a program to implement circular queue operations using linked list.
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front=-1;
struct node *rear=-1;
// function to insert the element in the Queue
void enqueue(int x)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
if(rear==-1) // checking whether the Queue is empty or not.
{
front=rear=newnode;
rear->next=front;
}
else
{
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
void dequeue()
{
struct node *temp;
temp=front;
if((front==-1)&&(rear==-1))
{
printf("\nQueue is empty");
}
else if(front==rear)
{
front=rear=-1;
free(temp);
}
else
{
front=front->next;
rear->next=front;
free(temp);
}
}
int peek()
{
if((front==-1) &&(rear==-1))
{
printf("\nQueue is empty");
}
else
{
printf("\nThe front element is %d", front->data);
}
}
void display()
{
struct node *temp;
temp=front;
printf("\n The elements in a Queue are : ");
if((front==-1) && (rear==-1))
{
printf("Queue is empty");
}

else
{
while(temp->next!=front)
{
printf("%d,", temp->data);
temp=temp->next;
}
printf("%d", temp->data);
}
}
void main()
{
enqueue(34);
enqueue(10);
enqueue(23);
display();
dequeue();
peek();
}
OUTPUT:-
Program No.-16

Objective- Write a program to implement bubble sort.


Code-
#include<stdio.h>
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}

OUTPUT:-
Program No.-17
Write a program to implement quick sort.

#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot
element int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0; }
OUTPUT:-
Program No.-18
Write a program to implement selection sort.
#include <stdio.h>
void selection(int arr[], int n)
{

int i, j, small;
for (i = 0; i < n-1; i++)
{

small = i;
for (j = i+1; j < n; j++) if
(arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i]; arr[i]
= temp;
}
}

void printArr(int a[], int n) /* function to print the array */


{

int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);

int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);

selection(a, n);

printf("\nAfter sorting array elements are - \n");


printArr(a, n);
return 0;
}
OUTPUT:-
Program No. 19
Objective: Write a program in C to implement Tower of Hanoi.
Code:
#include <stdio.h>
#include <conio.h>
void hanoi(char,char,char,int);
void main()
{
int num;
clrscr();
printf("\nENTER NUMBER OF DISKS: ");
scanf("%d",&num);
printf("\nTOWER OF HANOI FOR %d NUMBER OF DISKS:\n", num);
hanoi('A','B','C',num);
getch();
}
void hanoi(char from,char to,char other,int n)
{
if(n<=0)
printf("\nILLEGAL NUMBER OF DISKS");
if(n==1)
printf("\nMOVE DISK FROM %c TO %c",from,other);
if(n>1)
{
hanoi(from,other,to,n-1);
hanoi(from,to,other,1);
hanoi(to,from,other,n-1);
}
}
Output:
Program No. 20

Objective: Write a C program to implement operations on single linked list.


#include <stdio.h>
struct node{
int val;
struct node *next;
};
void print_list(struct node *head)
{
printf("H->");

while(head)
{
printf("%d->", head->val);
head = head->next;
}

printf("|||\n");
}

void insert_front(struct node **head, int value)


{
struct node * new_node = NULL;

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

if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
}

new_node->val = value;

new_node->next = *head;

*head = new_node;
}

void main()
{
int count = 0, i, val;
struct node * head = NULL;

printf("Enter number of elements: ");


scanf("%d", &count);

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


{
printf("Enter %dth element: ", i);
scanf("%d", &val);
insert_front(&head, val);
}

printf("Linked List: ");


print_list(head);
}
Output:
Program No. 21
Objective: Write a program in C to implement operations on circular linked list.
#include <stdio.h>

struct node
{ int data;
int key;

struct node *next;


};

struct node *head = NULL;


struct node *current = NULL;

bool isEmpty() {
return head == NULL;
}

int length() {
int length = 0;

if(head == NULL) {
return 0;
}

current = head->next;

while(current != head) {
length++;
current = current->next;
}

return length;
}

void insertFirst(int key, int data) {

struct node *link = (struct node*) malloc(sizeof(struct


node)); link->key = key;
link->data = data;

if (isEmpty()) {
head = link;
head->next = head;
} else {
link->next = head;

//point first to new first node


head = link;
}
}

struct node * deleteFirst() {

struct node *tempLink = head;

if(head->next == head) {
head = NULL;
return tempLink;
}

head = head->next;

return tempLink;
}

void printList() {

struct node *ptr = head;


printf("\n[ ");

if(head != NULL) {

while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

printf(" ]");
}

void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

printf("Original List: ");


printList();

while(!isEmpty()) {
struct node *temp = deleteFirst();
printf("\nDeleted value:"); printf("(%d,
%d) ",temp->key,temp->data);
}

printf("\nList after deleting all items: ");


printList();
}

Output:
Program No. 22
Objective: Write a program in C to implement operations on double linked list.
#include <stdio.h>
struct node {
int data;
int key;
struct node *next;
struct node *prev;
};
struct node *head = NULL;
struct node *last = NULL;
struct node *current = NULL;
bool isEmpty() {
return head == NULL;
}
int length() {
int length = 0;
struct node *current;
for(current = head; current != NULL; current = current->next){
length++;
}
return length;
}
void displayForward() {
struct node *ptr = head;
printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
void displayBackward() {
struct node *ptr = last;
printf("\n[ ");
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr ->prev;
}
}
void insertFirst(int key, int data) {
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
head->prev = link;
}
link->next = head;
head = link;
}
void insertLast(int key, int data) {
struct node *link = (struct node*) malloc(sizeof(struct
node)); link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
last->next = link;
link->prev = last;
}
last = link;
}
struct node* deleteFirst() {
struct node *tempLink = head;
if(head->next == NULL){
last = NULL;
} else {
head->next->prev = NULL;
}
head = head->next;
return tempLink;
}
struct node* deleteLast() {
struct node *tempLink = last;
if(head->next == NULL) {
head = NULL;
} else {
last->prev->next = NULL;
}
last = last->prev;
return tempLink;
}
struct node* delete(int key) {
struct node* current = head;
struct node* previous = NULL;
if(head == NULL) {
return NULL;
}
while(current->key != key) {
if(current->next == NULL) {
return NULL;
} else {
previous = current;
current = current->next;
}
}
if(current == head) {
head = head->next;
} else {
current->prev->next = current->next;
}
if(current == last) {
last = current->prev;
} else {
current->next->prev = current->prev;
}
return current;
}
bool insertAfter(int key, int newKey, int data) {
struct node *current = head;
if(head == NULL) {
return false;
}
while(current->key != key) {
if(current->next == NULL) {
return false;
} else {
current = current->next;
}
}
struct node *newLink = (struct node*) malloc(sizeof(struct node));
newLink->key = newKey;
newLink->data = data;
if(current == last) {
newLink->next = NULL;
last = newLink;
} else {
newLink->next = current->next;
current->next->prev = newLink;
}
newLink->prev = current;
current->next = newLink;
return true;
}
void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("\nList (First to Last): ");
displayForward();
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
printf("\nList , after deleting first record: ");
deleteFirst();
displayForward();
printf("\nList , after deleting last record: ");
deleteLast();
displayForward();
printf("\nList , insert after key(4) : ");
insertAfter(4,7, 13);
displayForward();
printf("\nList , after delete key(4) : ");
delete(4);
displayForward();
}

Output:
Program No. 23
Objective: Write a program in C to implement binary tree using linked list.
#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node *root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index ) {
struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct
node ) ); temp->left = constructTree(
leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root ) {
if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}

Output:
Program No. 24
Objective: Write a program in C to implement binary search tree using
linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right_child;
struct node *left_child;
};
struct node* new_node(int x) {
struct node *temp;
temp = malloc(sizeof(struct node));
temp -> data = x;
temp -> left_child = NULL;
temp -> right_child = NULL;
return temp;
}
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x)
return root;
else if (x > root -> data)
return search(root -> right_child, x);
else
return search(root -> left_child, x);
}
struct node* insert(struct node * root, int x) {
if (root == NULL)
return new_node(x);
else if (x > root -> data)
root -> right_child = insert(root -> right_child, x);
else
root -> left_child = insert(root -> left_child, x);
return root;
}
struct node* find_minimum(struct node * root) {
if (root == NULL)
return NULL;
else if (root -> left_child != NULL)
return find_minimum(root -> left_child);
return root;
}
struct node* delete(struct node * root, int x) {
if (root == NULL)
return NULL;
if (x > root -> data)
root -> right_child = delete(root -> right_child, x);
else if (x < root -> data)
root -> left_child = delete(root -> left_child, x);
else {
if (root -> left_child == NULL && root -> right_child == NULL) {
free(root);
return NULL;
}
else if (root -> left_child == NULL || root -> right_child == NULL) {
struct node *temp;
if (root -> left_child == NULL)
temp = root -> right_child;
else
temp = root -> left_child;
free(root);
return temp;
}
else {
struct node *temp = find_minimum(root -> right_child);
root -> data = temp -> data;
root -> right_child = delete(root -> right_child, temp -> data);
}
}
return root;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root -> left_child);
printf(" %d ", root -> data);
inorder(root -> right_child);
}
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);
inorder(root); printf("\
n");
return 0;
}

Output:
Program No. 25
Objective: Write a program in C to implement Tower of Hanoi.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;};
struct node *root =
NULL; void insert(int
data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;
if(data < parent->data) {
current = current->leftChild;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
else {
current = current->rightChild;
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}}
struct node* search(int data)
{ struct node *current =
root; printf("Visiting
elements: ");

while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
if(current->data > data) {
current = current->leftChild;
}
else {
current = current->rightChild;
}
if(current == NULL) {
return NULL;
}
}
return current;}
void pre_order_traversal(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}}
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
}}
void post_order_traversal(struct node* root) {
if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->data);
}}
int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };

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


insert(array[i]);

i = 31;
struct node * temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}
i = 15;
temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}

printf("\nPreorder traversal: ");


pre_order_traversal(root);

printf("\nInorder traversal: ");


inorder_traversal(root);

printf("\nPost order traversal: ");


post_order_traversal(root);

return 0;}

Output:
Program No. 26
Objective: Write a program in C to implement BFS using linked list.
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++) if(a[v]
[i] && !visited[i]) q[+
+r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i); else
printf("\n Bfs is not possible");
getch();
}

Output:
Program No. 27
Objective: Write a program in C to implement DFS using linked list.
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v) {
int i;
reach[v]=1;
for (i=1;i<=n;i++) if(a[v]
[i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main() {
int i,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++) {
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++) {
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
Output:

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