DS Program-1
DS Program-1
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void display()
{
int i;
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nContent of stack is:\n");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
void push() {
if(top==size-1)
{
printf("\nStack is full");
return;
}
printf("\nEnter item:\n");
scanf("%d",&item);
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void main()
{
int ch;
top=-1;
clrscr();
printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
do
{
printf("\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong entry ! try again");
}
}
while(ch<=4);
getch();
}
OUTPUT:
1.push 2.pop
3.display 4.exit
Enter your choice:
1
Enter item:
100
Enter your choice:
1
Enter item:
200
Enter your choice:
1
Enter item:
300
Enter your choice:
2
Deleted item is: 300
Enter your choice:
3
Content of stack is:
100 200
Enter your choice:4
PROGRAM :
#include<stdio.h>
#include<conio.h>
#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */
Qinsert(int elem)
{ /* Function for Insert operation */
if( Qfull())
printf("\n\n Overflow!!!!\n\n");
else
{
++r;
Q[r]=elem;
}}
int Qdelete()
{
/* Function for Delete operation */
int elem;
if(Qempty())
{
printf("\n\nUnderflow!!!!\n\n");
else
{
++r;
Q[r]=elem;
}}
int Qdelete()
{
/* Function for Delete operation */
int elem;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
return(-1);
}
else
{
elem=Q[f];
f=f+1;
return(elem);
}}
int Qfull()
{
/* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
int Qempty()
{ /* Function to Check Queue Empty */
if(f> r) return 1;
return 0;
}
void display()
{
/* Function to display status of Queue */
int i;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}}
void main()
{
/* Main Program */
int opn,elem;
do
{
clrscr();
printf("\n ### Queue Operations using Arrays### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1:
printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem);
break;
case 2:
elem=Qdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3:
printf("\n\nStatus of Queue\n\n");
display();
break;
case 4:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}
while(opn != 4);
getch();
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
clrscr();
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
printf("\t%d", b[i]);
}}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}}}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}}
OUTPUT:
Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 1
Enter the number of elements: 4
Enter the elements:
10
20
30
40
Do u want to continue(y/n): y Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 2
Enter the element to delete:20
Elements after deletion: 10
30
40
Do u want to continue(y/n): y Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 3
Enter the element to search: 100
Element not found
Do u want to continue(y/n): y
Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 4
Enter the element to insert: 15
Enter the position to insert:2
Elements after insertion:
10
15
30
40
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
typedef struct list
{
int no;
struct list *next;
}
LIST;
LIST *p,*t,*h,*y,*ptr,*pt;
void create( void );
void insert( void );
void delet( void );
void display ( void );
int j,pos,k=1,count;
void main()
{
int n,i=1,opt;
clrscr();
p = NULL;
printf("%d",sizeof(LIST));
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n );
count = n;
while( i <= n)
{
create();
i++; }
printf("\nEnter your option:\n");
printf("1.Insert\t 2.Delete\t 3.Display\t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt )
{
case 1:
insert();
count++;
break;
case 2:
delet();
count--;
if ( count == 0 )
{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break; }
printf("\nEnter your option\n");
}
while( opt != 4 );
getch();
}
void create ( ) {
if( p== NULL ) {
p = ( LIST * ) malloc ( sizeof ( LIST ) );
printf( "Enter the element:\n" );
scanf( "%d",&p->no );
p->next = NULL;
h = p;
}
else
{
t= ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" );
scanf( "%d",&t->no );
t->next = NULL;
p->next = t;
p = t;
}}
void insert()
{
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be inserted:\n");
scanf("%d",&p->no);
printf("Enter the position to insert:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = p; h->next = t;
}
else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p;
t=p;
}}
void delete()
{
printf("Enter the position to delete:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = h->next ;
}
Else
{
t= h;
for(j=1;j<(pos-1);j++)
t = t->next;
pt=t->next->next;
free(t->next);->next= pt;
}}
void display()
{
t= h;
while( t->next != NULL )
{
printf("\t%d",t->no);
t = t->next;
}
printf( "\t %d\t",t->no );
}
OUTPUT:
Enter the no of nodes: 3
Enter the element: 1
Enter the element 2
Enter the element 3
Enter your option:
1. Insert 2.Delete 3.Display 4.Exit 3
List elements are:
123
Enter your option 1
Enter the element to be inserted:
12
Enter the position to insert: 1
Enter your option 3
List elements are:
12 1 2 3
Enter your option 1
Enter the element to be inserted:
13
Enter the position to insert: 3
Enter your option 1
Enter the element to be inserted:
14
Enter the position to insert:6
20
Enter your option 3
List elements are:
12 1 13 2 3 14
Enter your option 2
Enter the position to delete:1
Enter your option 3
List elements are:
1 13 2 3 14
Enter your option 2
Enter the position to delete:3
Enter your option 3
List elements are:
1 13 3 14
Enter your option 2
Enter the position to delete:4
Enter your option 3
List elements are:
1 13 3
Enter your option: 6
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
typedef struct list
{
int no;
struct list *next;
struct list *pre;
}
LIST;
LIST *p,*t,*h;
void create( void );
void insert( void );
void delet( void );
void display ( void );
int j,pos,k=1,count;
void main() {
int n,i=1,opt;
clrscr();
p = NULL;
printf( "Enter the no of nodes :\n " );
scanf( "%d",&n );
count = n;
while( i <= n)
{
create();
i++;
}
printf("\nEnter your option:\n");
printf("1.Insert\t 2.Delete\t 3.Display\t 4.Exit\n");
do
{
scanf("%d",&opt);
switch( opt )
{
case 1:
insert();
count++;
break;
case 2:
delete();
count--;
if ( count == 0 )
{
printf("\n List is empty\n");
}
break;
case 3:
printf("List elements are:\n");
display();
break; }
printf("\nEnter your option\n");
}
while( opt != 4 );
getch();
}
void create ()
{
if( p == NULL )
{
p = ( LIST * ) malloc ( sizeof ( LIST ) );
printf( "Enter the element:\n" );
scanf( "%d",&p->no );
p->next = NULL;
p->pre = NULL;
h = p;
}
else
{
t= ( LIST * ) malloc (sizeof( LIST ));
printf( "\nEnter the element" );
scanf( "%d",&t->no );
t->next = NULL; p->next = t; t->pre = p;
p = t;
}}
void insert()
{
t=h;
p = ( LIST * ) malloc ( sizeof(LIST) );
printf("Enter the element to be insrted:\n");
scanf("%d",&p->no);
printf("Enter the position to insert:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h = p; h->next = t; t->pre = h; h->pre = NULL;
}
else
{
for(j=1;j<(pos-1);j++)
t = t->next;
p->next = t->next;
t->next = p; p->pre = t; }}
void delete()
{
printf("Enter the position to delete:\n");
scanf( "%d",&pos );
if( pos == 1 )
{
h =h->next ;
h->pre = NULL;
}
else
{
t= h;
for(j=1;j<(pos-1);j++)
t = t->next;
t->next = t->next->next;
t->next->pre = t;
free( t->next );
}}
void display()
{
t= h;
while( t->next != NULL )
{
printf("%d\n",t->no);
t = t->next;
}
printf( "%d",t->no );
}
OUTPUT:
Enter the no of nodes: 3
Enter the element3
Enter your option:
1.Insert 2.Delete 3.Display 4.Exit 3
List elements are:
123
Enter your option 1
Enter the element to be inserted:22
Enter the position to insert:1
Enter your option 3
List elements are:
22 1 2 3
Enter your option 1
Enter the element to be inserted:
11
Enter the position to insert:5
Enter your option 3
26
List elements are:
22 1 2 3 11
Enter your option
2
Enter the position to delete:
1
Enter your option
3
List elements are:
1 2 3 11
Enter your option 4
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void pop();
void push(int value);
void display();
struct node
{
int data;
struct node *link;
};
struct node *top=NULL,*temp;
void main()
{
int choice,data;
while(1) //infinite loop is used to insert/delete infinite number of elements in stack
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnter ur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: //To push a new element into stack
printf("Enter a new element :");
scanf("%d",&data);
push(data);
break;
case 2: // pop the element from stack
pop();
break;
case 3: // Display the stack elements
display();
break;
case 4: // To exit
exit(0);
}}
getch();
//return 0;
}
void display()
{
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
printf("\n The Contents of the Stack are...");
while(temp!=NULL)
{
printf(" %d ->",temp->data);
temp=temp->link;
}
}
void push(int data)
{
temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new element.
temp->data=data;
temp->link=top;
top=temp;
display();
}
void pop()
{
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
display();
}
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :10
The Contents of the Stack are... 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :20
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :30
The Contents of the Stack are... 30 -> 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:2
The poped element is 30
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:3
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:4
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}
*front = NULL, *rear = NULL;
void insert();
void delete();
void display();
int item;
void main()
{
int ch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert();
break;
case 2:delet();
break;
case 3:display();
break;
case 4:exit(0);
default:
printf("\n\nInvalid choice. Please try again...\n");
}
}
while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}}}
OUTPUT:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Item deleted: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:3
15 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:4
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
int top=-1;
char pop();
char stack[MAX];
void push(char item);
int prcd(char symbol) {
switch(symbol) {
case '+':
case '-':
return 2;
break;
case '*':
case '/':
return 4;
break;
case '^':
case '$':
return 6;
break;
case '(':
case ')':
case '#':
return 1;
break;
}}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:
return 0;
}}
void convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++; }
pop();//pop out.
}
else
{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else
{
while(prcd(symbol)<=prcd(stack[top])){
postfix[j]=pop();
j++; }
push(symbol);
}//end of else.
}//end of else.
}//end of else.
}//end of for.
while(stack[top]!='#'){
postfix[j]=pop();
j++; }
postfix[j]='\0';//null terminate string. }
void main() {
char infix[20],postfix[20];
clrscr();
printf("Enter the valid infix string:\n");
gets(infix);
convertip(infix,postfix);
printf("The corresponding postfix string is:\n");
puts(postfix);
getch();
}
void push(char item)
{
top++;
stack[top]=item; }
char pop() {
char a;
a=stack[top];
top--;
return a;
}
OUTPUT:
Enter the valid infix string:
(a+b)*c
The corresponding postfix string is:
ab+c*
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct stack{
int top;
float a[50];
}s;
void main()
{
char pf[50];
float d1,d2,d3;
int i;
clrscr();
s.top=-1;
printf("\n\n Enter the postfix expression:");
gets(pf);
for(i=0;pf[i]!='\0';i++)
{
switch(pf[i]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
s.a[++s.top]=pf[i]-'0';
break;
case '+':d1=s.a[s.top--];
d2=s.a[s.top--];
s.a[++s.top]=d1+d2;
break;
case '-':d2=s.a[s.top--];
d1=s.a[s.top--];
s.a[++s.top]=d1-d2;
break;
case '*':
d2=s.a[s.top--];
d1=s.a[s.top--];
s.a[++s.top]=d1*d2;
break;
case '/':
d2=s.a[s.top--];
d1=s.a[s.top--];
44
s.a[++s.top]=d1/d2;
break;
}}
printf("\n\n The value of expression is%f",s.a[s.top]);
getch();
}
OUTPUT:
Enter the postfix expression:56*7-
The value of expression is23.000000
PROGRAM:
#include<stdio.h>
void main()
{
int c1[10],e1[10],c2[10],e2[10],i,rc[20],re[20],n,m,k,l,j;
clrscr();
printf("Enter the highest index of 1st Polynomial : ");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
printf("Enter the coefficient of x^%d : ",i);
scanf("%d",&c1[i]); e1[i]=i;
}
printf("Enter the highest index of 2nd Polynomial : ");
scanf("%d",&m);
for(i=m;i>=0;i--)
{
printf("Enter the coefficient of x^%d : ",i);
scanf("%d",&c2[i]); e2[i]=i;
}
printf("\nThe first Polynomial is : \n");
for(i=n;i>=0;i--)
{
printf("%d x^%d",c1[i],e1[i]);
if(i>0) printf(" + ");
}
printf("\nThe second Polynomial is : \n");
for(i=m;i>=0;i--)
{
printf("%d x^%d",c2[i],e2[i]);
if(i>0) printf(" + ");
}
k=n; l=m; j=0;
while(k>=0 && l>=0)
{
if(k>=0 || l>=0)
{
if(e1[k]==e2[l])
{
rc[j]=c1[k]+c2[l]; re[j]=e1[k];
46
j=j+1; k=k-1; l=l-1;
}
else if(e1[k]>e2[l])
{
rc[j]=c1[k]; re[j]=e1[k]; j=j+1; k=k-1;
}
else if(e1[k]<e2[l])
{
rc[j]=c2[l]; re[j]=e2[l]; j=j+1; l=l-1;
}}
else if (k==0 && l>0)
{
rc[j]=c2[l]; re[j]=e2[l]; j=j+1; l=l-1;
}
else if(k>0 && l==0)
{
rc[j]=c1[k]; re[j]=e1[k]; j=j+1; k=k-1;
}}
printf("\nThe Sum of the two Polynomials is : \n");
j=j-1;
for(i=0;i<=j;i++)
{
printf("%d x^%d",rc[i],re[i]);
}
OUTPUT:
Enter the highest degree of the 1st Polynomial: 2
Enter the coefficient of x^2: 3
Enter the coefficient of x^1: 2
Enter the coefficient of x^0: 1
Enter the highest degree of the 2nd Polynomial: 2
Enter the coefficient of x^2: 5
Enter the coefficient of x^1: 0
Enter the coefficient of x^0: -1
PROGRAM:
OUTPUT:
Current queue :
Current queue : h
Current queue :hw
Current queue :hwr
Current queue :wr
Current queue : r
Current queue :rc
Current queue : c
Current queue : dequeue:
Queue is empty
Current queue :
PROGRAM:
#include<stdio.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}
node;
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}
void preorder(node *t) //address of root node is passed in t
{
if(t!=NULL)
{
printf("\n%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
}}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
return 0;
}
OUTPUT:
Enter data(-1 for no data):5
Enter left child of 5:
Enter data(-1 for no data):7
Enter left child of 7:
Enter data(-1 for no data):8
Enter left child of 8:
Enter data(-1 for no data):3
Enter left child of 3:
Enter data(-1 for no data):-1
Enter right child of 3:
Enter data(-1 for no data):-1
Enter right child of 8:
Enter data(-1 for no data):-1
Enter right child of 7:
Enter data(-1 for no data):-1
Enter right child of 5:
Enter data(-1 for no data):-1
The preorder traversal of tree is:
5
7
8
3
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct BST
{
int data;
struct BST *left;
struct BST *right;
}node;
node *create();
void insert(node *,node *);
void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
printf("nDo you want to enter more(y/n)?");
getchar();
scanf("%c",&ch);
}
while(ch=='y'|ch=='Y');
printf("nPreorder Traversal: ");
preorder(root);
return 0;
}
node *create()
{
node *temp;
printf("nEnter data:");
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;
}
void insert(node *root,node *temp)
{
if(temp->data<root->data)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}}
OUTPUT:
PROGRAM:
#include<stdio.h>
typedef struct node
{
int data;
struct node *left,*right;
int ht;
}
node;
node *insert(node *,int);
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main()
{
node *root=NULL;
int x,n,i,op;
do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
case 2: printf("\nEnter a data:");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: printf("\nEnter a data:");
scanf("%d",&x);
root=Delete(root,x);
break;
case 4: printf("\nPreorder sequence:\n");
preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
}while(op!=5);
return 0;
}
node * insert(node *T,int x)
{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}
node * Delete(node *T,int x)
{
node *p;
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(BF(T)==2)//Rebalance during windup
if(BF(T->left)>=0) T=LL(T);
else
T=LR(T);
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}
int height(node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
node * rotateright(node *x)
{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y); }
node * rotateleft(node *x)
{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * RR(node *T)
{
T=rotateleft(T);
return(T);
}
node * LL(node *T)
node * LL(node *T)
{
T=rotateright(T);
return(T);
}
node * LR(node *T)
{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T)
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
int BF(node *T)
{
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
void preorder(node *T)
{
if(T!=NULL)
{
printf("%d(Bf=%d)",T->data,BF(T));
preorder(T->left);
preorder(T->right);
}}
void inorder(node *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d)",T->data,BF(T));
inorder(T->right);
}}
OUTPUT:
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:1
Enter no. of elements:4
Enter tree data:7 12 4 9
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
7(Bf=-1)4(Bf=0)12(Bf=1)9(Bf=0)
Inorder sequence:
4(Bf=0)7(Bf=-1)9(Bf=0)12(Bf=1)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:3
Enter a data:7
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
9(Bf=0)4(Bf=0)12(Bf=0)
Inorder sequence:
4(Bf=0)9(Bf=0)12(Bf=0)
PROGRAM :
OUTPUT :
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 1
Enter data to be inserted : 52
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 1
Enter data to be inserted : 63
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 1
Enter data to be inserted : 45
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 1
Enter data to be inserted : 2
74
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 1
Enter data to be inserted : 99
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 3
99 63 45 2 52
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 2
The deleted value is : 99
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 3
63 52 45 2
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 2
The deleted value is : 63
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 2
The deleted value is : 52
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 3
45 2
.....MAIN MENU.....
1.Insert.
2.Delete.
3.Display.
4.Quit.
Enter your choice : 4
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main() {
create_graph();
BF_Traversal();
return 0; }
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS:\n");
scanf("%d", &v);
BFS(v); }
void BFS(int v) {
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
for(i=0; i<n; i++)
{
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}}}
printf("\n");
}}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front ==-1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}}
int isEmpty_queue()
{
if(front ==-1 || front > rear)
return 1;
else
return 0; }
int delete_queue()
{
int delete_item;
if(front ==-1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d %d",&origin,&destin);
if((origin == -1) && (destin == -1))
break;
if(origin>=n || destin>=n || origin<0 || destin<0)
{
printf("Invalid edge!\n");
count--;
}
else
{
adj[origin][destin] = 1;
}}}
OUTPUT:
PROGRAM :
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node *next;
int vertex;
}
node;
node *G[20];
int visited[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
void main() {
int i;
read_graph();
for(i=0;i<n;i++)
visited[i]=0;
DFS(0); }
void DFS(int i) {
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
G[i]=NULL;
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}}}
void insert(int vi,int vj)
{
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else
{
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}}
OUTPUT:
PROGRAM :
#include<stdio.h>
#include<conio.h>
static int m, n;
static int c=0;
static int count=0;
int g[50][50];
int x[50];
void nextValue(int k);
void GraphColoring(int k);
void main()
{
int i, j;
int temp;
clrscr();
printf("\nEnter the number of nodes: " );
scanf("%d", &n);
printf("\nIf edge exists then enter 1 else enter 0 \n");
for(i=1; i<=n; i++)
{
x[i]=0;
for(j=1; j<=n; j++)
{
if(i==j)
88
g[i][j]=0;
else
{
printf("%d -> %d: " , i, j);
scanf("%d", &temp);
g[i][j]=g[j][i]=temp;
}}}
printf("\nEnter Adjacency Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &g[i][j]);
}}
printf("\nPossible Solutions are\n");
for(m=1;m<=n;m++)
{
if(c==1)
{
break;
}
GraphColoring(1);
}
printf("\nThe chromatic number is %d", m-1);
printf("\nThe total number of solutions is %d", count);
getch();
}
void GraphColoring(int k)
{
int i;
while(1)
{
nextValue(k);
if(x[k]==0) {
return; }
if(k==n) {c=1;
for(i=1;i<=n;i++) {
printf("%d ", x[i]); }
count++;
printf("\n");
}
else
GraphColoring(k+1); }}
void nextValue(int k) {
int j;
while(1)
{
x[k]=(x[k]+1)%(m+1);
if(x[k]==0) {
return; }
for(j=1;j<=n;j++)
{
if(g[k][j]==1&&x[k]==x[j])
break;
}
if(j==(n+1))
{
return;
}}}
OUTPUT:
Enter the number of nodes: 5
Enter Adjacency Matrix:
01011
10110
01011
11101
10110
Possible Solutions are
12132
13123
21231
23213
31321
32312
The chromatic number is 3
The total number of solutions are 6
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i,size,sech;
printf("\n\t-- Linear Search --\n\n");
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]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++)
{
if(sech==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}}
getch();
}
OUTPUT:
-- Linear Search --
Enter total no. of elements : 5
Enter 1 element : 10
Enter 2 element : 4
Enter 3 element : 2
Enter 4 element : 17
Enter 5 element : 100
Enter the element to be searched: 17
Element exits in the list at position : 4
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main(){
int n,i,search,f=0,low,high,mid,a[20];
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the search element:");
scanf("%d",&search);
low=1;
high=n;
while(low<=high){
mid=(low+high)/2;
if(search<a[mid]){
high=mid-1;
}
else if(search>a[mid]){
low=mid+1;
}
else{
f=1;
printf("obtained in the position %d:",mid);
getch();
exit();
}}
if(f==0)
printf("not present");
getch();
}
OUTPUT:
Enter the n value:5
Enter the number in ascending order a[1]=10
Enter the number in ascending order a[2]=8
Enter the number in ascending order a[3]=9
Enter the number in ascending order a[4]=24
Enter the number in ascending order a[5]=1
Enter the search element:9
obtained in the position 3:
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main(){
int n, i, j, temp , a[100];
printf("Enter the total integers you want to enter (make it less than 100):\n");
scanf("%d",&n);
printf("Enter the %d integer array elements:\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(a[j+1]<a[j]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}}}
printf("The sorted numbers are:");
for(i=0;i<n;i++){
printf("%3d",a[i]);
}
getch();
}
OUTPUT:
Enter the total integers you want to enter (make it less than 100):
5
Enter the 5 integer array elements:
99
87
100
54
150
The sorted numbers are: 54 87 99 100 150
PROGRAM :
#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
void main(){
int arr[30];
int i,size;
printf("\n\t------- Merge sorting method -------\n\n");
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(arr,0,size-1);
printf("\n\t------- Merge sorted elements -------\n\n");
for(i=0; i<size; i++)
printf("%d ",arr[i]);
getch(); }
void part(int arr[],int min,int max)
{
int mid;
if(min<max){
mid=(min+max)/2;
part(arr,min,mid);
part(arr,mid+1,max);
merge(arr,min,mid,max);}}
void merge(int arr[],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++;
100
}}
else
{
for(k=j; k<=mid; k++)
{
tmp[i]=arr[k];
i++;
}}
for(k=min; k<=max; k++)
arr[k]=tmp[k];
}
OUTPUT:
------- Merge sorting method -------
Enter total no. of elements : 9
Enter 1 element : 100
Enter 2 element : 99
Enter 3 element : 75
Enter 4 element : 155
Enter 5 element : 11
Enter 6 element : 43
Enter 7 element : 13
Enter 8 element : 35
Enter 9 element : 48
------- Merge sorted elements -------
11 13 35 43 48 75 99 100 155
PROGRAM :
#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
void main(){
int arr[30];
int i,size;
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(arr,0,size-1);
printf("Quick sorted elements are as : \n");
for(i=0; i<size; i++)
printf("%d\t",arr[i]);
getch();
}
void qsort(int arr[20], 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(arr,fst,j-1);
qsort(arr,j+1,last);
}}
OUTPUT:
Enter total no. of the elements: 5
Enter total 5 elements:
11
97
54
10
100
Quick sorted elements are as: 10 11 54 97 100
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct item
{
int key;
int value;
};
struct hashtable_item
{
int flag;
struct item *data;
};
struct hashtable_item *array;
int size = 0;
int max = 10;
int hashcode(int key)
{
return (key % max);
}
void init_array()
{
int i;
for (i = 0; i < max; i++)
{
array[i].flag = 0;
array[i].data = NULL;
}}
/* this function inserts an element in the hash table */
void insert(int key, int value)
{
int index = hashcode(key);
int i = index;
int h = 1;
struct item *new_item = (struct item*) malloc(sizeof(struct item));
new_item->key = key;
new_item->value = value;
/* probing through the array until an empty space is found */
while (array[i].flag == 1)
{
if (array[i].data->key == key)
{
/* case when already present key matches the given key */
printf("\n This key is already present in hash table, hence updating it's value\n");
array[i].data->value = value;
return;
}
i = (i + (h * h)) % max;h++;
if (i == index)
{
printf("\n Hash table is full, cannot add more elements \n");
return;
}}
array[i].flag = 1;
array[i].data = new_item;
printf("\n Key (%d) has been inserted\n", key);
size++;
}
/* to remove an element form the hash table array */
void remove_element(int key)
{
int index = hashcode(key);
int i = index;
int h = 1;
/* probing through the hash table until we reach at location where there had not been an element
even once */
while (array[i].flag != 0)
{
if (array[i].flag == 1 && array[i].data->key == key)
{
/* case where data exists at the location and its key matches to the given key */
array[i].flag = 2;
array[i].data = NULL;
size--;
printf("\n Key (%d) has been removed \n", key);
return;
}
i = (i + (h * h)) % max;h++;
if (i == index)
{
break;
}}
printf("\n Key does not exist \n");
}
/* to display the contents of hash table */
void display()
{
int i;
for(i = 0; i < max; i++)
{
if (array[i].flag != 1)
{
printf("\n Array[%d] has no elements \n", i);
}
else
{
OUTPUT:
Implementation of Hash Table in C with Quadratic Probing
MENU-:
1. Inserting item in the Hash table
2. Removing item from the Hash table
3. Check the size of Hash table
4. Display Hash table
Please enter your choice-: 3
Size of hash table is-: 0
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Quadratic Probing
MENU-:
1. Inserting item in the Hash table
110
2. Removing item from the Hash table
3. Check the size of Hash table
4. Display Hash table
#include<stdio.h>
#include<stdlib.h>
/* to store a data (consisting of key and value) in hash table array */
struct item
{
int key;
int value;
};
/* each hash table item has a flag (status) and data (consisting of key and value) */
struct hashtable_item
{
int flag;
/*
* flag = 0 : data does not exist
* flag = 1 : data exists
* flag = 2 : data existed at least once
struct item *data;
};
struct hashtable_item *array;
int size = 0;
int max = 10;
/* initializing hash table array */
void init_array()
{
int i;
for (i = 0; i < max; i++)
{
array[i].flag = 0;
array[i].data = NULL;
}}
/* to every key, it will generate a corresponding index */
int hashcode(int key)
{
return (key % max);
}
/* to insert an element in the hash table */
void insert(int key, int value)
{
int index = hashcode(key);
int i = index;
/* creating new item to insert in the hash table array */
struct item *new_item = (struct item*) malloc(sizeof(struct item));
new_item->key = key;
new_item->value = value;
/* probing through the array until we reach an empty space */
while (array[i].flag == 1)
{
if (array[i].data->key == key)
{
/* case where already existing key matches the given key */
printf("\n Key already exists, hence updating its value \n");
array[i].data->value = value;
return;
}
i = (i + 1) % max;
if (i == index)
{
printf("\n Hash table is full, cannot insert any more item \n");
return;
}}
array[i].flag = 1;
array[i].data = new_item;
size++;
printf("\n Key (%d) has been inserted \n", key);
}
/* to remove an element from the hash table */
void remove_element(int key)
{
int index = hashcode(key);
int i = index;
/* probing through array until we reach an empty space where not even once an element had been
present */
while (array[i].flag != 0)
{
if (array[i].flag == 1 && array[i].data->key == key )
{
// case when data key matches the given key
array[i].flag = 2;
array[i].data = NULL;
size--;
printf("\n Key (%d) has been removed \n", key);
return;
}
i = (i + 1) % max;
if (i == index)
{
break;
}
}
printf("\n This key does not exist \n");
}
/* to display all the elements of hash table */
void display()
{
int i;
for (i = 0; i < max; i++)
{
struct item *current = (struct item*) array[i].data;
if (current == NULL)
{
printf("\n Array[%d] has no elements \n", i);
}
else
{
printf("\n Array[%d] has elements -: \n %d (key) and %d(value) ", i, current->key, current->value);
}}}
int size_of_hashtable()
{
return size;
}
void main()
{
int choice, key, value, n, c;
clrscr();
array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item*));
init_array();
do
{
printf("Implementation of Hash Table in C with Linear Probing \n\n");
printf("MENU-: \n1.Inserting item in the Hashtable" "\n2.Removing item from the Hashtable"
"\n3.Check the size of Hashtable" "\n4.Display Hashtable" "\n\n Please enter your choice-:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hashtable\n");
printf("Enter key and value-:\t");
scanf("%d %d", &key, &value);
insert(key, value);
break;
case 2:
printf("Deleting in Hashtable \n Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hashtable is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}
while(c == 1);
getch();
}
OUTPUT:
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 3
Size of Hashtable is-: 0
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 1
Inserting element in Hashtable
Enter key and value-: 12 10
Key (12) has been inserted
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 1
Inserting element in Hash table
Enter key and value-: 122
4
Key (122) has been inserted
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 3
Size of Hashtable is-: 2
Do you want to continue
-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 4
Array[0] has no elements
Array[1] has no elements
Array[2] has elements-:12 (key) and 10 (value)
Array[3] has elements-:122(key) and 5(value)
Array[4] has no elements
Array[5] has no elements
Array[6] has no elements
Array[7] has no elements
Array[8] has no element
Array[9] has no elements
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashta
Please enter your choice-: 2
Deleting in Hashtable
Enter the key to delete-: 12
Key (122) has been removed
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear Probing
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Please enter your choice-: 2
Deleting in Hashtable
Enter the key to delete-: 56
This key does not exist
Do you want to continue-:(press 1 for y
PROGRAM:
#include <stdio.h>
int main()
{
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(i=0;i<n;i++){
printf("Enter row %d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++){
indeg[i]=0;
flag[i]=0;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];
printf("\nThe topological order is:");
while(count<n)
{
for(k=0;k<n;k++){
if((indeg[k]==0) && (flag[k]==0)){
printf("%d ",(k+1));
flag [k]=1;
}
for(i=0;i<n;i++){
if(a[i][k]==1)
indeg[k]--;
}}
count++;
}
return 0;
}
OUTPUT: