HIT

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

 STACK MENU DRIVEN

#include<stdio.h>
#include<stdlib.h>
#define max_size 5
int stack[max_size],top=-1;
/*------ Function Prototype------------*/
void push();
void pop();
void peep();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Peep\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=5);
return 0;
}
void push() //Inserting element in to the stack
{
int item;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void peep()
{
if(top==-1)
{
printf("\nStack is empty:");
}
else
{
printf("The topmost element of the stack is %d",stack[top]);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}

 QUEUE MENU DRIVEN

#include<stdio.h>
#include<stdlib.h>
#define max_size 5

int queue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{

printf("\n\n--------QUEUE OPERATIONS-----------\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);
return 0;
}
void insert() //Inserting an element in to the queue
{
int item;
if(rear==(max_size-1))
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;

if(front==-1)
front=0;
}

}//end of insert()
void del() //deleting an element from the queue
{
int item;
if(front==-1)
{
printf("\nQueue Underflow:");
}
else
{
item=queue[front];
printf("\nThe deleted element: %d\t",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
}
}//end of del()
void display() //To display the queue elements
{
int i;
if(front==-1)
{
printf("\nQueue is Empty:");
}
else
{
printf("\nThe queue elements are:\n" );
for(i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}

}//end of display()

 Circular queue menu driven

#include<stdio.h>s
#include<stdlib.h>
#define max_size 5
int cqueue[max_size],front=-1,rear=-1;
/*------ Function Prototype------------*/
void insert();
void del();
void display();
/*-------------------------------------*/
main()
{
int choice;
do{

printf("\n\n--------CIRCULAR QUEUE OPERATIONS-----------\n");


printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);

}
void insert() //Inserting an element in to the queue
{
int item;
if(front==(rear+1)%max_size)
{
printf("\nQueue Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
rear=(rear+1)%max_size;
cqueue[rear]=item;

if(front==-1)
{
front=0;
rear=0;
}

}//end of insert()

void del() //deleting an element from the queue


{
int item;
if(front==-1)
{
printf("\nQueue Underflow:");
}
else
{
item=cqueue[front];
printf("\nThe deleted element: %d\t",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%max_size;
}

}
}//end of del()

void display() //To display the queue elements


{
int i;
if(front==-1)
{
printf("\nQueue is Empty:");
}
else
{
printf("\nThe queue elements are:\n" );
if(front<rear)
{
for(i=front;i<=rear;i++)
{
printf("%d\t",cqueue[i]);
}
}
else
{
for(i=0;i<=rear;i++)
{
printf("%d\t",cqueue[i]);
}
for(i=front;i<max_size;i++)
{
printf("%d\t",cqueue[i]);
}
}

}//end of display()

 Singly link list

#include<stdlib.h>
/*----Function Prototypes-----*/
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
/*-----------------------------*/
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main() //main() starts
{
int choice;
while(1){
printf("\n***SINGLE LINKED LIST OPERATIONS:****\n");
printf("\n MENU \n");
printf("---------------------------------------\n");
printf("\n 1.Create \n");
printf("\n 2.Display \n");
printf("\n 3.Insert at the beginning \n");
printf("\n 4.Insert at the end \n");
printf("\n 5.Insert at specified position \n");
printf("\n 6.Delete from beginning \n");
printf("\n 7.Delete from the end \n");
printf("\n 8.Delete from specified position \n");
printf("\n 9.Exit \n");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("\n Wrong Choice:\n");
break;
}//end of switch()
}
return 0;
}//end of main()
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}//end of create()
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}//end of while
}//end of else
}//end of display()
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}//end of insert_begin()
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}//end of insert_end
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}//end of else
}//end of insert_pos
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}//end of delete_begin()
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}//end of else
}//end of delete_pos()

 Add two polynomial

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct pnode
{
float coef;
int exp;
struct pnode *next;
}p;
p *getnode();
//Main function starts here
void main()
{
p *p1,*p2,*p3;
p *getpoly(),*add(p*,p*);
void display(p*);
clrscr();
printf(“\n Enter first polynomial”);
p1=getpoly();
printf(“\n Enter second polynomial”);
p2=getpoly();
printf(“\nThe first polynomial is”);
display(p1);
printf(“\nThe second polynomial is”);
display(p2);
p3=add(p1,p2);
printf(“\nAddition of two polynomial is :\n”);
display(p3);
}

/*Funtion to get polynomial*/


p *getpoly()
{
p *temp,*New,*last;
int flag,exp;
char ans;
float coef;
temp=NULL;
flag=1;
printf(“\nenter the polynomial in descending order of exponent”);
do
{
printf(“\nenter the coef & exponent of a term”);
scanf(“%f%d”,&coef,&exp);
New=getnode();
if(New==NULL)
printf(“\nmemory cannot be allocated”);
New->coef=coef;
New->exp=exp;
if(flag==1)
{
temp=New;
last=temp;
flag=0;
}
else
{
last->next=New;
last=New;
}
printf(“\ndou want to more terms”);
ans=getch();
}
while(ans==’y');
return(temp);
}
/*Function to get the Nodes of Polynomial*/
p *getnode()
{
p *temp;
temp=(p*) malloc (sizeof(p));
temp->next=NULL;
return(temp);
}
/*Function to display Polynomial*/
void display(p*head)
{
p*temp;
temp=head;
if(temp==NULL)
printf(“\npolynomial empty”);
while(temp->next!=NULL)
{
printf(“%0.1fx^%d+”,temp->coef,temp->exp);
temp=temp->next;
}
printf(“\n%0.1fx^%d”,temp->coef,temp->exp);
getch();
}
/*Function to add Polynomials*/
p*add(p*first,p*second)
{
p *p1,*p2,*temp,*dummy;
char ch;
float coef;
p *append(int,float,p*);
p1=first;
p2=second;
temp=(p*)malloc(sizeof(p));
if(temp==NULL)
printf(“\nmemory cannot be allocated”);
dummy=temp;
while(p1!=NULL&&p2!=NULL)
{
if(p1->exp==p2->exp)
{
coef=p1->coef+p2->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
p2=p2->next;
}
else
if(p1->expexp)
{
coef=p2->coef;
temp=append(p2->exp,coef,temp);
p2=p2->next;
}
else
if(p1->exp>p2->exp)
{
coef=p1->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
}
}
while(p1!=NULL)
{
temp=append(p1->exp,p1->coef,temp);
p1=p1->next;
}
while(p2!=NULL)
{
temp=append(p2->exp,p2->coef,temp);
p2=p2->next;
}
temp->next=NULL;
temp=dummy->next;
free(dummy);
return(temp);
}
/*Function to append the coefficients with Polynomial*/
p*append(int Exp,float Coef,p*temp)
{
p*New,*dum;
New=(p*)malloc(sizeof(p));
if(New==NULL)
printf(“\ncannot be allocated”);
New->exp=Exp;
New->coef=Coef;
New->next=NULL;
dum=temp;
dum->next=New;
dum=New;
return(dum);
}

 Doubly link list

/*
* C Program to Implement a Doubly Linked List & provide Insertion,
Deletion & Display Operations
*/
#include <stdio.h>
#include <stdlib.h>

struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;

void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();

int count = 0;

void main()
{
int ch;

h = NULL;
temp = temp1 = NULL;

printf("\n 1 - Insert at beginning");


printf("\n 2 - Insert at end");
printf("\n 3 - Insert at position i");
printf("\n 4 - Delete at i");
printf("\n 5 - Display from beginning");
printf("\n 6 - Display from end");
printf("\n 7 - Search for element");
printf("\n 8 - Sort the list");
printf("\n 9 - Update an element");
printf("\n 10 - Exit");

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
delete();
break;
case 5:
traversebeg();
break;
case 6:
temp2 = h;
if (temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case 7:
search();
break;
case 8:
sort();
break;
case 9:
update();
break;
case 10:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}

/* TO create an empty node */


void create()
{
int data;

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


temp->prev = NULL;
temp->next = NULL;
printf("\n Enter value to node : ");
scanf("%d", &data);
temp->n = data;
count++;
}

/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}

/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}

/* To insert at any position */


void insert3()
{
int pos, i = 2;

printf("\n Enter position to be inserted : ");


scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))


{
printf("\n Position out of range to insert");
return;
}
if ((h == NULL) && (pos != 1))
{
printf("\n Empty list cannot insert other than 1st position");
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}

/* To delete an element */
void delete()
{
int i = 1, pos;

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


scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))


{
printf("\n Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf("\n Error : Empty list no elements to delete");
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this
statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}

/* Traverse from beginning */


void traversebeg()
{
temp2 = h;

if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");

while (temp2->next != NULL)


{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
}

/* To traverse from end recursively */


void traverseend(int i)
{
if (temp2 != NULL)
{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(" %d ", i);
}
}

/* To search for an element in the list */


void search()
{
int data, count = 0;
temp2 = h;

if (temp2 == NULL)
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d", &data);
while (temp2 != NULL)
{
if (temp2->n == data)
{
printf("\n Data found in %d position",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}

/* To update a node value in the list */


void update()
{
int data, data1;

printf("\n Enter node data to be updated : ");


scanf("%d", &data);
printf("\n Enter new data : ");
scanf("%d", &data1);
temp2 = h;
if (temp2 == NULL)
{
printf("\n Error : List empty no node to update");
return;
}
while (temp2 != NULL)
{
if (temp2->n == data)
{

temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}

printf("\n Error : %d not found in list to update", data);


}

/* To sort the linked list */


void sort()
{
int i, j, x;

temp2 = h;
temp4 = h;

if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}

for (temp2 = h; temp2 != NULL; temp2 = temp2->next)


{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}
}
}
traversebeg();
}

 Singly header link list

#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_hll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
printf("\n\n -----Main Menu-----");
printf("\n 1. Create a list");
printf("\n 2. Display the list");
printf("\n 3. Exit");
printf("\n Enter your choice : ");
scanf("%d", &option);
switch(option)
{
case 1:
start = create_hll(start);
printf("\n Header Linked List Created Successfully");
break;
case 2:
start = display(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_hll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data :");
scanf("%d", &num);
while(num != -1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
if(start == NULL)
{
start = (struct node*)malloc(sizeof(struct node));
start -> next = new_node;
}
else
{
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
printf("\n Enter the data :");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
ptr = ptr -> next;
while(ptr != NULL)
{
printf("\t %d",ptr -> data);
ptr = ptr -> next;
}
return start;
}

 Searching

#include<stdio.h>
int key;
int binary(int a[],int low,int high);
int linear(int a[],int n);
main()
{
int n,i,a[10],pos,ch;
do
{
printf("-------Menu--------\n1.Binary Search\n2.Linear Search\n3.Exit");
printf("\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
printf("\n Enter how many elements: ");
scanf("%d",&n);
printf("\n Enter elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the elements to be searched");
scanf("%d",&key);
pos=linear(a,n);
if(pos==-1)
printf("\n element not found");
else
printf("\n element found at position %d",pos+1);
break;
case 2 :
printf("\n Enter how many elements: ");
scanf("%d",&n);
printf("\n Enter elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the elements to be searched");
scanf("%d",&key);
pos=linear(a,n);
if(pos==-1)
printf("\n element not found");
else
printf("\n element found at position %d",pos+1);
break;

case 3 :exit(0);

default:printf("\n Invalid coice");


break;
}
}
while(ch!=3);
}
int binary(int a[],int low,int high)
{
int mid;

if(low>high)
{
return(-1);
}
mid=(low+high)/2;
if(key==a[mid])
{

return(mid);
}
if(key<a[mid])
{
return(binary(a,low,mid-1));
}
else
{
return(binary(a,mid+1,high));
}
}

int linear(int a[],int n)


{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
return(i);
}
return(-1);
}

 Sorting

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

void display(int a[],int n);


void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);

//-----------------Main Function----------------------

int main()
{
int n,choice,i;
char ch[20];
printf("Enter no. of elements u want to sort : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Please select any option Given Below for Sorting : \n");

while(1)
{

printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Display Array.\n5. Exit
the Program.\n");
printf("\nEnter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
case 3:
insertion_sort(arr,n);
break;
case 4:

display(arr,n);
break;

case 5:
return 0;
default:
printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}

//-----------End of main function---------------------

//-------------------Display Function-----------------

void display(int arr[],int n)


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

//---------------------Bubble Sort Function-----------

void bubble_sort(int arr[],int n)


{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("After Bubble sort Elements are : ");
display(arr,n);
}

//------------------Selection Sort Function---------

void selection_sort(int arr[],int n)


{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

}
printf("After Selection sort Elements are : ");
display(arr,n);
}

//---------------Insertion Sort Function-------------------

void insertion_sort(int arr[],int n)


{
int i,j,min;
for(i=1;i<n;i++)
{
min=arr[i];
j=i-1;
while(min<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=min;
}
printf("After Insertion sort Elements are : ");
display(arr,n);
}
 Merge sort

/ Merges two subarrays of arr[].


// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

/* l is for left index and r is right index of the


sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

/* Driver program to test above functions */


int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

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


printArray(arr, arr_size);
return 0;
}

 BST

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node NODE;

NODE *insert(int item,NODE *root)


{
NODE *temp,*cur,*prev;
temp=(NODE *)malloc(sizeof(NODE));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(cur->info < item )
cur=cur->rlink;
else
cur=cur->llink;

}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
NODE *construct_BST(NODE *root)
{
int a,n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements to be inserted in the tree\n");
for (i=0;i<n;i++)
{
scanf("%d",&a);
root=insert(a,root);
}
printf("Tree Constructed Successfully!!!!!!\n");
return root;
}
void preorder(NODE *root)
{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
}

void inorder(NODE *root)


{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
}

void postorder(NODE *root)


{
if(root==NULL)
return ;
else
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
}
int search_element(NODE *root,int key)
{
NODE *cur;
int n=0;
cur=root;
if (cur!=NULL)
{
if (key==cur->info)
n=1;
else if (key<cur->info)
return search_element(cur->llink,key);
else
return search_element(cur->rlink,key);
}
else
return n;
}

NODE *minValueNode(NODE* node)


{
NODE *current = node;
/* loop to find the leftmost leaf */
while (current->llink != NULL)
current = current->llink;
return current;
}

NODE *delete_element(NODE *root,int key)


{
if (root == NULL)
return root;
if (key < root->info)
root->llink = delete_element(root->llink, key);
else if (key > root->info)
root->rlink = delete_element(root->rlink, key);
else
{
// node with only one child or no child
if (root->llink == NULL)
{
NODE *temp = root->rlink;
free(root);
return temp;
}
else if (root->rlink == NULL)
{
NODE *temp = root->llink;
free(root);
return temp;
}
// node with two children: Get the inorder successor (smallest
// in the right subtree)
else
{
NODE *temp = minValueNode(root->rlink);
root->info = temp->info;
root->rlink = delete_element(root->rlink, temp->info);
}
}
return root;
}

void main()
{
int item,ch,key,n;
NODE *root;
root=NULL;
while (1)
{
printf("\nEnter the choice\n1.ConstructBST\n2.Preorder\n3.Inorder\n4.Postorder\n5.Search
an Element\n6.Delete an Element\n7:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: root=construct_BST(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
case 5: if (root==NULL)
printf("List Empty\n");
else
{

printf("Enter the element\n");


scanf("%d",&key);
n=search_element(root,key);
if(n)
printf("Key found\n");
else
printf("Not found\n");
}
break;
case 6:
if (root==NULL)
printf("List Empty\n");
else
{
printf("Enter the element\n");
scanf("%d",&key);
n=search_element(root,key);
if(n)
root=delete_element(root,key);
else
printf("Not found\n");
}
break;
case 7: exit(0);
default: printf("Wrong Choice\n");
}
}
}

/*

*/
 KRUSKAL

#include<stdio.h>

#define MAX 30

typedef struct edge


{
int u,v,w;
}edge;

typedef struct edgelist


{
edge data[MAX];
int n;
}edgelist;

edgelist elist;

int G[MAX][MAX],n;
edgelist spanlist;

void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();

void main()
{
int i,j,total_cost;
printf("\nEnter number of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
kruskal();
print();
}

void kruskal()
{
int belongs[MAX],i,j,cno1,cno2;
elist.n=0;

for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(G[i][j]!=0)
{
elist.data[elist.n].u=i;
elist.data[elist.n].v=j;
elist.data[elist.n].w=G[i][j];
elist.n++;
}
}

sort();
for(i=0;i<n;i++)
belongs[i]=i;
spanlist.n=0;
for(i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u);
cno2=find(belongs,elist.data[i].v);
if(cno1!=cno2)
{
spanlist.data[spanlist.n]=elist.data[i];
spanlist.n=spanlist.n+1;
union1(belongs,cno1,cno2);
}
}
}

int find(int belongs[],int vertexno)


{
return(belongs[vertexno]);
}

void union1(int belongs[],int c1,int c2)


{
int i;
for(i=0;i<n;i++)
if(belongs[i]==c2)
belongs[i]=c1;
}

void sort()
{
int i,j;
edge temp;
for(i=1;i<elist.n;i++)
for(j=0;j<elist.n-1;j++)
if(elist.data[j].w>elist.data[j+1].w)
{
temp=elist.data[j];
elist.data[j]=elist.data[j+1];
elist.data[j+1]=temp;
}
}

void print()
{
int i,cost=0;
for(i=0;i<spanlist.n;i++)
{
printf("\n%d\t%d\t%d",spanlist.data[i].u,spanlist.data[i].v,spanlist.data[i].w);
cost=cost+spanlist.data[i].w;
}

printf("\n\nCost of the spanning tree=%d",cost);


}

 BFS

#include<stdio.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. Not all nodes are reachable");
break;
}
}
}

You might also like