DATA STRUCTURE
CS8391
Content
8 Implementation of Heap
10 Application of Graph
#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(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++)
{
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::");
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]);
}
}
#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 *);
/*
* Main :contains menu
*/
int main()
{
int ch;
char ans = 'Y';
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;
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)
{
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;
/*
* Inserting Node at Last
*/
void insert_node_last()
{
/* ds
* Inserting Node at position
*/
void insert_node_pos()
{
int pos, val, cnt = 0, i;
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
{
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;
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;
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;
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");
}
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);
}
}
}/
#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)
getch();
}
#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;
}
}
{
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);
case 3:
display();
break;
default:
printf("\nExit");
x=0;
}
}
getch();
}
#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[])
{
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);
break;
default:
printf("\nExit");
x=0;
}
}
getch();
}
#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;
}
}
}
void display()
{
if (rear==NULL || front==NULL)
getch();
}
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++;
}
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++;
}
}
}
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
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]);
}
break;
case 2:
dequeue(lst,queue);
break;
default:
printf("\nExit");
x=0;
}
}
getch();
}
#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);
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:
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();
}
#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;
return root;
}
node *delet(node *root,int x)
{
if(root!=NULL)
{
if(root->data==x)
{
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);
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)
{
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)
{
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);
#include<malloc.h>
#include<stdio.h>
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:");
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;
return 0;
T->ht=height(T);
return(T);
}
{
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
{
while(p->left!= NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
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 *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
return(y);
}
return(T);
}
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);
}
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)
{
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;
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");
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++)
{
printf("%d\t",adj_mat[i][j]);
}
printf("\n"); }
break;
case 3 : exit(0);
} // switch
}while(1);
}
int dir_graph()
{
int undir_graph()
{
int deg, i, j;
printf("\n How Many Vertices ? : ");
scanf("%d", &n);
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;
}
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;
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();
if(p!=0)
printf(" %d ",p);
}
for(i=1;i<=n;i++)
if(vis[i]==0)
bfs(i,n);
}
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);
}
}
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++ )
{
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
{
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;
}
#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;
last = middle - 1;
binary(array,x,n);
break;
default:
printf("\nExit");
}
getch();
}
#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++)
{
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];
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];
while(large > 0)
{
num++;
large = large/10;
}
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]);
}
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();
#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;
}
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;
}
}
}
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]);
}
break;
default:
exit(0);
break;
}
}
return 0;
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;
}
}
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");
}
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;
}