Anda di halaman 1dari 80

1|Page

B.E Regulation 2017 Data Structure Lab Manual

DATA STRUCTURE
CS8391

Balaji N , GCE Bodi


2|Page

Content

S.No Experiment Page No

1 Implementation of List using Array and Linked List

2 Implementation of Stack using Array and Linked List

3 Implementation of Queue using Array and Linked List

4 Application of List,Stack,Queue ADTs

5 Implementation of Binary Tree and its operation

6 Implementation of Binary Search Tree

7 Implementation of AVL Tree

8 Implementation of Heap

9 Graph Representation and Traversal Algorithm

10 Application of Graph

11 Implementation of Searching and Sorting Algorithm

12 Hashing Technique- any two collision

Balaji N , GCE Bodi


3|Page

Implementation of List using Array and Linked List

Implementation of List using Array:

#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;

Balaji N , GCE Bodi


4|Page

case 5:
display();
break;
case 6:
exit(0);
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 elements");
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++)

Balaji N , GCE Bodi


5|Page

{
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);
break;
}
else
{
continue;
}
}
if(i==n)
printf("Value %d is not in the list::", e);
}

void insert()
{
printf("\n Enter the position u need to insert::");

Balaji N , GCE Bodi


6|Page

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]);
}
}

Balaji N , GCE Bodi


7|Page

Implementation of List using Linked List:

#include <stdio.h>
#include <malloc.h>
#define ISEMPTY printf("\nEMPTY");

/*
* Node Declaration
*/
struct node
{
int value;
struct node *next;
};
typedef struct node snode;
snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void sorted_ascend();
void delete_pos();
void search();
void update_val();
void display();
void rev_display(snode *);

snode *newnode, *ptr, *prev, *temp;


snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
int ch;
char ans = 'Y';

Balaji N , GCE Bodi


8|Page

while (ans == 'Y'||ans == 'y')


{
printf("\n---------------------------------\n");
printf("\nOperations on singly linked list\n");
printf("\n---------------------------------\n");
printf("\n1.Insert node at first");
printf("\n2.Insert node at last");
printf("\n3.Insert node at position");
printf("\n4.Sorted Linked List in Ascending Order");
printf("\n5.Delete Node from any Position");
printf("\n6.Update Node Value");
printf("\n7.Search Element in the linked list");
printf("\n8.Display List from Beginning to end");
printf("\n9.Display List from end using Recursion");
printf("\n10.Exit\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\nEnter your choice");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\n...Inserting node at first...\n");
insert_node_first();
break;
case 2:
printf("\n...Inserting node at last...\n");
insert_node_last();
break;
case 3:
printf("\n...Inserting node at position...\n");
insert_node_pos();
break;
case 4:
printf("\n...Sorted Linked List in Ascending Order...\n");
sorted_ascend();
break;

Balaji N , GCE Bodi


9|Page

case 5:
printf("\n...Deleting Node from any Position...\n");
delete_pos();
break;
case 6:
printf("\n...Updating Node Value...\n");
update_val();
break;
case 7:
printf("\n...Searching Element in the List...\n");
search();
break;
case 8:
printf("\n...Displaying List From Beginning to End...\n");
display();
break;
case 9:
printf("\n...Displaying List From End using Recursion...\n");
rev_display(first);
break;
case 10:
printf("\n...Exiting...\n");
return 0;
break;
default:
printf("\n...Invalid Choice...\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &ans);
}
return 0;
}

/*
* Creating Node
*/
snode* create_node(int val)

Balaji N , GCE Bodi


10 | P a g e

{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->value = val;
newnode->next = NULL;
return newnode;
}
}

/*
* Inserting Node at First
*/
void insert_node_first()
{
int val;

printf("\nEnter the value for the node:");


scanf("%d", &val);
newnode = create_node(val);
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\n----INSERTED----");

Balaji N , GCE Bodi


11 | P a g e

/*
* Inserting Node at Last
*/
void insert_node_last()
{

/* ds
* Inserting Node at position
*/
void insert_node_pos()
{
int pos, val, cnt = 0, i;

printf("\nEnter the value for the Node:");


scanf("%d", &val);
newnode = create_node(val);
printf("\nEnter the position ");
scanf("%d", &pos);
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{

Balaji N , GCE Bodi


12 | P a g e

temp = first;
first = newnode;
first->next = temp;
}
printf("\nInserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf("\n----INSERTED----");
}
else
{
printf("Position is out of range");
}
}

/*
* Sorted Linked List
*/
void sorted_ascend()
{
snode *nxt;
int t;

if (first == NULL)
{
ISEMPTY;
printf(":No elements to sort\n");
}
else

Balaji N , GCE Bodi


13 | P a g e

{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
for (nxt = ptr->next;nxt != NULL;nxt = nxt->next)
{
if (ptr->value > nxt->value)
{
t = ptr->value;
ptr->value = nxt->value;
nxt->value = t;
}
}
}
printf("\n---Sorted List---");
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->value);
}
}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
int pos, cnt = 0, i;

if (first == NULL)
{
ISEMPTY;
printf(":No node to delete\n");
}
else
{
printf("\nEnter the position of value to be deleted:");
scanf(" %d", &pos);
ptr = first;

Balaji N , GCE Bodi


14 | P a g e

if (pos == 1)
{
first = ptr->next;
printf("\nElement deleted");
}
else
{
while (ptr != NULL)
{
ptr = ptr->next;
cnt = cnt + 1;
}
if (pos > 0 && pos <= cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
}
else
{
printf("Position is out of range");
}
free(ptr);
printf("\nElement deleted");
}
}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
int oldval, newval, flag = 0;

Balaji N , GCE Bodi


15 | P a g e

if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to update\n");
}
else
{
printf("\nEnter the value to be updated:");
scanf("%d", &oldval);
printf("\nEnter the newvalue:");
scanf("%d", &newval);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
if (ptr->value == oldval)
{
ptr->value = newval;
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nUpdated Successfully");
}
else
{
printf("\nValue not found in List");
}
}
}

/*
* searching an element in a non-empty list
*/
void search()
{
int flag = 0, key, pos = 0;

Balaji N , GCE Bodi


16 | P a g e

if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list\n");
}
else
{
printf("\nEnter the value to search");
scanf("%d", &key);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
pos = pos + 1;
if (ptr->value == key)
{
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nElement %d found at %d position\n", key, pos);
}
else
{
printf("\nElement %d not found in list\n", key);
}
}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to display\n");
}

Balaji N , GCE Bodi


17 | P a g e

else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->value);
}
}
}

/*
* Display non-empty list in Reverse Order
*/
void rev_display(snode *ptr)
{
int val;

if (ptr == NULL)
{
ISEMPTY;
printf(":No nodes to display\n");
}
else
{
if (ptr != NULL)
{
val = ptr->value;
rev_display(ptr->next);
printf("%d\t", val);
}

}
}/

Balaji N , GCE Bodi


18 | P a g e

Implementation of Stack using Array and Linked List

Implementation using Array:

#include<stdio.h>
#include<conio.h>
void push(int,int []);
void pop(int[]);
void display(int[]);
static int top=0;
void push(int a,int stack[])
{
if(top<10)
{
stack[top]=a;
top++;
printf("\nEnter number successfully inserted to top of stack");
}
else
printf("\nStack full");
}
void pop(int stack[])
{
if(top>0)
{
top--;
printf("\nNumber poped");
}
else
{
printf("\nStack underflow");
}
}
void display(int stack[])
{
int i;
printf("\n");
if(top<=0)

Balaji N , GCE Bodi


19 | P a g e

printf("\nNo number to display in stack");


else
{
for(i=0;i<top;i++)
{
printf("%d\t",stack[i]);
}
}
}
void main()
{
int n,choice,x=1,stack[50];
clrscr();
while(x)
{
printf("\nStack Implementation\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your
choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the number to insert:");
scanf("%d",&n);
push(n,stack);
display(stack);
break;
case 2:
pop(stack);
display(stack);
break;
case 3:
display(stack);
break;
default:
printf("\nExit");
x=0;
}
}

Balaji N , GCE Bodi


20 | P a g e

getch();
}

Implementation using Linked List:

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node snode;
snode* create_node(int);
void push(int);
void pop(int);
void display();
snode *top,*first=NULL,*last=NULL,*newnode,*prev,*ptr;
snode* create_node(int val)
{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->data = val;
newnode->next = NULL;
return newnode;
}
}

void push(int value)


{
newnode = create_node(value);
if (first == last && last == NULL)

Balaji N , GCE Bodi


21 | P a g e

{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
last->next = newnode;
last = newnode;
last->next = NULL;
}
top=last;
printf("\n----INSERTED----");
}
void pop(int value)
{
int c=0,i;
if(last->data==value)
{
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
c++;
}
ptr = first;
for (i=1;i<c;i++)
{
prev = ptr;
ptr = ptr->next;
}
last=prev;
last->next=NULL;
printf("\nTop of stack matched...\nNumber deleted...\n");
}
else
{
printf("\nNo element deleted\nYou can delete only the top value: %d",top->data);

Balaji N , GCE Bodi


22 | P a g e

printf("\nTop of stack value mismatch\n");


}
}
void display()
{
if (first == NULL)
printf(":No nodes in the list to display\n");
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->data);
}
}
}
void main()
{
int choice,x=1,n;
clrscr();
while(x)
{
printf("\nStack Implementation\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your
choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the number to insert:");
scanf("%d",&n);
push(n);
display();
break;
case 2:
printf("\nEnter the number to delete:");
scanf("%d",&n);
pop(n);
display();
break;

Balaji N , GCE Bodi


23 | P a g e

case 3:
display();
break;
default:
printf("\nExit");
x=0;
}
}
getch();
}

Balaji N , GCE Bodi


24 | P a g e

Implementation of Queue using Array and Linked List

Implementation using Array:

#include<stdio.h>
#include<conio.h>
void enqueue(int,int []);
void dequeue(int[]);
void display(int[]);
static int rear=0,front=0;
void enqueue(int a,int queue[])
{
if(rear<5)
{

queue[rear]=a;
rear++;
printf("\nEnter number successfully inserted ");
}
else
printf("Queue full");
}
void dequeue(int queue[])
{
if(rear>front)
{
int x;

x=queue[front];
front++;
printf("\nDequeued element is:%d",x);
}
else
{
printf("\nNo number to dequeue");
}
}
void display(int queue[])

Balaji N , GCE Bodi


25 | P a g e

{
int i;
printf("\n");
if(front<=0 && rear<=0)
printf("\nNo number to display in queue");
else if(front==rear)
printf("\nNo number in the queue");
else
{
for(i=front;i<rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
void main()
{
int n,b,choice,x=1,queue[10];
clrscr();
while(x)
{
printf("\nQueue Implementation\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\nEnter
your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the number to insert:");
scanf("%d",&n);
enqueue(n,queue);
display(queue);
break;
case 2:
dequeue(queue);
display(queue);
break;
case 3:
display(queue);

Balaji N , GCE Bodi


26 | P a g e

break;
default:
printf("\nExit");
x=0;
}
}
getch();
}

Implementation using Linked List:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
typedef struct node snode;
snode* create_node(int);
void enqueue(int);
void dequeue();
void display();
snode *rear=NULL,*front=NULL,*newnode,*ptr;
snode* create_node(int val)
{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->data = val;
newnode->next = NULL;
return newnode;

Balaji N , GCE Bodi


27 | P a g e

}
}

void enqueue(int value)


{
newnode = create_node(value);
if (rear == NULL && front == NULL)
{
rear = front = newnode;
rear->next = NULL;
front->next = NULL;
}
else
{
front->next = newnode;
front = newnode;
front->next = NULL;
}
printf("\n----INSERTED----\n");
}
void dequeue()
{
if(rear==NULL)
{
printf("\nStack underflow");
}
else
{
printf("\nDequeued element is %d\n",rear->data);
rear=rear->next;
if(rear==NULL)
front=rear;
}

}
void display()
{
if (rear==NULL || front==NULL)

Balaji N , GCE Bodi


28 | P a g e

printf("\nNo element found in list\n");


else
{
for (ptr = rear;ptr != NULL;ptr = ptr->next)
{
printf("%d\t", ptr->data);
}
}
}
void main()
{
int choice,x=1,n;
clrscr();
while(x)
{
printf("\nQueue Implementation\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\nEnter
your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the number to insert:");
scanf("%d",&n);
enqueue(n);
display();
break;
case 2:
dequeue();
display();
break;
case 3:
display();
break;
default:
printf("\nExit");
x=0;
}
}

Balaji N , GCE Bodi


29 | P a g e

getch();
}

Balaji N , GCE Bodi


30 | P a g e

Application of List,Stack,Queue

Application of Stack:

#include<stdio.h>
#include<conio.h>
void push(char[],char,int);
void pop(char[],char[],int,int);
void main()
{
char stack[50],x[50],y[50],m,n;
int i,top_x=0,top_y=0,k;
clrscr();
printf("\nEnter the expression:");
gets(stack);
for(i=0;i<=strlen(stack);i++)
{
if(i==strlen(stack))
{
for(i=top_y-1;i>=0;i--)
{
pop(x,y,top_x,top_y);
top_y--;
top_x++;
}
break;
}
else if(stack[i]>='a' && stack[i]<='z' || stack[i]>='A' && stack[i]<='Z')
{
m=stack[i];
push(x,m,top_x);
top_x++;
}
else if(stack[i]=='(')
{
n=stack[i];
push(y,n,top_y);
top_y++;

Balaji N , GCE Bodi


31 | P a g e

}
else if(stack[i]==')')
{
do
{
pop(x,y,top_x,top_y);
top_y--;
top_x++;

}while(y[top_y-1]!='(');

}
else if(stack[i]=='+' || stack[i]=='-' || stack[i]=='*' || stack[i]=='/')
{
if(top_y==0)
{
n=stack[i];
push(y,n,top_y);
top_y++;
}
else if(y[top_y-1]=='*' || y[top_y-1]=='/' || y[top_y-1]==stack[i])
{
pop(x,y,top_x,top_y);
top_y--;
top_x++;
n=stack[i];
push(y,n,top_y);
top_y++;
}

else
{
n=stack[i];
push(y,n,top_y);
top_y++;
}
}
}

Balaji N , GCE Bodi


32 | P a g e

printf("\nPostfix Expression:\n");
for(i=0;i<top_x;i++)
{
if(x[i]!='(')
printf("%c",x[i]);
}
getch();
}
void pop(char x[50],char y[50],int top_x,int top_y)
{
x[top_x]=y[top_y-1];
}
void push(char x[50],char m,int top_x)
{
x[top_x]=m;
}

Application of queue:

Task Management:

#include<stdio.h>
#include<conio.h>
void enqueue(int,int[]);
void dequeue(int[],int[]);
void display(int[],int[]);
static int rear=0,front=0,k=0;
int queue[10],lst[10];
void enqueue(int c,int queue[])
{
if(rear<10)
{

queue[rear]=c;
rear++;
printf("\nTask set");
}
else

Balaji N , GCE Bodi


33 | P a g e

printf("Queue full.No task to insert");


}
void dequeue(int lst[],int queue[])
{
if(rear>front)
{
if(queue[front]==1)
{
k++;
printf("\nElement deleted");
front++;
display(lst,queue);
}
else if(queue[front]==2)
{
printf("\nFront element is:%d",lst[k]);
front++;
}
}
else
{
printf("\nNo task available to do....");
}
}
void display(int lst[],int queue[])
{
int i,n=0;
printf("\nElement in the two array are:");
if(n==1)
printf("\nNo element");
else
{
printf("\nTask Schedule:");
for(i=front;i<rear;i++)
{
printf("%d\t",queue[i]);
}
printf("\nList element:");

Balaji N , GCE Bodi


34 | P a g e

for(i=k;i<10;i++)
{
printf("%d\t",lst[i]);
n=1;
}
}
}
void main()
{
int n,b,choice,x=1,i;
int c,y=0;
printf("\nEnter any ten numbers:");
for(i=0;i<=9;i++)
{
scanf("%d",&lst[i]);
}
printf("\nTask Scheduling\nEnter the task to do:\n1 for Delete and 2 for Display\nEnter any 5
tasks\n");
for(rear=0;rear<=5;rear++)
{
scanf("%d",&queue[rear]);
}
while(x)
{
printf("\nEnter what you wish to do:\n1.Extra task to add\n2.Do the
task\n3.Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter extra task:");
scanf("%d",&c);
enqueue(c,queue);
i++;
for(y=front;y<rear;y++)
{
printf("%d\t",queue[y]);
}

Balaji N , GCE Bodi


35 | P a g e

break;
case 2:
dequeue(lst,queue);
break;
default:
printf("\nExit");
x=0;
}
}
getch();
}

Application of List(Student Record):

#include<stdio.h>
#include<conio.h>
struct student
{
int roll_num;
char name[50];
char dob[15];
char dept[10];
int year;
};
void main()
{
struct student s[50];
int k,j,n,ch,i=0,x,flag=0,m=1,q;
clrscr();
while(m)
{
printf("\nThis file must contains 50 records only\n1.Add student record\n2.Delete student
record\n3.Displey record\n4.Search record\n5.Exit\nEnter your choice:");
scanf("%d",&ch);

Balaji N , GCE Bodi


36 | P a g e

switch(ch)
{
case 1:
printf("\nRoll Number:");
scanf("%d",&s[i].roll_num);
printf("\nName:");
scanf("%s",&s[i].name);
printf("\nDOB:");
scanf("%s",&s[i].dob);
printf("\nDept:");
scanf("%s",&s[i].dept);
printf("\nYear:");
scanf("%d",&s[i].year);
i++;
printf("\nRecord successfully inserted");
break;
case 2:
printf("\nEnter the roll num to delete:");
scanf("%d",&x);
for(k=0;k<i;k++)
{
if(s[k].roll_num==x)
{
q=k;
flag=1;
break;
}
}
for(k=q;k<i;k++)
{
s[k]=s[k+1];
}
if(flag==0)
printf("\nRoll number not found");
else
i--;
break;
case 3:

Balaji N , GCE Bodi


37 | P a g e

printf("Roll Num\tName\tDOB\t\tDept\tYear\n");
for(j=0;j<i;j++)
{

printf("%d\t\t%s\t%s\t%s\t%d\n",s[j].roll_num,s[j].name,s[j].dob,s[j].dept,s[j].year);
}
break;
case 4:
printf("\nEnter the roll number to search:");
scanf("%d",&x);
for(j=0;j<i;j++)
{
if(s[j].roll_num==x)
{
printf("\nRecord found");
flag=1;
break;
}
}
if(flag==0)
printf("\nNo record found");
break;
case 5:
printf("\nExit");
m=0;
break;
default:
printf("\nWrong entry");
}
}
getch();
}

Balaji N , GCE Bodi


38 | P a g e

Implementation of Binary Tree and its operations

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;
static int flag=0,n=1,f=0;
node *ptr,*p1,*p2,*newnode;
node *create()
{
node *root;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);

if(x==-1)
return NULL;

root=(node*)malloc(sizeof(node));
root->data=x;

printf("Enter left child of %d:\n",x);


root->left=create();

printf("Enter right child of %d:\n",x);


root->right=create();

return root;
}
node *delet(node *root,int x)
{
if(root!=NULL)
{
if(root->data==x)

Balaji N , GCE Bodi


39 | P a g e

{
ptr=root->left;
p1=root->left;
p2=root->right;
while(p1->left!=NULL)
p1=p1->left;
p1->left=p2;
flag=0;
return ptr;
}
else if(root->left->data==x)
{
p1=root->left->left;
p2=root->left->right;
free(root->left);
root->left=p1;
while(p1->left != NULL)
p1=p1->left;
p1->left=p2;
flag=0;
return(root);
}
else if(root->right->data==x)
{
p1=root->right->left;
p2=root->right->right;
free(root->right);
root->right=p1;
while(p1->left != NULL)
p1=p1->left;
p1->left=p2;
flag=0;
return(root);
}
else
{
flag=1;
delet(root->left,x);

Balaji N , GCE Bodi


40 | P a g e

delet(root->right,x);
}
}
return root;

}
void search(node *root,int x)
{

if(root != NULL)
{
if(root->data==x)
{
printf("\nElement found in tree");
f=1;
}
else
{
search(root->left,x);
search(root->right,x);
}
}
return;
}
void inorder(node *root)
{

if(root != NULL)
{
inorder(root->left);
printf(" %d\t",root->data);
inorder(root->right);
}
return;
}
void postorder(node *root)
{

Balaji N , GCE Bodi


41 | P a g e

if(root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
return;
}
void preorder(node *root)
{

if(root != NULL)
{
printf(" %d\t",root->data);
preorder(root->left);
preorder(root->right);
}
return;
}
node* create_node(int val)
{
newnode = (node *)malloc(sizeof(node));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->data = val;
newnode->left = NULL;
newnode->right=NULL;
return newnode;
}
}
void insert(node *root, int x,int y)
{
if(root!=NULL)

Balaji N , GCE Bodi


42 | P a g e

{
if(root->data==y)
{
if(root->left!=NULL && root->right!=NULL)
printf("\nEntered parent has already two child");
else if(root->left==NULL)
root->left=create_node(x);
else if(root->right==NULL)
root->right=create_node(x);
}
else
{
insert(root->left,x,y);
insert(root->right,x,y);
}
}
return;
}

int main()
{
node *root;
int a,b,y,x=1,c;
int choice;
while(x)
{
printf("\nImplementation of Binary
Tree\n1.Create\n2.Insert\n3.Delete\n4.Traversal\n5.Search\n6.Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
root=create();
break;
case 2:
printf("\nEnter the number to insert:");
scanf("%d",&b);

Balaji N , GCE Bodi


43 | P a g e

printf("\nEnter the parent number to check:");


scanf("%d",&y);
insert(root,b,y);
break;
case 3:
printf("\nEnter the number to delete:");
scanf("%d",&a);
root=delet(root,a);
if(flag==1)
printf("Element not found in tree");
break;
case 4:
printf("\nINORDER:\n");
inorder(root);
printf("\nPOSTORDER:\n");
postorder(root);
printf("\nPREORDER:\n");
preorder(root);
break;
case 5:
printf("\nEnter the element to search:");
scanf("%d",&c);
search(root,c);
if(f==0)
printf("\nElement not in tree");
break;
default:
printf("\nExit");
x=0;
}
}
return 0;
}

Balaji N , GCE Bodi


44 | P a g e

Implementation of AVL Tree

#include<malloc.h>
#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 *);
void postorder(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:");

Balaji N , GCE Bodi


45 | P a g e

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\nPostorder sequence:\n");
postorder(root);
printf("\n");
break;
}
}while(op!=5);

return 0;

Balaji N , GCE Bodi


46 | P a g e

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)

Balaji N , GCE Bodi


47 | P a g e

{
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;

Balaji N , GCE Bodi


48 | P a g e

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)

Balaji N , GCE Bodi


49 | P a g e

{
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)


{
T=rotateright(T);
return(T);
}

node * LR(node *T)


{
T->left=rotateleft(T->left);
T=rotateright(T);

Balaji N , GCE Bodi


50 | P a g e

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);
}

Balaji N , GCE Bodi


51 | P a g e

void inorder(node *T)


{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d)",T->data,BF(T));
inorder(T->right);
}
}

void postorder(node *T)


{
if(T!=NULL)
{
postorder(T->left);
postorder(T->right);
printf("%d(Bf=%d)",T->data,BF(T));
}
}

Balaji N , GCE Bodi


52 | P a g e

Implementation Of Heap
# include <stdio.h>
int arr[100],n;
void search(int x)
{
int i,flag=0;
if(n==0)
{

printf("Heap is empty\n");
return;
}
for(i=0;i<n;i++)
{
if(arr[i]==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("\nElement found in heap\n");
else
printf("\nElement not found in heap\n");
}
void display()
{ int i;
if(n==0)
{
printf("Heap is empty\n");
return;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
void insert(int num,int loc)
{

Balaji N , GCE Bodi


53 | P a g e

int par;
while(loc>0)
{
par=(loc-1)/2;
if(num<=arr[par])
{
arr[loc]=num;
return;
}
arr[loc]=arr[par];
loc=par;
}
arr[0]=num;
}
void del(int num)
{
int left,right,i,temp,par;

for(i=0;i<n;i++)
{
if(num==arr[i])
break;
}
if( num!=arr[i] )
{
printf("%d not found in heap\n",num);
return;
}
arr[i]=arr[n-1];
n=n-1;
par=(i-1)/2;
if(arr[i] > arr[par])
{
insert( arr[i],i);
return;
}
left=2*i+1;
right=2*i+2;

Balaji N , GCE Bodi


54 | P a g e

while(right < n)
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )
return;
if( arr[right]<=arr[left] )
{
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}
if( left==n-1 && arr[i]<arr[left] )
{ temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
}
}
void main()
{
int choice,num,x;
n=0;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Search\n");
printf("5.Quit\n");

Balaji N , GCE Bodi


55 | P a g e

printf("Enter your choice : ");


scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num,n);
n=n+1;
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
display();
break;
case 4:
printf("\nEnter the number to search:");
scanf("%d",&x);
search(x);
break;
case 5:
exit(0);
break;
default:
printf("Wrong choice\n");
}
}
}

Balaji N , GCE Bodi


56 | P a g e

Graph Representation and Traversal Algorithm

Graph Representation:

#include <stdio.h>
#include <stdlib.h>
int adj_mat[50][50],n;
void main()
{
int option,i,j;
do
{
printf("\n A Program to represent a Graph by using an ");
printf("Adjacency Matrix method \n ");
printf("\n 1. Directed Graph ");
printf("\n 2. Un-Directed Graph ");
printf("\n 3. Exit ");
printf("\n\n Select a proper option : ");
scanf("%d", &option);
switch(option)
{
case 1 : dir_graph();
printf("\nAdjacancy Matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d\t",adj_mat[i][j]);
}
printf("\n");
}
break;
case 2 : undir_graph();
printf("\nAdjacancy Matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

Balaji N , GCE Bodi


57 | P a g e

printf("%d\t",adj_mat[i][j]);
}
printf("\n"); }
break;
case 3 : exit(0);
} // switch
}while(1);
}

int dir_graph()
{

int in_deg, out_deg, i, j;


printf("\n How Many Vertices ? : ");
scanf("%d", &n);
read_graph(adj_mat, n);
printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
for (i = 1; i <= n ; i++ )
{
in_deg = out_deg = 0;
for ( j = 1 ; j <= n ; j++ )
{
if ( adj_mat[j][i] == 1 )
in_deg++;
}
for ( j = 1 ; j <= n ; j++ )
if (adj_mat[i][j] == 1 )
out_deg++;
printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
}
return;
}

int undir_graph()
{
int deg, i, j;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);

Balaji N , GCE Bodi


58 | P a g e

read_graph(adj_mat, n);
printf("\n Vertex \t Degree ");
for ( i = 1 ; i <= n ; i++ )
{
deg = 0;
for ( j = 1 ; j <= n ; j++ )
if ( adj_mat[i][j] == 1)
deg++;
printf("\n\n %5d \t\t %d\n\n", i, deg);
}
return;
}

int read_graph ( int adj_mat[50][50], int n )


{
int i, j;
int reply;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i == j )
{
adj_mat[i][j] = 0;
}
else
{
printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j);
scanf("%d",&reply);
if ( reply == 1)
adj_mat[i][j] = 1;
else if(reply == 0)
adj_mat[i][j] = 0;
}
}
}
return;
}

Balaji N , GCE Bodi


59 | P a g e

Traversal Algorithm:

#include<stdio.h>
int q[20],top=-1,front=-1,rear=-1,a[20][20],vis[20],stack[20];
int delete();
void add(int item);
void bfs(int s,int n);
void dfs(int s,int n);
void push(int item);
int pop();
void main()
{
int n,i,s,ch,j;
char c,dummy;
printf("ENTER THE NUMBER VERTICES ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("ENTER 1 IF %d HAS A NODE WITH %d ELSE 0 ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("THE ADJACENCY MATRIX IS\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

do
{
for(i=1;i<=n;i++)
vis[i]=0;

Balaji N , GCE Bodi


60 | P a g e

printf("\nMENU");
printf("\n1.B.F.S");
printf("\n2.D.F.S");
printf("\nENTER YOUR CHOICE");
scanf("%d",&ch);
printf("ENTER THE SOURCE VERTEX :");
scanf("%d",&s);

switch(ch)
{
case 1:bfs(s,n);
break;
case 2:
dfs(s,n);
break;
}
printf("DO U WANT TO CONTINUE(Y/N) ? ");
scanf("%c",&dummy);
scanf("%c",&c);
}while((c=='y')||(c=='Y'));
}
void bfs(int s,int n)
{
int p,i;
add(s);
vis[s]=1;
p=delete();
if(p!=0)
printf(" %d",p);
while(p!=0)
{
for(i=1;i<=n;i++)
if((a[p][i]!=0)&&(vis[i]==0))
{
add(i);
vis[i]=1;
}
p=delete();

Balaji N , GCE Bodi


61 | P a g e

if(p!=0)
printf(" %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}

void add(int item)


{
if(rear==19)
printf("QUEUE FULL");
else
{
if(rear==-1)
{
q[++rear]=item;
front++;
}
else
q[++rear]=item;
}
}
int delete()
{
int k;
if((front>rear)||(front==-1))
return(0);
else
{
k=q[front++];
return(k);
}
}
void dfs(int s,int n)
{
int i,k;
push(s);

Balaji N , GCE Bodi


62 | P a g e

vis[s]=1;
k=pop();
if(k!=0)
printf(" %d ",k);
while(k!=0)
{
for(i=1;i<=n;i++)
if((a[k][i]!=0)&&(vis[i]==0))
{
push(i);
vis[i]=1;
}
k=pop();
if(k!=0)
printf(" %d ",k);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
dfs(i,n);
}
void push(int item)
{
if(top==19)
printf("Stack overflow ");
else
stack[++top]=item;
}
int pop()
{
int k;
if(top==-1)
return(0);
else
{
k=stack[top--];
return(k);
}
}

Balaji N , GCE Bodi


63 | P a g e

Application Of Graph(Friends Circle)


#include <stdio.h>
#include <stdlib.h>
int adj_mat[50][50];
int list();
void check();
int n;
void main()
{
int option,i,j;
do
{
printf("\nApplication of Graph\nFriends circle\n ");
printf("\n1.Link friends ");
printf("\n2.Friend List ");
printf("\n3.Check friends\n4.Friend\n5.Unfriend");
printf("\n6.Exit");
printf("\n\n Select a proper option : ");
scanf("%d", &option);
switch(option)
{
case 1 : link();
break;
case 2 : list();
break;
case 3: check();
break;
case 4: friends();
break;
case 5: unfriend();
break;
case 6: exit(0);
} // switch
}while(1);
}
int friends()
{

Balaji N , GCE Bodi


64 | P a g e

int x,y;
printf("Enter 1st user id:");
scanf("%d",&x);
printf("\nEnter 2nd user id:");
scanf("%d",&y);
if(adj_mat[x][y]==1)
printf("\nThey are already friends");
else
adj_mat[x][y]=1;
return;
}
int unfriend()
{
int x,y;
printf("Enter 1st user id:");
scanf("%d",&x);
printf("\nEnter 2nd user id:");
scanf("%d",&y);
if(adj_mat[x][y]==0)
printf("\nThey are already not now a friends");
else
adj_mat[x][y]=0;
return;
}
int link()
{
int deg, i, j;
printf("\n Enter total number of friends: ");
scanf("%d", &n);
read_graph(adj_mat, n);
return;
}
int list()
{
int i,deg,j;
printf("\n User \t Total number of friends ");
for ( i = 1 ; i <= n ; i++ )
{

Balaji N , GCE Bodi


65 | P a g e

deg = 0;
for ( j = 1 ; j <= n ; j++ )
if ( adj_mat[i][j] == 1)
deg++;
printf("\n\n %d \t\t %d\n\n", i, deg);
}
return;
}
void check()
{
int x,y;
printf("\nEnter the 1st friend id:");
scanf("%d",&x);
printf("\nEnter the 2nd friend id:");
scanf("%d",&y);
if(x==y)
printf("\nBoth are same user");
else
{
if(adj_mat[x][y]==1)
printf("\nUser %d and %d are friends\n",x,y);
else
printf("\nUser %d and %d are not now friends\n",x,y);
}
}
int read_graph ( int adj_mat[50][50], int n )
{
int i, j;
int reply;
for ( i = 1 ; i <= n ; i++ )
{
for ( j = 1 ; j <= n ; j++ )
{
if ( i == j )
{
adj_mat[i][j] = 0;
}
else

Balaji N , GCE Bodi


66 | P a g e

{
if(i<j)
{
printf("\n Is user %d and %d are friends(1 for yes and 0 for no) :",i,j);
scanf("%d",&reply);
if ( reply == 1)
{
adj_mat[i][j] = 1;
adj_mat[j][i]=1;
}
else if(reply == 0)
{
adj_mat[i][j] = 0;
adj_mat[j][i]=0;
}
}
}
}
}
return;
}

Balaji N , GCE Bodi


67 | P a g e

Searching and Sorting Algorithm

Implementation of Searching Algorithm:

#include<stdio.h>
#include<conio.h>
void linear(int array[],int x,int n)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(array[i]==x)
{
printf("Element %d is found in the given array.\nPosition number %d",x,i+1);
flag=1;
break;
}
}
if(flag==0)
printf("\nElement not found in array");

}
void binary(int array[],int search,int n)
{
int first = 0;
int last = n - 1,middle;
middle = (first+last)/2;

while (first <= last)


{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else

Balaji N , GCE Bodi


68 | P a g e

last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
}
void main()
{
int n,i,array[50],ch,x,j,t;
printf("\nEnter the number of terms:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
printf("\nSearching Algorithm:\nLinear search\n2.Binary Search\n3.Exit\nEnter your
choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nLinear search:\nEnter the number to search:");
scanf("%d",&x);
linear(array,x,n);
break;
case 2:
printf("\nBinary search:\nEnter the number to search:");
scanf("%d",&x);
for(i=1;i<=n-1;i++)
{
j=i;
while(j>0&&array[j-1]>array[j])
{
t=array[j];
array[j]=array[j-1];
array[j-1]=t;
j--;
}
}

Balaji N , GCE Bodi


69 | P a g e

binary(array,x,n);
break;
default:
printf("\nExit");
}
getch();
}

Implementation of Sorting Algorithm:

#include<stdio.h>
#include<conio.h>
void display(int[],int);
void selection_sort(int array[],int n)
{
int i,position,j,swap;
for ( i=0;i<(n-1);i++)
{
position = i;

for (j=i+1;j<n;j++)
{
if (array[position] > array[j] )
position = j;
}
if(position!=i)
{
swap = array[i];
array[i] = array[position];
array[position] = swap;
}
}
display(array,n);
}
void bubble_sort(int list[],int n)
{
long c, d,i,t;
for (c = 0 ; c < n - 1; c++)

Balaji N , GCE Bodi


70 | P a g e

{
for (d=0;d<n-c-1;d++)
{
if (list[d] > list[d+1])
{

t= list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
display(list,n);
}
void insertion_sort(int array[],int n)
{
int i,j,t;
for(i=1;i<=n-1;i++)
{
j=i;
while(j>0&&array[j-1]>array[j])
{
t=array[j];
array[j]=array[j-1];
array[j-1]=t;
j--;
}
}
display(array,n);
}
void ShellSort(int array[],int n)
{
int i, j, k, tmp;
for(i=n/2;i>0;i/=2)
{
for(j=i;j<n;j++)
{
tmp = array[j];

Balaji N , GCE Bodi


71 | P a g e

for(k=j;k>=i;k-=i)
{
if(tmp < array[k-i])
array[k] = array[k-i];
else
break;
}
array[k] = tmp;
}
}
display(array,n);
}
void radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;

div=1;
num=0;
large=arr[0];

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


{
if(arr[i] > large)
{
large = arr[i];
}

while(large > 0)
{
num++;
large = large/10;
}

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


{
for(k=0 ; k<10 ; k++)
{

Balaji N , GCE Bodi


72 | P a g e

buck[k] = 0;
}
for(i=0 ; i<n ;i++)
{
l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}

i=0;
for(k=0 ; k<10 ; k++)
{
for(j=0 ; j<buck[k] ; j++)
{
arr[i++] = bucket[k][j];
}
}
div*=10;
}
}
display(arr,n);
}
void display(int array[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",array[i]);
}
}
void main()
{
int array[50],i,j,k,n,ch,c;
clrscr();
printf("\nRead the unsorted array:\nEnter number of terms:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);

Balaji N , GCE Bodi


73 | P a g e

}
printf("\nSorting Algorithm\n1.Bubble Sort\n2.Selection Sort\n3.Insertion sort\n4.Radix
Sort\n5.Shell Sort\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nBubble Sort");
bubble_sort(array,n);
break;
case 2:
printf("\nSelection sort:");
selection_sort(array,n);
break;
case 3:
printf("\nInsertion Sort:");
insertion_sort(array,n);
break;
case 4:
printf("Radix Sort:\nAvoid negative sign in radix sort.\nRadix sort will take the
bit operation\nDo you want to continue(1 or 0):");
scanf("%d",&c);
if(c==1)
radix_sort(array,n);
else
printf("\nThank you");
break;
case 5:
printf("\nShell Sort:");
ShellSort(array,n);
break;
default:
printf("\nExit");

}
getch();

Balaji N , GCE Bodi


74 | P a g e

Hashing Technique- any two collision technique

Technique 1 (Linear Probing):

#include<stdio.h>
#include<conio.h>
int main()
{
int max;
int array[100],i,n,x,ch,a=0;
printf("\nEnter Total number of elements:");
scanf("%d",&max);
while(1)
{
printf("\nLinear Probing:");
printf("\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(a<max)
{
printf("\nEnter the number to insert:");
scanf("%d",&n);
if(array[n%max]==NULL)
{
array[n%max]=n;
printf("\nInserted");
}
else
{
x=n%max;
while(array[x]!=NULL)
{
x=x+1;
if(x==max)
x=0;

Balaji N , GCE Bodi


75 | P a g e

}
array[x]=n;
printf("\nInserted");
}
a++;
}
else
printf("Hash Table Full");
break;
case 2:
printf("\nEnter the number to delete:");
scanf("%d",&n);
if(n==array[n%max])
array[n%max]=NULL;
else
{
x=n%max;
if(x==max-1)
x=-1;
for(i=x+1;i<max;i++)
{
if(array[i]==n)
{
array[i]=NULL;
printf("\nElement deleted");
break;
}
else if(i==max-1)
{
i=0;
continue;
}
else if(i==x)
{
printf("\nElement not found");
break;
}
}

Balaji N , GCE Bodi


76 | P a g e

}
a--;
break;
case 3:
printf("\nEnter the number to search:");
scanf("%d",&n);
if(n==array[n%max])
printf("\nElement found");
else
{
x=n%max;
if(x==max-1)
x=-1;
for(i=x+1;i<max;i++)
{
if(array[i]==n)
{
printf("\nElement found");
break;
}
else if(i==max-1)
{
i=0;
continue;
}
else if(i==x)
{
printf("\nElement not found");
break;
}
}
}
break;
case 4:
printf("\n----------------\n Hash Table \n----------------\n");
for(i=0;i<max;i++)
{
printf("array[%d]:%d\n",i,array[i]);

Balaji N , GCE Bodi


77 | P a g e

}
break;
default:
exit(0);
break;
}
}
return 0;

Hashing Technique II:

Separate Chaining:

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int size,flag;
struct node *head[100]={NULL},*c,*p,*ptr;
void insert(int i,int val)
{
struct node * newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=val;
newnode->next = NULL;
if(head[i] == NULL)
head[i] = newnode;
else
{
c=head[i];
while(c->next != NULL)
c=c->next;
c->next=newnode;

Balaji N , GCE Bodi


78 | P a g e

}
}
void display(int i)
{
if(head[i] == NULL)
{
printf("No Hash Entry");
return;
}
else
{
for(c=head[i];c!=NULL;c=c->next)
printf("%d->",c->data);
}
}
void search(int i,int x)
{
if(head[i] == NULL)
{
printf("No Hash Entry");
return;
}
else
{
for(c=head[i];c!=NULL;c=c->next)
{
if(c->data==x)
{
flag=1;
break;
}
}
}
if(flag==1)
printf("\nElement found");
else
printf("\nElement not found");
}

Balaji N , GCE Bodi


79 | P a g e

void delet(int i,int x)


{
if(head[i] == NULL)
{
printf("No Hash Entry");
return;
}
else
{
if(head[i]->data==x)
head[i]=head[i]->next;
else
{
ptr=head[i];
for(ptr=head[i];ptr!=NULL;ptr=ptr->next)
{
if(ptr->next->data==x)
{
ptr->next=ptr->next->next;
break;
}
}
}
}
return;
}
int main()
{
int opt,val,i;
printf("\nEnter total number of element:");
scanf("%d",&size);
while(1)
{
printf("\nSeperate Chaining\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\nEnter
your choice:");
scanf("%d",&opt);
switch(opt)
{

Balaji N , GCE Bodi


80 | P a g e

case 1:
printf("\nenter a value to insert into hash table\n");
scanf("%d",&val);
i=val%size;
insert(i,val);
break;
case 2:
for(i=0;i<size;i++)
{
printf("\nentries at index %d\n",i);
display(i);
}
break;
case 3:
printf("\nenter a value to delete into hash table\n");
scanf("%d",&val);
i=val%size;
delet(i,val);
break;
case 4:
printf("\nenter a value to search into hash table\n");
scanf("%d",&val);
i=val%size;
search(i,val);
break;
case 5:
exit(0);
}
}
return 0;
}

Balaji N , GCE Bodi

Anda mungkin juga menyukai