sebcodes.files.wordpress.com  · web viewq. implement operations on binary search tree (insertion,...

157
PROGRAM-1 Q. Implement operations (traverse, insert, delete, linear search) on an array. #include<stdio.h> #include<conio.h> int arr[100],size; void display(); void insert(int n,int pos); int delete(int pos); void linear_search(int n); int main() { int i,ch,pos,temp,n; printf("Enter the size:"); scanf("%d",&size); printf("Enter the numbers:"); for(i=0;i<size;++i) { scanf("%d",&arr[i]); } for(i=size;i<100;++i) { arr[i]=0; } do

Upload: lamnhi

Post on 14-Jun-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

PROGRAM-1

Q. Implement operations (traverse, insert, delete, linear search) on an array.

#include<stdio.h>

#include<conio.h>

int arr[100],size;

void display();

void insert(int n,int pos);

int delete(int pos);

void linear_search(int n);

int main()

{

int i,ch,pos,temp,n;

printf("Enter the size:");

scanf("%d",&size);

printf("Enter the numbers:");

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

{

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

}

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

{

arr[i]=0;

}

do

{

printf("Type 1 for traverse, 2 for insertion, 3 for deletion, 4 for linear search, 5 for exit:");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("\nTraverse\n");

display();

break;

case 2: printf("\nInsertion\n");

printf("Enter the number and the position to be inserted:");

scanf("%d %d",&n,&pos);

insert(n,pos);

++size;

display();

break;

case 3: printf("\nDeletion\n");

printf("Enter the position of deletion:");

scanf("%d",&pos);

temp=delete(pos);

--size;

printf("Deleted Number=%d\n",temp);

display();

break;

case 4: printf("\nLinear Search\n");

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

scanf("%d",&n);

linear_search(n);

break;

case 5: break;

default: printf("Wrong Input\n");

break;

}

}while(ch!=5);

getch();

return 0;

}

void display()

{

int i;

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

{

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

}

}

void insert(int n,int pos)

{

int i,j;

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

{

if(i==pos)

{

for(j=size-1;j>=i;--j)

{

arr[j+1]=arr[j];

}

arr[i]=n;

break;

}

}

}

int delete(int pos)

{

int i,j,temp;

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

{

if(i==pos)

{

temp=arr[i];

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

{

arr[j-1]=arr[j];

}

break;

}

}

return temp;

}

void linear_search(int n)

{

int i;

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

{

if(arr[i]==n)

{

printf("Search Successful\n");

break;

}

}

if(i==size)

{

printf("Search Unsuccessful\n");

}

}

OUTPUT

PROGRAM-2

Q. Implement Sparse Array.

#include<stdio.h>

#include<conio.h>

int main()

{

int arr[100][100],m,n,i,j,k,brr[10000][3],c=0;

printf("Enter the number of rows and columns:");

scanf("%d %d",&m,&n);

printf("Enter the elements in the array:");

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

{

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

{

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

if(arr[i][j]!=0)

{

++c;

}

}

}

brr[0][0]=m;

brr[0][1]=n;

brr[0][2]=c;

k=1;

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

{

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

{

if(arr[i][j]!=0)

{

brr[k][0]=i+1;

brr[k][1]=j+1;

brr[k][2]=arr[i][j];

++k;

}

}

}

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

{

for(j=0;j<3;++j)

{

printf("%d ",brr[i][j]);

}

printf("\n");

}

getch();

return 0;

}

OUTPUT

PROGRAM-3

Q. Implement operations (push, pop) on a stack using arrays. Check the status of the stack whether there is underflow or overflow.

#include<stdio.h>

#include<conio.h>

int arr[100],size,top=-1;

void push(int n);

int pop();

void display();

int main()

{

int n,temp,ch;

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

scanf("%d",&size);

do

{

printf("Type 1 for push, 2 for pop, 3 for display, 4 for exit:");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("Enter the number:");

scanf("%d",&n);

push(n);

break;

case 2: temp=pop();

if(temp!=-999)

{

printf("Popped value=%d\n",temp);

}

break;

case 3: display();

break;

case 4: break;

default: printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

void push(int n)

{

if(top==size-1)

{

printf("Overflow\n");

}

else

{

arr[++top]=n;

}

}

int pop()

{

int temp;

if(top==-1)

{

printf("Underflow\n");

return -999;

}

else

{

temp=arr[top];

--top;

return temp;

}

}

void display()

{

int i;

if(top==-1)

{

printf("Stack is empty\n");

}

else

{

for(i=top;i>=0;--i)

{

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

}

}

}

OUTPUT

PROGRAM-4

Q. Implement operations (enqueue, dequeue) on a queue using arrays. Check the status of the queue whether it is empty or full.

#include<stdio.h>

#include<conio.h>

int arr[100],front=0,rear=0,size;

void enqueue(int n);

int dequeue();

void display();

int main()

{

int n,temp,ch;

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

scanf("%d",&size);

do

{

printf("Type 1 for enqueue, 2 for dequeue, 3 for display, 4 for exit:");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("Enter the number:");

scanf("%d",&n);

enqueue(n);

break;

case 2: temp=dequeue();

if(temp!=-999)

{

printf("Deleted Value=%d\n",temp);

}

break;

case 3: display();

break;

case 4: break;

default: printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

void enqueue(int n)

{

if(rear==size)

printf("Array is full\n");

else

arr[rear++]=n;

}

int dequeue()

{

if(front==rear)

{

printf("Array is empty\n");

return -999;

}

else

{

return arr[front++];

}

}

void display()

{

int i;

if(front==rear)

printf("Array is empty\n");

else

{

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

{

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

}

}

}

OUTPUT

PROGRAM-5

Q. Implement the conversion of infix notation to postfix notation.

#include<stdio.h>

#include<conio.h>

#include<string.h>

char i[100],p[100],s[100];

int top=-1;

int priority(char a);

void push(char n);

int pop();

int main()

{

int l,j,k=0,q,x;

printf("Enter the infix expression:");

scanf("%s",&i);

s[0]='(';

strcat(s,i);

l=strlen(s);

s[l]=')';

++l;

for(j=0;j<l-2;++j)

{

i[j]=0;

}

for(j=0;j<l;++j)

{

if(s[j]=='(')

{

push(s[j]);

}

else if(s[j]=='+'||s[j]=='-'||s[j]=='/'||s[j]=='*'||s[j]=='^')

{

if(priority(s[j])<=priority(i[top]))

{

p[k++]=pop();

}

push(s[j]);

}

else if(s[j]==')')

{

x=pop();

while(x!='(')

{

p[k++]=x;

x=pop();

}

}

else

{

p[k++]=s[j];

}

}

for(j=0;j<k;++j)

{

printf("%c",p[j]);

}

getch();

return 0;

}

int priority(char a)

{

if(a=='(')

return 1;

else if(a=='+'||a=='-')

return 2;

else if(a=='*'||a=='/')

return 3;

else if(a=='^')

return 4;

}

void push(char n)

{

i[++top]=n;

}

int pop()

{

int temp;

temp=i[top];

--top;

return temp;

}

OUTPUT

PROGRAM-6

Q. Implement the evaluation of postfix notation using stacks.

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<math.h>

int s[100],top=-1;

char p[100];

void push(int n);

int pop();

int main()

{

int i,a,b,c;

printf("Enter the postfix expression:");

scanf("%s",&p);

for(i=0;i<strlen(p);++i)

{

if(isdigit(p[i]))

{

push(p[i]-48);

}

else if(p[i]=='+')

{

a=pop();

b=pop();

c=b+a;

push(c);

}

else if(p[i]=='-')

{

a=pop();

b=pop();

c=b-a;

push(c);

}

else if(p[i]=='*')

{

a=pop();

b=pop();

c=b*a;

push(c);

}

else if(p[i]=='/')

{

a=pop();

b=pop();

c=b/a;

push(c);

}

else

{

a=pop();

b=pop();

c=(int)(pow(b,a));

push(c);

}

}

a=pop();

printf("%d",a);

getch();

return 0;

}

void push(int n)

{

s[++top]=n;

}

int pop()

{

int temp;

temp=s[top];

--top;

return temp;

}

OUTPUT

PROGRAM-7

Q. Implement binary search using recursion.

#include<stdio.h>

#include<conio.h>

int arr[100],size;

void sort();

int bsearch(int n,int beg,int end);

int main()

{

int i,n,pos;

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

scanf("%d",&size);

printf("Enter the numbers:");

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

{

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

}

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

scanf("%d",&n);

sort();

pos = bsearch(n,0,size-1);

if(pos!=-1)

printf("Search Successful at position %d\n",(pos+1));

else

printf("Search Unsuccessful\n");

getch();

return 0;

}

void sort()

{

int i,j,t;

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

{

for(j=0;j<=size-i-2;++j)

{

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

{

t=arr[j];

arr[j]=arr[j+1];

arr[j+1]=t;

}

}

}

}

int bsearch(int n,int beg,int end)

{

int mid;

if(beg>end)

{

return -1;

}

else

{

mid =(beg+end)/2;

if(arr[mid]==n)

return mid;

else if(n<arr[mid])

return (bsearch(n,beg,mid-1));

else

return (bsearch(n,mid+1,end));

}

}

OUTPUT

PROGRAM-8

Q. Implement insertion (at the beginning, at specified location, at the end) & deletion (at the beginning, at specified location, at the end) on single linked list and circular single linked list.

SINGLE LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *head,*var;

void insert_at_begning(int value)

{

var=(node *)(malloc(sizeof(node)));

var->data=value;

if(head==NULL)

{

head=var;

head->next=NULL;

}

else

{

var->next=head;

head=var;

}

}

void insert_at_end(int value)

{

node *temp;

temp=head;

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

var->data=value;

if(head==NULL)

{

head=var;

head->next=NULL;

}

else

{

while(temp->next!=NULL)

{

temp=temp->next;

}

var->next=NULL;

temp->next=var;

}

}

void insert_at_middle(int value, int loc)

{

node *temp;

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

var->data=value;

temp=head;

if(head==NULL)

{

head=var;

head->next=NULL;

}

else

{

while(temp!=NULL && temp->data!=loc)

{

temp=temp->next;

}

if(temp==NULL)

{

printf("Number not found\n");

}

else

{

var->next=temp->next;

temp->next=var;

}

}

}

int delete_from_begining()

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

head=head->next;

return temp->data;

}

int delete_from_middle(int value)

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

while(temp!=NULL)

{

if(temp->data==value)

{

if(temp==head)

{

head=temp->next;

}

else

{

var->next=temp->next;

return temp->data;

}

}

else

{

var=temp;

temp=temp->next;

}

}

printf("Number not found\n");

return -998;

}

int delete_from_end()

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

while(temp->next != NULL)

{

var=temp;

temp=temp->next;

}

if(temp==head)

{

head=temp->next;

}

else

{

var->next=NULL;

}

return temp->data;

}

void display()

{

node *temp=head;

if(temp==NULL)

{

printf("\nList is Empty\n");

}

else

{

printf("\nElements in the List: ");

while(temp!=NULL)

{

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

temp=temp->next;

}

printf("\n");

}

}

int main()

{

int ch,value,loc,del;

head=NULL;

do

{

printf("Enter the choice:\ninsertion at the begning of linked list - 1");

printf("\ninsertion at the end of linked list - 2");

printf("\ninsertion at the location where you want - 3");

printf("\ndeletion at begining of linked list - 4");

printf("\ndeletion from the end of linked list - 5");

printf("\ndeletion of the data that you want - 6");

printf("\nexit - 7\n");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 2:printf("\nenter value to be inserted:");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 3:printf("\nafter which data you want to insert the data:");

scanf("%d",&loc);

printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_middle(value,loc);

display();

break;

case 4:del=delete_from_begining();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 6:printf("\nenter the data that you want to delete from the list shown above:");

scanf("%d",&value);

del=delete_from_middle(value);

if(del!=-999 && del!=-998)

{

printf("\nDeleted Number=%d\n",del);

}

display();

break;

case 5:del=delete_from_end();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 7:break;

default:printf("\nWrong input\n");

}

}while(ch!=7);

getch();

return 0;

}

OUTPUT

CIRCULAR SINGLE LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *head,*var;

void insert_at_begning(int value)

{

node *temp=head;

var=(node *)(malloc(sizeof(node)));

var->data=value;

if(head==NULL)

{

head=var;

head->next=head;

}

else

{

temp=head;

var->next=head;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=var;

head=var;

}

}

void insert_at_end(int value)

{

node *temp;

temp=head;

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

var->data=value;

if(head==NULL)

{

head=var;

head->next=head;

}

else

{

while(temp->next!=head)

{

temp=temp->next;

}

var->next=head;

temp->next=var;

}

}

void insert_at_middle(int value, int loc)

{

node *temp;

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

var->data=value;

temp=head;

if(head==NULL)

{

head=var;

head->next=head;

}

else

{

while(temp->data!=loc)

{

temp=temp->next;

}

var->next=temp->next;

temp->next=var;

}

}

int delete_from_begining()

{

node *temp;

temp=head;

if(head==NULL)

{

printf("List is empty\n");

return -999;

}

while(temp->next!=head)

{

temp=temp->next;

}

head=head->next;

temp->next=head;

return temp->data;

}

int delete_from_middle(int value)

{

node *temp,*temp1;

temp=head;

temp1=head;

if(head==NULL)

{

printf("List is empty\n");

return -999;

}

while(temp->next!=head)

{

if(temp->data == value)

{

if(temp==head)

{

head=temp->next;

while(temp1->next!=head)

{

temp1=temp1->next;

}

temp1->next=head;

}

else

{

var->next=temp->next;

}

return temp->data;

}

else

{

var=temp;

temp=temp->next;

}

}

printf("Number not found\n");

return -998;

}

int delete_from_end()

{

node *temp;

temp=head;

if(head==NULL)

{

printf("List is empty\n");

return -999;

}

while(temp->next != head)

{

var=temp;

temp=temp->next;

}

if(temp==head)

{

head=NULL;

}

else

{

var->next=head;

}

return temp->data;

}

void display()

{

node *temp=head;

if(temp==NULL)

{

printf("\nList is Empty\n");

}

else

{

printf("\nElements in the List:%d",temp->data);

temp=temp->next;

while(temp!=head)

{

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

temp=temp->next;

}

printf("\n");

}

}

int main()

{

int ch,value,loc,del;

head=NULL;

do

{

printf("Enter the choice:\ninsertion at the begning of linked list - 1");

printf("\ninsertion at the end of linked list - 2");

printf("\ninsertion at the location where you want - 3");

printf("\ndeletion at begining of linked list - 4");

printf("\ndeletion from the end of linked list - 5");

printf("\ndeletion of the data that you want - 6");

printf("\nexit - 7\n");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 2:printf("\nenter value to be inserted:");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 3:printf("\nafter which data you want to insert the data:");

scanf("%d",&loc);

printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_middle(value,loc);

display();

break;

case 4:del=delete_from_begining();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 6:printf("\nenter the data that you want to delete from the list shown above:");

scanf("%d",&value);

del=delete_from_middle(value);

if(del!=-999 && del!=-998)

printf("\nDeleted Number=%d",del);

display();

break;

case 5:del=delete_from_end();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 7:break;

default:printf("\nWrong input\n");

}

}while(ch!=7);

getch();

return 0;

}

OUTPUT

PROGRAM-9

Q. Implement stacks and queues using linked list.

STACK

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *top;

void push(int value)

{

node *temp=(node *)(malloc(sizeof(node)));

if(temp==NULL)

{

printf("\nOverflow\n");

}

else

{

temp->data=value;

if(top==NULL)

{

top=temp;

top->next=NULL;

}

else

{

temp->next=top;

top=temp;

}

}

}

int pop()

{

node *temp;

temp=top;

if(top==NULL)

{

printf("Underflow");

return -999;

}

top=top->next;

return temp->data;

}

void display()

{

node *temp=top;

if(temp==NULL)

{

printf("\nStack is Empty\n");

}

else

{

printf("\nElements in the List: ");

while(temp!=NULL)

{

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

temp=temp->next;

}

printf("\n");

}

}

int main()

{

int value,ch;

top=NULL;

do

{

printf("Type 1 for push, 2 for pop, 3 for display, 4 for exit:");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("Enter the number:");

scanf("%d",&value);

push(value);

break;

case 2:value=pop();

if(value!=-999)

{

printf("Deleted Number=%d\n",value);

}

break;

case 3:display();

break;

case 4:break;

default:printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

OUTPUT

QUEUE

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *front,*rear;

void enqueue(int value)

{

node *temp;

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

if(temp==NULL)

{

printf("Overflow");

}

else

{

temp->data=value;

if(front==NULL)

{

front=temp;

front->next=NULL;

rear=front;

}

else

{

front->next=temp;

front=front->next;

front->next=NULL;

}

}

}

int dequeue()

{

node *temp;

temp=rear;

if(rear==NULL)

{

printf("Underflow");

return -999;

}

rear=rear->next;

return temp->data;

}

void display()

{

node *temp=rear;

if(temp==NULL)

{

printf("\nQueue is Empty\n");

}

else

{

printf("\nElements in the List: ");

while(temp!=NULL)

{

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

temp=temp->next;

}

printf("\n");

}

}

int main()

{

int value,ch;

front=NULL;

rear=NULL;

do

{

printf("Type 1 for enqueue, 2 for dequeue, 3 for display, 4 for exit:");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("Enter the number:");

scanf("%d",&value);

enqueue(value);

break;

case 2:value=dequeue();

if(value!=-999)

{

printf("Deleted Number=%d\n",value);

}

break;

case 3:display();

break;

case 4:break;

default:printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

OUTPUT

PROGRAM-10

Q. Implement insertion (at the beginning, at specified location, at the end) & deletion (at the beginning, at specified location, at the end) on double linked list and circular double linked list.

DOUBLE LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *left;

struct node *right;

};

typedef struct node node;

node *head,*var;

void insert_at_begning(int value)

{

var=(node *)(malloc(sizeof(node)));

var->data=value;

if(head==NULL)

{

head=var;

head->left=NULL;

head->right=NULL;

}

else

{

var->left=NULL;

var->right=head;

head->left=var;

head=var;

}

}

void insert_at_end(int value)

{

node *temp;

temp=head;

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

var->data=value;

if(head==NULL)

{

head=var;

head->left=NULL;

head->right=NULL;

}

else

{

while(temp->right!=NULL)

{

temp=temp->right;

}

var->left=temp;

var->right=NULL;

temp->right=var;

}

}

void insert_at_middle(int value, int loc)

{

node *temp,*temp1;

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

var->data=value;

temp=head;

temp1=head;

if(head==NULL)

{

head=var;

head->left=NULL;

head->right=NULL;

}

else

{

while(temp->right!=NULL && temp->data!=loc)

{

temp=temp->right;

temp1=temp;

}

if(temp->data!=loc)

{

printf("Number not found\n");

}

else

{

var->left=temp;

var->right=temp->right;

temp->right=var;

temp1->left=var;

}

}

}

int delete_from_begining()

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

head=head->right;

head->left=NULL;

return temp->data;

}

int delete_from_middle(int value)

{

node *temp,*temp1,*var;

temp=head;

temp1=head;

if(head==NULL)

{

return -999;

}

while(temp1!=NULL)

{

if(temp->data == value)

{

if(temp==head)

{

head=temp->right;

head->left=NULL;

return temp->data;

}

else

{

temp1->left=var;

var->right=temp1;

return temp->data;

}

}

else

{

var=temp;

temp=temp->right;

temp1=temp->right;

}

}

if(temp->data==value)

{

var->right=NULL;

return temp->data;

}

printf("Number not found\n");

return -998;

}

int delete_from_end()

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

while(temp->right != NULL)

{

var=temp;

temp=temp->right;

}

if(temp==head)

{

head=temp->right;

}

else

{

var->right=NULL;

}

return temp->data;

}

void display()

{

node *temp=head;

if(temp==NULL)

{

printf("\nList is Empty\n");

}

else

{

printf("\nElements in the List: ");

while(temp!=NULL)

{

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

temp=temp->right;

}

printf("\n");

}

}

int main()

{

int ch,value,loc,del;

head=NULL;

do

{

printf("Enter the choice:\ninsertion at the begning of linked list - 1");

printf("\ninsertion at the end of linked list - 2");

printf("\ninsertion at the location where you want - 3");

printf("\ndeletion at begining of linked list - 4");

printf("\ndeletion from the end of linked list - 5");

printf("\ndeletion of the data that you want - 6");

printf("\nexit - 7\n");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 2:printf("\nenter value to be inserted:");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 3:printf("\nafter which data you want to insert the data:");

scanf("%d",&loc);

printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_middle(value,loc);

display();

break;

case 4:del=delete_from_begining();

if(del!=-999 && del!=-998)

printf("\nDeleted Number=%d",del);

display();

break;

case 6:printf("\nenter the data that you want to delete from the list shown above:");

scanf("%d",&value);

del=delete_from_middle(value);

if(del!=-999 && del!=-998)

printf("\nDeleted Number=%d",del);

display();

break;

case 5:del=delete_from_end();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 7:break;

default:printf("\nWrong input\n");

}

}while(ch!=7);

getch();

return 0;

}

OUTPUT

CIRCULAR DOUBLE LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *left;

struct node *right;

};

typedef struct node node;

node *head,*var,*tail;

void insert_at_begning(int value)

{

var=(node *)(malloc(sizeof(node)));

var->data=value;

if(head==NULL)

{

head=var;

tail=head;

head->left=NULL;

head->right=NULL;

}

else

{

var->left=tail;

var->right=head;

head->left=var;

head=var;

}

}

void insert_at_end(int value)

{

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

var->data=value;

if(head==NULL)

{

head=var;

tail=head;

head->left=NULL;

head->right=NULL;

}

else

{

var->left=tail;

var->right=head;

tail->right=var;

tail=var;

}

}

void insert_at_middle(int value, int loc)

{

node *temp;

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

var->data=value;

temp=head;

if(head==NULL)

{

head=var;

tail=head;

head->left=NULL;

head->right=NULL;

}

else

{

while(temp->data!=loc)

{

temp=temp->right;

}

var->left=temp;

var->right=temp->right;

temp->right=var;

}

}

int delete_from_begining()

{

node *temp;

temp=head;

if(head==NULL)

{

return -999;

}

head=head->right;

head->left=tail;

return temp->data;

}

int delete_from_middle(int value)

{

node *temp,*temp1,*var;

temp=head;

temp1=head;

if(head==NULL)

{

return -999;

}

while(temp->right!=head)

{

if(temp->data == value)

{

if(temp==head)

{

head=temp->right;

head->left=tail;

return temp->data;

}

else

{

temp1->left=var;

var->right=temp->right;

return temp->data;

}

}

else

{

var=temp;

temp=temp->right;

temp1=temp->right;

}

}

if(temp->data==value)

{

var->right=head;

tail=var;

head->left=var;

return temp->data;

}

printf("Number not found\n");

return -998;

}

int delete_from_end()

{

node *temp;

temp=tail;

if(head==NULL)

{

return -999;

}

if(temp==head)

{

head=NULL;

tail=NULL;

}

else

{

tail=tail->left;

tail->right=NULL;

}

return temp->data;

}

void display()

{

node *temp=head;

if(temp==NULL)

{

printf("\nList is Empty\n");

}

else

{

printf("\nElements in the List: %d",temp->data);

temp=temp->right;

while(temp!=NULL && temp!=head)

{

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

temp=temp->right;

}

printf("\n");

}

}

int main()

{

int ch,value,loc,del;

head=NULL;

do

{

printf("Enter the choice:\ninsertion at the begning of linked list - 1");

printf("\ninsertion at the end of linked list - 2");

printf("\ninsertion at the location where you want - 3");

printf("\ndeletion at begining of linked list - 4");

printf("\ndeletion from the end of linked list - 5");

printf("\ndeletion of the data that you want - 6");

printf("\nexit - 7\n");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_begning(value);

display();

break;

case 2:printf("\nenter value to be inserted:");

scanf("%d",&value);

insert_at_end(value);

display();

break;

case 3:printf("\nafter which data you want to insert the data:");

scanf("%d",&loc);

printf("\nenter the value to be inserted:");

scanf("%d",&value);

insert_at_middle(value,loc);

display();

break;

case 4:del=delete_from_begining();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 6:printf("\nenter the data that you want to delete from the list shown above:");

scanf("%d",&value);

del=delete_from_middle(value);

if(del!=-999 && del!=-998)

printf("\nDeleted Number=%d",del);

display();

break;

case 5:del=delete_from_end();

if(del!=-999)

printf("\nDeleted Number=%d",del);

display();

break;

case 7:break;

default:printf("\nWrong input\n");

}

}while(ch!=7);

getch();

return 0;

}

OUTPUT

PROGRAM-11

Q. Write a program to count the number of nodes and reverse the single linked list.

#include<stdio.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *start;

void insert(int value)

{

node *var=(node *)(malloc(sizeof(node)));

node *temp=start;

var->data=value;

var->next=NULL;

if(start==NULL)

{

start=var;

}

else

{

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=var;

}

}

int count()

{

int c=0;

node *temp=start;

while(temp!=NULL)

{

++c;

temp=temp->next;

}

return c;

}

void reverse()

{

node *x,*y,*z,*temp=start;

if(start==NULL)

{

printf("List is empty\n");

}

else

{

x=start;

y=x->next;

if(y!=NULL)

{

z=y->next;

while(z!=NULL)

{

y->next=x;

x=y;

y=z;

z=z->next;

}

y->next=x;

start->next=NULL;

start=y;

}

}

}

void display()

{

node *temp=start;

printf("Elemnts in the list:");

while(temp!=NULL)

{

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

temp=temp->next;

}

}

int main()

{

int ch,value;

do

{

printf("\nType:\n1 for insertion\n2 for counting\n3 for reversing\n4 for exit\n");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("Enter the number to be inserted:");

scanf("%d",&value);

insert(value);

display();

break;

case 2: value=count();

printf("\n%d\n",value);

break;

case 3: reverse();

display();

break;

case 4: break;

default: printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

OUTPUT

PROGRAM-12

Q. Implement linear search and selection sort in a single linked list.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *start;

void insert(int value)

{

node *var=(node *)(malloc(sizeof(node)));

node *temp=start;

var->data=value;

var->next=NULL;

if(start==NULL)

{

start=var;

}

else

{

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=var;

}

}

int linear_search(int value)

{

node *temp=start;

while(temp!=NULL)

{

if(temp->data==value)

{

return 1;

}

temp=temp->next;

}

return 0;

}

void selection_sort()

{

node *temp1,*temp2,*temp3;

int t;

temp1=start;

temp2=start;

temp3=start;

while(temp1!=NULL)

{

temp2=temp1->next;

temp3=temp1;

while(temp2!=NULL)

{

if(temp2->data<temp1->data)

{

temp3=temp2;

}

temp2=temp2->next;

}

t=temp1->data;

temp1->data=temp3->data;

temp3->data=t;

temp1=temp1->next;

}

}

void display()

{

node *temp=start;

printf("Elemnts in the list:");

while(temp!=NULL)

{

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

temp=temp->next;

}

}

int main()

{

int ch,value,r;

do

{

printf("\nType:\n1 for insertion\n2 for linear search\n3 for selection sort\n4 for exit\n");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("Enter the number to be inserted:");

scanf("%d",&value);

insert(value);

display();

break;

case 2: printf("Enter the number to be searched:");

scanf("%d",&value);

r=linear_search(value);

if(r==1)

printf("\nSearch Successful\n");

else

printf("\nSearch Unsuccessful\n");

break;

case 3: selection_sort();

display();

break;

case 4: break;

default: printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

OUTPUT

PROGRAM-13

Q. Write a program to merge two sorted linked list and display the final sorted linked list.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

struct node

{

int data;

struct node *next;

};

typedef struct node node;

node *start1,*start2;

void display(node *start);

node* insert(node *start,int value)

{

node *var=(node *)(malloc(sizeof(node)));

node *temp=start;

var->data=value;

var->next=NULL;

if(start==NULL)

{

start=var;

}

else

{

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=var;

}

return start;

}

void selection_sort(node *start)

{

node *temp1,*temp2,*temp3;

int t;

temp1=start;

temp2=start;

temp3=start;

while(temp1!=NULL)

{

temp2=temp1->next;

temp3=temp1;

while(temp2!=NULL)

{

if(temp2->data<temp1->data)

{

temp3=temp2;

}

temp2=temp2->next;

}

t=temp1->data;

temp1->data=temp3->data;

temp3->data=t;

temp1=temp1->next;

}

}

void merge()

{

node *temp1,*temp2,*start3;

start3=NULL;

temp1=start1;

temp2=start2;

while(temp1!=NULL && temp2!=NULL)

{

if(temp1->data < temp2->data)

{

start3=insert(start3,temp1->data);

temp1=temp1->next;

}

else

{

start3=insert(start3,temp2->data);

temp2=temp2->next;

}

}

while(temp1!=NULL)

{

start3=insert(start3,temp1->data);

temp1=temp1->next;

}

while(temp2!=NULL)

{

start3=insert(start3,temp2->data);

temp2=temp2->next;

}

printf("\nMerged list is : ");

display(start3);

}

void display(node *start)

{

node *temp=start;

printf("\nElemnts in the list:");

while(temp!=NULL)

{

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

temp=temp->next;

}

}

int main()

{

int ch,value;

node *temp;

do

{

printf("\nType:\n1 for insertion in the first list\n2 for insertion in the second list\n3 for merging\n4 for exit\n");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("Enter the number to be inserted in the first list:");

scanf("%d",&value);

start1=insert(start1,value);

display(start1);

break;

case 2: printf("Enter the number to be inserted in the second list:");

scanf("%d",&value);

start2=insert(start2,value);

display(start2);

break;

case 3: selection_sort(start1);

display(start1);

selection_sort(start2);

display(start2);

merge();

break;

case 4: break;

default: printf("Wrong Input\n");

}

}while(ch!=4);

getch();

return 0;

}

OUTPUT

PROGRAM-14

Q. Implement stacks ADT.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

typedef struct node1

{

void* dataPtr;

struct node* link;

} STACK_NODE;

typedef struct node2

{

int count;

STACK_NODE* top;

} STACK;

STACK* stack;

STACK_NODE* temp;

void createStack ()

{

stack = (STACK*) malloc( sizeof (STACK));

if(stack!=NULL)

{

stack->count = 0;

stack->top = NULL;

}

}

void pushStack (STACK* stack)

{

STACK_NODE* newPtr;

newPtr = (STACK_NODE*)(malloc(sizeof( STACK_NODE)));

if (newPtr==NULL)

{

return ;

}

else

{

newPtr->link = stack->top;

stack->top = newPtr;

(stack->count)++;

}

}

void* popStack(STACK* stack)

{

void* dataOutPtr;

STACK_NODE* temp;

if(stack->count == 0)

{

dataOutPtr = NULL;

}

else

{

temp = stack->top;

dataOutPtr = stack->top->dataPtr;

stack->top = stack->top->link;

free(temp);

(stack->count)--;

}

return dataOutPtr;

}

int main()

{

int data;

int ch,dataout;

createStack();

do

{

printf("\nType:\n1.PUSH\n2.POP\n3.EXIT\n");

scanf("%d",&ch);

switch(ch)

{

case 1: pushStack(stack);

dataout=*(int *)(malloc(sizeof(int)));

printf("Enter the number:");

scanf("%d",&data);

stack->top->dataPtr=data;

temp=stack->top;

printf("Stack is:\n");

while(temp!=NULL)

{

printf("%d\n",(int *)(temp->dataPtr));

temp=temp->link;

}

break;

case 2: dataout=(int *)(popStack(stack));

if(dataout==NULL)

{

printf("\nUnderflow");

}

else

{

printf("\nPopped Value:%d",dataout);

temp=stack->top;

if(temp==NULL)

{

printf("\nNow stack is empty");

}

else

{

printf("Stack is:\n");

while(temp!=NULL)

{

printf("%d\n",(int *)(temp->dataPtr));

temp=temp->link;

}

}

}

break;

case 3: exit(0);

default: printf("\nWrong Input");

}

}while(ch!=3);

getch();

return 0;

}

OUTPUT

PROGRAM-15

Q. Implement queue ADT.

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

typedef struct node1

{

void* dataptr;

struct node1* next;

}QUEUE_NODE;

typedef struct node2

{

int count;

QUEUE_NODE* front;

QUEUE_NODE* rear;

}QUEUE;

QUEUE* create()

{

QUEUE* queue;

queue=(QUEUE*)(malloc(sizeof(QUEUE)));

if(queue!=NULL)

{

queue->count=0;

queue->front=NULL;

queue->rear=NULL;

}

return queue;

}

void enqueue(QUEUE* queue,void* datain)

{

QUEUE_NODE* newptr;

newptr=(QUEUE_NODE*)(malloc(sizeof(QUEUE_NODE)));

if(newptr==NULL)

{

return;

}

newptr->dataptr=datain;

newptr->next=NULL;

if(queue->count==0)

queue->front=newptr;

else

queue->rear->next=newptr;

(queue->count)++;

queue->rear=newptr;

return ;

}

void* dequeue(QUEUE* queue)

{

void* dataout;

QUEUE_NODE* temp;

if(queue->count==0)

dataout=NULL;

else

{

temp=queue->front;

dataout=queue->front->dataptr;

if(queue->count==1)

queue->rear=queue->front=NULL;

else

queue->front=queue->front->next;

free(temp);

(queue->count)--;

}

return dataout;

}

int main()

{

QUEUE* queue;

QUEUE_NODE* temp;

int *i,*data;

int g,j,n;

queue=create();

do

{

printf("\nType:\n1.ENQUEUE\n2.DEQUEUE\n3.EXIT\n");

scanf("%d",&g);

switch(g)

{

case 1: printf("Enter the number:");

scanf("%d",&n);

enqueue(queue,n);

temp=queue->front;

/*data=(int *)(malloc(sizeof(int)));

*data=*(int *)(queue->rear->dataptr);

queue->rear->dataptr=data;*/

printf("Queue is:\n");

while(temp!=NULL)

{

printf("%d\n",(int *)(temp->dataptr));

temp=temp->next;

}

break;

case 2: j=(int *)(dequeue(queue));

temp=queue->front;

if(j==NULL)

printf("\nUnderflow");

else

{

printf("\nPopped Value:%d",j);

printf("\nQueue is:\n");

}

while(temp!=NULL)

{

printf("%d\n",(int *)(temp->dataptr));

temp=temp->next;

}

break;

case 3: exit(0);

default: printf("\nWrong Input");

}

}while(g!=3);

getch();

return 0;

}

OUTPUT

PROGRAM-17

Q. Implement operations on Binary Search Tree (insertion, deletion, search, traversals (using recursion)- Inorder , Preorder, Postorder).

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct tree

{

int info;

struct tree *left;

struct tree *right;

};

struct tree *insert(struct tree *,int);

void inorder(struct tree *);

void postorder(struct tree *);

void preorder(struct tree *);

struct tree *delet(struct tree *,int);

struct tree *search(struct tree *);

int main(void)

{

struct tree *root;

int choice, item,item_no;

root = NULL;

do

{

do

{

printf("\n \t 1. Insert to Binary Tree ");

printf("\n\t 2. Delete from Binary Tree ");

printf("\n\t 3. Inorder traversal ");

printf("\n\t 4. Postorder traversal");

printf("\n\t 5. Preorder traversal");

printf("\n\t 6. Search ");

printf("\n\t 7. Exit ");

printf("\n\t Enter choice : ");

scanf(" %d",&choice);

if(choice<1 || choice>7)

printf("\n Not a valid choice");

}while (choice<1 || choice>7);

switch(choice)

{

case 1:

printf("\n Enter new element: ");

scanf("%d", &item);

root= insert(root,item);

printf("\n Inorder traversal of binary tree is : ");

inorder(root);

break;

case 2:

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

scanf(" %d",&item_no);

root=delet(root,item_no);

inorder(root);

break;

case 3:

printf("\n Inorder traversal of binary tree is : ");

inorder(root);

break;

case 4:

printf("\n Postorder traversal of binary tree is : ");

postorder(root);

break;

case 5:

printf("\n Preorder traversal of binary tree is : ");

preorder(root);

break;

case 6:

printf("\n Search");

root=search(root);

break;

default:

printf("\n End of program ");

}

}while(choice !=7);

return(0);

}

struct tree *insert(struct tree *root, int x)

{

if(!root)

{

root=(struct tree*)malloc(sizeof(struct tree));

root->info = x;

root->left = NULL;

root->right = NULL;

return(root);

}

if(root->info > x)

root->left = insert(root->left,x);

else

{

if(root->info < x)

root->right = insert(root->right,x);

}

return(root);

}

void inorder(struct tree *root)

{

if(root != NULL)

{

inorder(root->left);

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

inorder(root->right);

}

return;

}

void postorder(struct tree *root)

{

if(root != NULL)

{

postorder(root->left);

postorder(root->right);

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

}

return;

}

void preorder(struct tree *root)

{

if(root != NULL)

{

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

preorder(root->left);

preorder(root->right);

}

return;

}

struct tree *delet(struct tree *ptr,int x)

{

struct tree *p1,*p2;

if(!ptr)

{

printf("\n Node not found ");

return(ptr);

}

else

{

if(ptr->info < x)

{

ptr->right = delet(ptr->right,x);

}

else if (ptr->info >x)

{

ptr->left=delet(ptr->left,x);

return ptr;

}

else

{

if(ptr->info == x)

{

if(ptr->left == ptr->right)

{

free(ptr);

return(NULL);

}

else if(ptr->left==NULL)

{

p1=ptr->right;

free(ptr);

return p1;

}

else if(ptr->right==NULL)

{

p1=ptr->left;

free(ptr);

return p1;

}

else

{

p1=ptr->right;

p2=ptr->right;

while(p1->left != NULL)

p1=p1->left;

p1->left=ptr->left;

free(ptr);

return p2;

}

}

}

}

return(ptr);

}

struct tree *search(struct tree *root)

{

int no,i,ino;

struct tree *ptr;

ptr=root;

printf("\n elment to be searched :");

scanf(" %d",&no);

fflush(stdin);

while(ptr)

{

if(no>ptr->info)

ptr=ptr->right;

else if(no<ptr->info)

ptr=ptr->left;

else

break;

}

if(ptr)

printf("\n Element %d which was searched is found ",no);

else

printf("\n Element %d does not exist in the binary tree",no);

return(root);

}

OUTPUT

PROGRAM-18

Q. Implement traversals on binary search tree (using stacks) – Inorder, Preorder, Postorder.

#include <stdio.h>

#include <malloc.h>

#include<conio.h>

typedef struct node

{

struct node *left;

struct node *right;

int data;

}node;

node *stack[50];

node *stack2[50];

int top = -1, top2 = -1;

node *insert(node*, int);

void inorder(node*);

void postorder(node*);

void preorder(node*);

node *search(node*, int);

node *del(node*, int);

node *pop();

int main(void)

{

node *r = NULL;

int choice, item;

node *x, *searched;

do

{

printf("\n\t1.Insert\n\t2.Preorder\n\t3.Inorder\n\t4.Postorder\n\t5. Exit\n");

printf("Enter choice : ");

scanf("%d", &choice);

switch(choice)

{

case 1:

printf("Enter the item : ");

scanf("%d", &item);

r = insert(r, item);

break;

case 2:preorder(r);

break;

case 3:inorder(r);

break;

case 4:postorder(r);

break;

case 5:

break;

}

}while(choice != 5);

}

node *insert(node *root, int item)

{

node *prev = root;

node *temp = root;

if(root == NULL)

{

root = (node *)(malloc(sizeof(node)));

root->data = item;

root->left = root->right = NULL;

}

else

{

while(temp != NULL)

{

if(item > temp->data)

{

prev = temp;

temp = temp->right;

}

else

{

prev = temp;

temp = temp->left;

}

}

temp = (node *)(malloc(sizeof(node)));

temp->data = item;

temp->left = temp->right = NULL;

if(item >= prev->data)

prev->right = temp;

else

prev->left = temp;

}

return root;

}

void inorder(node *root)

{

node *temp = root;

while((top >= 0) || (temp != NULL))

{

if(temp != NULL)

{

stack[++top] = temp;

temp = temp->left;

}

else

{

temp = stack[top--];

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

temp = temp->right;

}

}

}

node *pop()

{

if(top == -1)

printf("Underflow\n");

else

return stack[top--];

}

void postorder(node *root)

{

node *temp;

if(root != NULL)

stack[++top] = root;

while(top >= 0)

{

temp = pop();

stack2[++top2] = temp;

if(temp->left != NULL)

stack[++top] = temp->left;

if(temp->right != NULL)

stack[++top] = temp->right;

}

while(top2 != -1)

{

temp = stack2[top2--];

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

}

}

void preorder(node *root)

{

node *temp;

if(root != NULL)

stack[++top] = root;

while(top >= 0)

{

temp = pop();

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

if(temp->right != NULL)

stack[++top] = temp->right;

if(temp->left != NULL)

stack[++top] = temp->left;

}

}

OUTPUT

PROGRAM-19

Q. Implement graph traversal (DFT & BFT).

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define MAX 20

typedef enum boolean{false,true} bool;

int adj[MAX][MAX];

bool visited[MAX];

int n;

int main()

{

int i,v,choice;

create_graph();

while(1)

{

printf("\n");

printf("1. Adjacency matrix\n");

printf("2. Depth First Search using stack\n");

printf("3. Breadth First Search\n");

printf("4. Adjacent vertices\n");

printf("5. Components\n");

printf("6. Exit\n");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

{

case 1: printf("Adjacency Matrix\n");

display();

break;

case 2: printf("Enter starting node for Depth First Search : ");

scanf("%d",&v);

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

visited[i]=false;

dfs(v);

break;

case 3: printf("Enter starting node for Breadth First Search : ");

scanf("%d", &v);

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

visited[i]=false;

bfs(v);

break;

case 4: printf("Enter node to find adjacent vertices : ");

scanf("%d", &v);

printf("Adjacent Vertices are : ");

adj_nodes(v);

break;

case 5: components();

break;

case 6: exit(1);

default: printf("Wrong choice\n");

break;

}

}

}

void create_graph()

{

int i,max_edges,origin,destin;

printf("Enter number of nodes : ");

scanf("%d",&n);

max_edges=n*(n-1);

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

{

printf("Enter edge %d( 0 0 to quit ) : ",i);

scanf("%d %d",&origin,&destin);

if((origin==0) && (destin==0))

break;

if( origin > n || destin > n || origin<=0 || destin<=0)

{

printf("Invalid edge!\n");

i--;

}

else

{

adj[origin][destin]=1;

}

}

}

void display()

{

int i,j;

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

{

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

printf("%4d",adj[i][j]);

printf("\n");

}

}

void dfs_rec(int v)

{

int i;

visited[v]=true;

printf("%d ",v);

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

if(adj[v][i]==1 && visited[i]==false)

dfs_rec(i);

}

void dfs(int v)

{

int i,stack[MAX],top=-1,pop_v,j,t;

int ch;

top++;

stack[top]=v;

while (top>=0)

{

pop_v=stack[top];

top--;

if(visited[pop_v]==false)

{

printf("%d ",pop_v);

visited[pop_v]=true;

}

else

continue;

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

{

if( adj[pop_v][i]==1 && visited[i]==false)

{

top++;

stack[top]=i;

}

}

}

}

void bfs(int v)

{

int i,front,rear;

int que[20];

front=rear= -1;

printf("%d ",v);

visited[v]=true;

rear++;

front++;

que[rear]=v;

while(front<=rear)

{

v=que[front];

front++;

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

{

if( adj[v][i]==1 && visited[i]==false)

{

printf("%d ",i);

visited[i]=true;

rear++;

que[rear]=i;

}

}

}

}

void adj_nodes(int v)

{

int i;

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

if(adj[v][i]==1)

printf("%d ",i);

printf("\n");

}

void components()

{

int i;

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

visited[i]=false;

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

{

if(visited[i]==false)

dfs_rec(i);

}

printf("\n");

}

OUTPUT

PROGRAM-20

Q. Make a menu driven program to perform various sorting techniques (insertion, shell, merge, heap, bubble, quick).

#include<stdio.h>

#include<conio.h>

int arr[100];

void merge(int min,int mid,int max);

void insertion()

{

int i,size,temp,j;

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

scanf("%d",&size);

printf("\nEnter the elements in the array:");

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

{

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

}

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

{

temp=arr[i];

j=i-1;

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

{

arr[j+1]=arr[j];

j=j-1;

}

arr[j+1]=temp;

}

printf("\nSorted array is:\n");

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

{

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

}

}

void bubble()

{

int i,size,t,j;

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

scanf("%d",&size);

printf("\nEnter the elements in the array:");

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

{

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

}

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

{

for(j=0;j<=size-i-2;++j)

{

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

{

t=arr[j];

arr[j]=arr[j+1];

arr[j+1]=t;

}

}

}

printf("\nSorted array is:\n");

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

{

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

}

}

void qsort(int fst, int last)

{

int i,j,pivot,tmp;

if(fst<last)

{

pivot=fst;

i=fst;

j=last;

while(i<j)

{

while(arr[i]<=arr[pivot] && i<last)

i++;

while(arr[j]>arr[pivot])

j--;

if(i<j)

{

tmp=arr[i];

arr[i]=arr[j];

arr[j]=tmp;

}

}

tmp=arr[pivot];

arr[pivot]=arr[j];

arr[j]=tmp;

qsort(fst,j-1);

qsort(j+1,last);

}

}

void part(int min,int max)

{

int mid;

if(min<max)

{

mid=(min+max)/2;

part(min,mid);

part(mid+1,max);

merge(min,mid,max);

}

}

void merge(int min,int mid,int max)

{

int tmp[30];

int i,j,k,m;

j=min;

m=mid+1;

for(i=min; j<=mid && m<=max ; i++)

{

if(arr[j]<=arr[m])

{

tmp[i]=arr[j];

j++;

}

else

{

tmp[i]=arr[m];

m++;

}

}

if(j>mid)

{

for(k=m; k<=max; k++)

{

tmp[i]=arr[k];

i++;

}

}

else

{

for(k=j; k<=mid; k++)

{

tmp[i]=arr[k];

i++;

}

}

for(k=min; k<=max; k++)

arr[k]=tmp[k];

}

void shell()

{

int i,j,k,tmp,num;

printf("Enter total no. of elements : ");

scanf("%d", &num);

for(k=0; k<num; k++)

{

printf("\nEnter %d number : ",k+1);

scanf("%d",&arr[k]);

}

for(i=num/2; i>0; i=i/2)

{

for(j=i; j<num; j++)

{

for(k=j-i; k>=0; k=k-i)

{

if(arr[k+i]>=arr[k])

break;

else

{

tmp=arr[k];

arr[k]=arr[k+i];

arr[k+i]=tmp;

}

}

}

}

printf("\nSorted array\n");

for(k=0; k<num; k++)

printf("%d\t",arr[k]);

}

void manage(int i)

{

int tmp;

tmp=arr[i];

while((i>1)&&(arr[i/2]<tmp))

{

arr[i]=arr[i/2];

i=i/2;

}

arr[i]=tmp;

}

void heapsort(int i, int size)

{

int tmp,j;

tmp=arr[i];

j=i*2;

while(j<=size)

{

if((j<size)&&(arr[j]<arr[j+1]))

j++;

if(arr[j]<arr[j/2])

break;

arr[j/2]=arr[j];

j=j*2;

}

arr[j/2]=tmp;

}

int main()

{

int ch;

int i,j,size,tmp,k;

do

{

printf("\nType:\n1.INSERTION\n2.BUBBLE\n3.QUICK\n4.MERGE\n5.SHELL\n6.HEAP\n7.EXIT\n");

scanf("%d",&ch);

switch(ch)

{

case 1: insertion();

break;

case 2: bubble();

break;

case 3: printf("Enter total no. of the elements : ");

scanf("%d",&size);

printf("Enter total %d elements : \n",size);

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

{

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

}

qsort(0,size-1);

printf("Quick sorted elements are as : \n");

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

{

printf("%d\t",arr[i]);

}

break;

case 4: printf("Enter total no. of elements : ");

scanf("%d",&size);

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

{

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

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

}

part(0,size-1);

printf("\nSorted elements\n");

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

{

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

}

break;

case 5: shell();

break;

case 6: printf("Enter the number of elements to sort : ");

scanf("%d",&size);

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

{

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

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

manage(i);

}

j=size;

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

{

tmp=arr[1];

arr[1]=arr[size];

arr[size]=tmp;

size--;

heapsort(1,size);

}

size=j;

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

{

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

}

break;

case 7: break;

default: printf("\nWrong Input");

}

}while(ch!=7);

}

OUTPUT