0% found this document useful (0 votes)
9 views34 pages

DS Labmanual

The document contains source code for three different types of linked list implementations: singly linked list, doubly linked list, and circular linked list. Each implementation includes functions for creation, insertion, deletion, and traversal of the list. The code is structured with a main function that provides a menu for user interaction to perform various operations on the linked lists.
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)
9 views34 pages

DS Labmanual

The document contains source code for three different types of linked list implementations: singly linked list, doubly linked list, and circular linked list. Each implementation includes functions for creation, insertion, deletion, and traversal of the list. The code is structured with a main function that provides a menu for user interaction to perform various operations on the linked lists.
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/ 34

1.

Write a program that uses functions to perform the following operations on singly linked
List.:
i)Creation ii) Insertion iii) Deletion iv) Traversal

Source Code :

#include <stdio.h>
#include <stdlib.h>

/* Declaring Structure of Node*/


struct node
{
int data;
struct node *next;
}*head=NULL;

/*Creating a node*/
struct node *create(int value)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
return newnode;
}
/*Function Declaration*/
void insbeg();
void insend();
void insany();
void display();
void delbeg();
void delend();
void delany();

/**** Insertion of Node ****/


void insbeg()
{
struct node *newnode;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
display();
}

void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
1
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
display();
}

void insany()
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
if(pos==1)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==NULL)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->next=temp1;
}

}
display();
}

/****. Deletion of Node .****/


void delbeg()
2
{
struct node *temp=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp);

display();
}
else
{
head=head->next;
free(temp);
display();
}
}

void delend()
{
struct node *temp2,*temp1=head;

if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();
}
else
{
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp2->next=NULL;
free(temp1);
display();
}
}

void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("\nEnter Position:");
scanf("%d",&pos);

if(head==NULL)
{
printf("List empty\n");
}
3
else if(head->next==NULL)
{
head=NULL;
free(temp1);

}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{

while(temp1->next==NULL)
{
delend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=temp1->next;
temp1->next=NULL;
free(temp1);
display();
}
}
}

/****. Displaying or traversal of the List .****/


void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List Empty\n");
}
else
{
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}

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

/* Main Function or Driver Function */


void main()
{
int a;
while(1)
{
4
printf("\n\t\t1.Insert at beginning\n");
printf("\t\t2.Insert at end\n");
printf("\t\t3.Insert at specified position\n");
printf("\t\t4.Traversal\n");
printf("\t\t5.Delete at beginning\n");
printf("\t\t6.Delete at end\n");
printf("\t\t7.Delete at specified position\n");
printf("\t\t8.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:insbeg();break;
case 2:insend();break;
case 3:insany();break;
case 4:display();break;
case 5:delbeg();break;
case 6:delend();break;
case 7:delany();break;
case 8:exit(0);
default:printf("\n******************( Invalid input )******************\n");
}
}
}

SAMPLE INPUT & OUTPUT

5
2. Write a program that uses functions to perform the following operations on doubly linked
list.:
i)Creation ii) Insertion iii) Deletion iv) Traversal
Source Code :

#include <stdio.h>
#include <stdlib.h>

void insbeg();
void insend();
void insany();
void display();
void delbeg();
void delend();
void delany();

struct node
{
int data;
struct node *next;
struct node *prev;
}*head=NULL;

struct node *create(int value)


{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
newnode->prev=NULL;
return newnode;
}

void insbeg()
{
struct node *newnode;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head->prev=newnode;
head=newnode;
}
display();
}

void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
scanf("%d",&value);
6
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
display();
}

void insany()
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
if(pos==0)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==NULL)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->prev=temp2;
newnode->next=temp1;
temp1->prev=newnode;
}

}
display();
}

7
void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List Empty\n");
}
else
{
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}

printf("%d->NULL\n",temp->data);
}
}

void delbeg()
{
struct node *temp=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp);
display();
}
else
{
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);

display();
}
}

void delend()
{
struct node *temp2,*temp1=head;
if(head==NULL)
{
printf("List Empty");
}
else if(temp1->next==NULL)
{
head=NULL;

free(temp1);
display();
}
else
8
{
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp2=temp1->prev;
temp1->next=NULL;
temp1->prev=NULL;
temp2->next=NULL;
free(temp1);
}
}

void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("Enter position:");
scanf("%d",&pos);

if(head==NULL)
{
printf("List empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();

}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{

while(temp1->next==NULL)
{
delend();break;
}

temp1=temp1->next;
}
temp2=temp1->prev;
temp2->next=temp1->next;
temp1->prev=NULL;
temp1->next=NULL;
free(temp1);
display();

}
}
}
9
void main()
{
int a;
while(1)
{

printf("\n\t\t1.Insert at beginning\n");
printf("\t\t2.Insert at end\n");
printf("\t\t3.Insert at specified position\n");
printf("\t\t4.Traversal\n");
printf("\t\t5.Delete at beginning\n");
printf("\t\t6.Delete at end\n");
printf("\t\t7.Delete at specified position\n");
printf("\t\t8.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:insbeg();break;
case 2:insend();break;
case 3:insany();break;
case 4:display();break;
case 5:delend();break;
case 6:delany();break;
case 7:display();break;
case 8:exit(0);
default:printf("\n******************( Invalid input )******************\n");

}
}
}
SAMPLE INPUT & OUTPUT

10
3. Write a program that uses functions to perform the following operations on circular linked
List.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
Source Code :

#include <stdio.h>
#include <stdlib.h>

void delbeg();
void delend();
void delany();
void insbeg();
void insend();
void insany();
void display();

struct node
{
int data;
struct node *next;

}*head=NULL;

struct node *create(int value)


{
11
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=NULL;
return newnode;
}

void insbeg()
{
struct node *newnode,*temp;
temp=head;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
newnode->next=head;
while(temp->next!=head)
{
temp=temp->next;
}
head=newnode;
temp->next=head;
}
display();
}

void insend()
{
struct node *newnode,*temp;
int value;
printf("Enter node data:");
scanf("%d",&value);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
}
display();
}

void insany()
12
{
struct node *newnode,*temp1,*temp2;
temp1=head;
int value,pos,i;
printf("Enter node data:");
scanf("%d",&value);
printf("\nEnter Position:");
scanf("%d",&pos);
newnode=create(value);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
if(pos==0)
{
insbeg();
}
else
{
for(i=0;i<pos-1;i++)
{
if(temp1->next==head)
{
insend();break;
}
temp2=temp1;
temp1=temp1->next;
}
temp2->next=newnode;
newnode->next=temp1;
}

}
display();
}

void display()
{
struct node *temp;
temp=head;
if(temp==NULL)
{
printf("List Empty\n");
}
else
{
while(temp->next!=head)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->",temp->data);
printf("NULL\n");
}
}

13
void delbeg()
{
struct node *temp=head;
if(head==NULL)
{
printf("List empty");
}
else if(head->next==NULL)
{
head=NULL;
free(temp);
display();
}
else
{
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);

display();
}
}

void delend()
{
struct node *temp2,*temp1=head;
if(head==NULL)
{
printf("List Empty");
}
else if(temp1->next==NULL)
{
head=NULL;

free(temp1);
display();
}
else
{
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp2=temp1->prev;
temp1->next=NULL;
temp1->prev=NULL;
temp2->next=NULL;
free(temp1);
}
}

void delany()
{
int pos,i;
struct node *temp1=head,*temp2;
printf("Enter position:");
14
scanf("%d",&pos);

if(head==NULL)
{
printf("List empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(temp1);
display();

}
else
{
if(pos==1)
{
delbeg();
}
else
{
for(i=1;i<pos;i++)
{

while(temp1->next==NULL)
{
delend();break;
}

temp1=temp1->next;
}
temp2=temp1->prev;
temp2->next=temp1->next;
temp1->prev=NULL;
temp1->next=NULL;
free(temp1);

display();

}
}
}

int main()
{
int a;
while(1)
{
printf("\n\t\t1.Insert at beginning\n");
printf("\t\t2.Insert at end\n");
printf("\t\t3.Insert at specified position\n");
printf("\t\t4.Traversal\n");
printf("\t\t5.Delete at beginning\n");
printf("\t\t6.Delete at end\n");
printf("\t\t7.Delete at specified position\n");
printf("\t\t8.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
15
switch(a)
{
case 1:insbeg();break;
case 2:insend();break;
case 3:insany();break;
case 4:display();break;
case 5:delbeg();break;
case 6:delend();break;
case 7:delany();break;
case 8:exit(1);
default:printf("Invalid Input");
}
}
}

SAMPLE INPUT & OUTPUT

4. Write a program that implement stack (its operations) using


i)Arrays ii) Pointer

Source Code :
#include <stdio.h>
#include <stdlib.h>
#define size 100

int stack[size],top=-1,i,n; //Global Variables

void stkarr();
void stkpoi();

void stkarr() //STACK USING ARRAY


{
auto void push();
auto void pop();
16
auto void display();

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


scanf("%d",&n);
int a;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4: return ; //Returns to main function
default:printf("Invalid Input");
}
}

void push()
{
int value;
printf("Enter data:");
scanf("%d",&value);
if(top==-1)
stack[++top]=value;
else if(top==n-1)
printf("****Stack overflow****\n");
else
stack[++top]=value;

printf("stack elements:\n");
display();
}

void pop()
{
if(top==-1)
printf("****Stack underflow****\n");
else if(top==n)
top=-1;
else
top=--top;

display();
}

void display()
{
int i;
if(top==-1)
printf("Stack Empty\n");
else
for(i=top;i>=0;i--)
printf("\t\t_%d_\n",stack[i]);
}
}

17
void stkpoi() //STACK USING POINTER
{

auto void push(int *x,int value,int y);


auto void pop(int *x,int y);
auto void display(int *x,int y);

int n,a,value;
printf("Enter the size of stack:");
scanf("%d",&n);
int stack[n];
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 1: printf("Enter data:");
scanf("%d",&value);
push(stack,value,n);break;
case 2: pop(stack,n);break;
case 3: display(stack,n);break;
case 4: return ; //returns to main function
default:printf("Invalid Input\n");
}

void push(int *x,int value,int y)


{

if(top==-1)
x[++top]=value;
else if(top==y-1)
printf("****Stack overflow****\n");
else
x[++top]=value;

printf("stack elements:\n");
display(x,y);
}

void pop(int*x,int y)
{
if(top==-1)
printf("****Stack underflow****\n");
else
top--;

display(x,y);
}

void display(int *x,int y)


{
int i;

if(top==-1)
18
printf("Stack Empty\n");
else
for(i=top;i>=0;i--)
printf("\t\t_%d_\n",x[i]);
}
}

void main()
{
int s;
while(1)
{
printf("\n1.Stack using array");
printf("\n2.Stack using Pointers");
printf(“\n3.Exit”);
printf("\nEnter your choice :");
scanf("%d",&s);
switch(s)
{
case 1:stkarr();break;
case 2:stkpoi();break;
case 3:exit(0);
default:printf("Invalid Input");
}
}
}

SAMPLE INPUT & OUTPUT

5. Write a program that implement Queue (its operations) using


i)Arrays ii) Pointers

Source Code :
#include <stdio.h>
#include <stdlib.h>
#define size 100

int rear=-1,front=-1;

void qarr();
19
void qpoi();

void qarr() //QUEUE USING ARRAY


{
int queue[size],n,i;
auto void enqueue();
auto void dequeue();
auto void display();

int a;
printf("Enter the size of queue:");
scanf("%d",&n);
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:enqueue();break;
case 2:dequeue();break;
case 3:display();break;
case 4:return;
default:printf("Invalid choice\n");
}
}

void enqueue()
{
int value;
printf("Enter the data:");
scanf("%d",&value);
if(rear==-1 && front==-1)
rear=++rear,front=rear,queue[front]=value;
else if(rear==n-1)
printf("****queue overflow****\n");
else
queue[++rear]=value;

display();
}

void dequeue()
{
if(front==-1 && rear==-1)
printf("Queue underflow\n");
else if(front==rear)
front=-1,rear=-1;
else
front=++front;

display();
}

void display()
{
int i;
if(rear==-1 && front==-1)
printf("Queue Empty\n");
20
else
for(i=rear;i>=front;i--)
printf("->|%d|",queue[i]);
}
}

void qpoi() //QUEUE USING POINTER


{
auto void enqueue(int *x,int n,int value);
auto void dequeue(int *x);
auto void display(int *x);

int a,n,value;
printf("Enter the size of queue:");
scanf("%d",&n);
int stack[n];
while(1)
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter data:");
scanf("%d",&value);
enqueue(stack,n,value);break;
case 2:dequeue(stack);break;
case 3:display(stack);break;
case 4:return ;
default:printf("Invalid choice\n");
}
}

void enqueue(int *x,int n,int value)


{
if(rear==-1 && front==-1)
rear=++rear,front=rear,x[front]=value;
else if(rear==n-1)
printf("****queue overflow****\n");
else
x[++rear]=value;

display(x);
}

void dequeue(int *x)


{
if(front==-1 && rear==-1)
printf("Queue underflow\n");
else if(front==rear)
front=-1,rear=-1;
else
front=++front;

display(x);
}

void display(int *x)


21
{
int i;
if(rear==-1 && front==-1)
printf("Queue Empty\n");
else
for(i=rear;i>=front;i--)
printf("->|%d|",x[i]);

void main()
{
int s;
while(1)
{
printf("\n1.Queue using array");
printf("\n2.Queue using Pointers");
printf(“\n3.Exit”);
printf("\nEnter your choice :");
scanf("%d",&s);
switch(s)
{
case 1:qarr();break;
case 2:qpoi();break;
case 3:exit(0);
default:printf("Invalid Input");
}
}
}
SAMPLE INPUT & OUTPUT

6. Write a program that implements the following sorting methods to sort a given list of
integers in ascending order
i)Bubble sort ii) Selection sort iii) Insertion sort.
Source Code :
#include <stdio.h>
void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);
void swap(int *a,int *b)

22
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
void bubble_sort(int a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j+1]<a[j])
swap(&a[j+1],&a[j]);
}
}
printf("\nsorted Array using bubble sort :");
for(i=0;i<n;i++)
printf("%d ",a[i]);

void insertion_sort(int a[],int n)


{
int i, key, j;
for (i = 1; i < n; i++)
{
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
printf("sorted Array using insertion sort :");
for(i=0;i<n;i++)
printf("%d ",a[i]);

}
void selection_sort(int a[],int n)
{
int i,j,min_idx;
for(i = 0; i < n - 1; i++)
{
min_idx=i;
for(j = i + 1; j < n; j++)
{
if(a[min_idx] > a[j])
min_idx=j;
}
if(min_idx != i)
swap(&a[i],&a[min_idx]);
}
printf("sorted array using selection sort :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
23
}
void main()
{
int n,i;
printf("Enter the size of array :");
scanf("%d",&n);
int a[n];
printf("Enter array elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(1)
{
int c;
printf("\n\n1.Bubble Sort");
printf("\n2.Selection Sort");
printf("\n3.Insertion Sort");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:bubble_sort(a,n);break;
case 2:selection_sort(a,n);break;
case 3:insertion_sort(a,n);break;
case 4:exit(0);
default:printf("\nInvalid Input");
}

}
}

SAMPLE INPUT & OUTPUT

7. Write a program that use both recursive and non recursive functions to perform the
Following searching operations for a Key value in a given list of integers:
i)Linear search ii) Binary search
Source Code :
#include<stdio.h>
24
#include<stdlib.h>
void linsearch(int a[],int size);
void binsearch(int a[],int size);
void insertion_sort(int a[],int n);
void lin_search_rec(int a[],int i,int key,int size);
void bin_search_rec(int a[],int beg,int key,int end);

void main()
{

int ch,n,i,key,t;
printf("Enter the size of list :");
scanf("%d",&n);
int a[n];
printf("Enter list elements :",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);

insertion_sort(a,n);

while(1)
{
printf("\n 1.Linear Search\n 2.Binary Search\n");
printf("3.Recursive Linear Search");
printf("\n4.Recursive Binary Search");
printf("\n5.Exit");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:linsearch(a,n);break;
case 2:binsearch(a,n);break;
case 3:printf("Enter the key to be searched :");
scanf("%d",&key);
lin_search_rec( a,0, key, n);
case 4:printf("Enter the key to be searched :");
scanf("%d",&key);

bin_search_rec( a,0, key, n-1);

case 5:exit(0);
}
}
}

void insertion_sort(int a[],int n)


{
int i, k, j;
for (i = 1; i < n; i++)
{
k = a[i];
j = i - 1;
while (j >= 0 && a[j] > k)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = k;
}
25
}

void linsearch(int a[],int n)


{
int i,key;

printf("Enter the key to be searched :");


scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\t\t---key found---\n");
break;
}
}
if(i==n)
printf("\t\t---key not found---\n");
}

void binsearch(int a[],int n)


{
int i=0,key,first=0,last=n,mid;
mid=first+last/2;

printf("Enter the key to be searched :");


scanf("%d",&key);

if(key==a[mid])
{
printf("\t\t---key found---");
}
else if(a[mid]>key)
{
for(i=0;i<mid;i++)
{
if(a[i]==key)
{
printf("\t\t---key found---\n");
break;
}
}
if(i==mid)
printf("\t\t---key not found---");
}
else
{
for(i=mid;i<n;i++ )
{
if(a[i]==key)
{
printf("\t\t---key found---\n");
break;
}
}
if(i==n)
printf("\t\t---key not found---\n");
26
}
}

void lin_search_rec(int a[],int i,int key,int size)


{

if(a[i]==key)
{
printf("\t\t---KEY FOUND---");

}
else if(i+1!=size)
{
lin_search_rec( a,i+1,key, size);
}
else
{
printf("\t\t---KEY NOT FOUND");

}
}

void bin_search_rec(int a[],int beg,int key,int end)


{
int mid;
mid=beg+end/2;
if(beg<=end)
{
if(a[mid]==key)
{
printf("\t\t--KEY FOUND--");
}
else if(a[mid]<key)
{
printf("%d,%d,%d",a[mid],key,end);
bin_search_rec(a,mid+1,key,end);
}
else if(a[mid]>key)
{
// printf("%d,%d,%d",a[mid],key,end);
bin_search_rec(a,0,key,mid-1);
}
}
else
{
printf("key not found ");
}
}
SAMPLE INPUT & OUTPUT

27
8. Write a program to implement the tree traversal methods.
Source Code :

#include <stdio.h>
#include <stdlib.h>

struct bstnode
{
int data;
struct bstnode *lchild;
struct bstnode *rchild;
}*root=NULL;

struct bstnode *bstcreate(int value)


{
struct bstnode *newnode;
newnode=(struct bstnode*)malloc(sizeof(struct bstnode));
newnode->lchild=NULL;
newnode->rchild=NULL;
newnode->data=value;
return newnode;
}

void insert(struct bstnode *temp,struct bstnode *newnode);


void inorder(struct bstnode*temp);
void postorder(struct bstnode *temp);
void preorder(struct bstnode *temp);

void main()
{
int a,value,key;
struct bstnode *newnode;

while(1)
{
printf("\n");
printf("\t\t1.Insert element into bst\n");
printf("\t\t2.inorder\n");
printf("\t\t3.post-order\n");
printf("\t\t4.pre-order\n");
printf("\t\t5.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&a);

switch(a)
{
case 1: printf("Enter Node data: ");
scanf("%d",&value);
newnode=bstcreate(value);
if(root==NULL)
{
root=newnode;
}
else
{
struct bstnode *temp;
temp=root;
insert(temp,newnode);
}break;
28
case 2:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
inorder(root);
}break;
case 3:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
postorder(root);
}break;
case 4:if(root==NULL)
{
printf("\n******************( TREE IS EMPTY )******************\n\n");
}
else
{
preorder(root);
}break;

case 5:exit(1);
default:printf("\n******************( Invalid input )******************\n");
}
}
}

void insert(struct bstnode *temp,struct bstnode *newnode)


{
if(newnode->data>temp->data)
{
if(temp->rchild==NULL)
{
temp->rchild=newnode;
}
else
{
insert(temp->rchild,newnode);
}
}
else
{
if(temp->lchild==NULL)
{
temp->lchild=newnode;
}
else
{
insert(temp->lchild,newnode);
}
}
}
void inorder(struct bstnode *temp)
{
if(temp!=NULL)
29
{
inorder(temp->lchild);
printf("%d->",temp->data);
inorder(temp->rchild);

}
}

void preorder(struct bstnode *temp)


{
if(temp!=NULL)
{
printf("%d->",temp->data);
preorder(temp->lchild);
preorder(temp->rchild);

}
}

void postorder(struct bstnode *temp)


{
if(temp!=NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d->",temp->data);
}
}

void search(struct bstnode *temp,int key,struct bstnode *parent)


{

if(temp==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
else if(key==temp->data)
{
printf("key found at node %d\n",parent->data);
}
else if(key>temp->data)
{
search(temp->rchild,key,temp);
}
else if(key<temp->data)
{
search(temp->lchild,key,temp);
}
}
void del(struct bstnode *temp,int key,struct bstnode *parent)
{
struct bstnode*temp1=temp;
if(key==temp->data)
{
if(temp->rchild==NULL && temp->lchild==NULL)
{
if(parent->data>key)
{
parent->lchild=NULL;
30
free(temp);
}
else if(parent->data<key)
{
parent->rchild=NULL;
free(temp);
}
else
{

root=NULL;
free(temp);
}
}
else if(temp->rchild==NULL || temp->lchild==NULL)
{
if(temp->rchild==NULL)
{
if(parent->data>key)
{
parent->lchild=temp->lchild;
temp->lchild=NULL;
free(temp);
}
else
{
parent->rchild=temp->lchild;
temp->lchild=NULL;
free(temp);
}
}
else
{
if(parent->data>key)
{
parent->lchild=temp->rchild;
temp->rchild=NULL;
free(temp);
}
else
{
parent->rchild=temp->rchild;
temp->rchild=NULL;
free(temp);
}
}
}
else if(temp->rchild!=NULL && temp->lchild!=NULL)
{
temp1=findmin(temp->rchild);
temp->data=temp1->data;
del(temp->rchild,temp1->data,temp);
}
}
else if(key<temp->data)
{ if(temp->lchild==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
31
else
{
del(temp->lchild,key,temp);
}
}
else if(key>temp->data)
{
if(temp->rchild==NULL)
{
printf("\n******************( KEY NOT FOUND )******************\n");
}
else
{
del(temp->rchild,key,temp);
}
}
}

struct bstnode *findmin(struct bstnode*temp)


{
if(temp->lchild!=NULL)
{
findmin(temp->lchild);
}
else
{
return temp;
}
}
SAMPLE INPUT & OUTPUT

9. Write a program to implement the graph traversal methods


Source Code:
#include <stdio.h>
#include <stdlib.h>
int bfs_que[20],bfs_f=-1,bfs_r=-1;
void bfs(int v);
void dfs(int v);
32
int dfs_gph[20][20],vis[20],n;

void main()
{
int i,j,s,t;

while(1)
{

printf("1.BFS\n2.DFS\n3.Re-enter data\n4.Exit\n");
printf("Enter the traversal operation to be performed:");
scanf("%d",&t);
printf("Enter the no.of vertices:");
scanf("%d",&n);
printf("\nEnter Graph:\n");
for(i=1;i<=n;i++)
{
vis[i]=0;//intializing array
for(j=1;j<=n;j++)
{
scanf("%d",&dfs_gph[i][j]);
}
}

printf("Enter the starting vertex:");


scanf("%d",&s);
switch(t)
{

case 1:bfs(s);break;
case 2:dfs(s);break;
case 4:exit(1);
default:printf("Invalid choice \n");
}

void dfs(int v)
{
int i;
vis[v]=1;
for(i=1;i<=n;i++)
{
if( dfs_gph[v][i]==1 && vis[i]==0 )
{
printf("%d->%d\n",v,i);
dfs(i);
}
}

void bfs(int v)
{
int i,j=1;
vis[v]=1;
33
bfs_que[bfs_r+1]=v;
while(j<n)
{
for(i=1;i<=n;i++)
{
if(dfs_gph[v][i]==1&&vis[i]==0)
{
vis[i]=1;
bfs_que[bfs_r]=i;
printf("%d-->%d\n",v,bfs_que[bfs_r++]);
}
}

j++;
}
if(bfs_f<n)
bfs(bfs_que[bfs_f++]);
}
SAMPLE INPUT & OUTPUT

34

You might also like