Sei sulla pagina 1di 26

1.

Array

#include<stdio.h>
#include<stdlib.h>
int a[20];
int n,val,i,pos;
//creating an array
void create()
{
printf("Enter the size of the array elements:");
scanf("%d",&n);
printf("Enter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
//displaying an array elements
void display()
{
int i;
printf("The array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
//inserting an element into an array
void insert()
{
printf("Enter the position for the new element: ");
scanf("%d",&pos);
printf("Enter the element to be inserted : ");
scanf("%d",&val);
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=val;
n=n+1;
}
//deleting an array element
void delete()
{
printf("Enter the position of the element to be deleted: ");
scanf("%d",&pos);
val=a[pos-1];
for(i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("The deleted element is =%d\n",val);
}
int main()
{
printf("MENU:\n");
printf("1.CREATE \t");
printf("2.DISPLAY\n");
printf("3.INSERT \t");
printf("4.DELETE\n");
printf("5.EXIT\n");
int c=0;
while(1)
{
printf("ENTER YOUR CHOICE: ");
scanf("%d",&c);
switch(c)
{
case 1: create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
delete();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return;
}
2. Pattern Matching

#include<stdio.h>
void main()
{
char STR[100],PAT[100],REP[100],ans[100];
int i,j,c,m,k,flag=0;
printf("Enter the MAIN string: ");
gets(STR);
printf("Enter a PATTERN string: ");
gets(PAT);
printf("Enter a REPLACE string: ");
gets(REP);
i = m = c = j = 0;
while ( STR[c] != '\0')
{
// Checking for Match
if ( STR[m] == PAT[i] )
{
i++;
m++;
flag=1;
if ( PAT[i] == '\0')
{
//copy replace string in ans string
for(k=0; REP[k] != '\0';k++,j++)
ans[j] = REP[k];
i=0;
c=m;
}
}
else //mismatch
{
ans[j] = STR[c];
j++;
c++;
m = c;
i=0;
}
}
if(flag==0)
printf("Pattern not found!\n");
else
{
ans[j] = '\0';
printf("The RESULTANT string is:%s\n" ,ans);
}
}

3. Stack (Palindrome)

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1;
void push() //Inserting element into the stack
{
int item,n;
if(top==(max_size-1))
printf("\nStack Overflow:");
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;

if(top==-1)
printf("Stack Underflow:");
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void pali()
{
int digit,j,k,len=top+1,flag=0,ind=0;
int num[len],rev[len],i=0;
while(top!=-1)
{
digit= stack[top];
num[i]=digit;
top--;
i++;
}
for(j=0;j<len;j++)
printf("Numbers= %d\n",num[j]);
printf("reverse operation : \n");
for(k=len-1;k>=0;k--)
{
rev[k]=num[ind];
ind++;
}
printf("reverse array : ");
for(k=0;k<len;k++)
printf("%d\n",rev[k]);

printf("check for palindrome :\n");


int length = 0;
for(i=0;i<len;i++)
if(num[i]==rev[i])
length = length+1;
if(length==len)
{
printf("It is palindrome number\n");
}
else
{
printf("It is not a palindrome number\n");
}
top = len-1;
}
void display()
{
int i;
if(top==-1)
printf("\nStack is Empty:");
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}
int main()
{
int choice;
while(1)
{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("\nInvalid choice:\n");
break;
}
}
return;
}
4. Infix to Postfix

#define SIZE 50
#include <ctype.h>
#include <stdio.h>
char s[SIZE];
int top = -1;
push(char elem)
{
s[++top] = elem;
}
char pop()
{
return (s[top--]);
}
int pr(char elem)
{
switch (elem)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
}
}
void main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\nInfix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop();
}
else
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0';
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}

5a. suffix expression evaluation

#include <stdio.h>
#include<math.h>
#include<string.h>
#include <ctype.h>
#include<stdlib.h>

double compute (char symbol, double op1, double op2)


{
double res;
switch (symbol )
{
case '+' : return op1 + op2;
case '-' : return op1 - op2;
case '*' : return op1 * op2;
case '/' : return op1 / op2;
case '^' : return (pow(op1,op2));
default : printf("Invalid operator!\n");
exit(0);
}
}

void main ()
{
double s[20], res, op1, op2;
int top = -1, i;
char postfix[20], symbol;
printf ("Enter postfix expression :");
scanf ("%s", postfix);
printf ("The expression is :%s\n", postfix);
for (i = 0; i < strlen (postfix); i++)
{
symbol = postfix [i];
if (isdigit (symbol))
s[++top] = symbol-'0';
else
{
op2 = s[top--];
op1 = s[top--];
res = compute (symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf ("The result is %.2f\n", res);
}

5b. Tower of Hanoi

#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("Move disk 1 from peg %c to peg %c\n", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("Move disk %d from peg %c to peg %c\n", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

6. Circular Queue

#include<stdio.h>
#include <stdlib.h>
#define SIZE 4
void InsertQ (int q[], int *count, int *r, int item)
{
if (*count == SIZE)
{
printf ("Queue Overflow \n");
return;
}
*r = (*r+1) % SIZE;
q[*r] = item;
(*count)++;
}
void DeleteQ (int q[], int *count, int *f, int *r)
{
if (*count == 0)
{
printf ("Empty Queue\n");
*f = 0, *r = -1;
return;
}
printf ("Item deleted is %d\n", q[*f]);
*f = (*f+1)% SIZE;
(*count)--;
return;
}
void Display(int q[], int *f, int *r, int *count)
{
int i;
if ( *count == 0){
printf ("Queue is Empty\n");
}
printf ("The contents of the Queue is : ");
for (i = *f; i<= *r; i++)
printf ("%d\n", q[i]);
}
void main()
{
int q[SIZE], item, option;
int f=0, r= -1, count=0;
printf("Menu:\n");
while (1)
{
printf ("1: Insert\n2: Delete\n3: Display\n4: Exit \n");
printf ("Enter option :");
scanf ("%d", &option);
switch (option)
{
case 1:
printf ("Enter item to be inserted :");
scanf ("%d", &item);
InsertQ (q, &count, &r, item);
if (count)
Display (q, &f, &r, &count);
break;
case 2:
DeleteQ (q, &count, &f, &r);
if (count)
Display (q, &f, &r, &count);
break;
case 3: Display (q, &f, &r, &count);
break;
default:
exit (0);
}
}
}

7. Singly Linked List

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
int sem,phno;
char name[20],branch[10],usn[20];
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL, *temp1;
void create()
{
int sem,phno;
char name[20],branch[10],usn[20];
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter usn,name, branch, sem, phno of student : ");
scanf("%s%s%s%d%d",usn,name,branch,&sem,&phno);
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem = sem;
temp->phno = phno;
temp->next=NULL;
count++;
}
void finsert()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first = temp;
}
}
void einsert()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last->next = temp;
last = temp;
}
}
void display()
{
temp1=first;
if(temp1 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\nLinked list elements from begining : \n");
while (temp1!= NULL)
{
printf("%s %s %s %d %d\n",temp1->usn,temp1->name,temp1->branch,temp1-
>sem,temp1->phno);
temp1 = temp1->next;
}
printf("No of students = %d ", count);
}
int edelete()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
}
else
{
while(temp->next!=last)
temp=temp->next;
printf("%s %s %s %d %d\n", last->usn, last->name,last->branch,
last->sem, last->phno );
free(last);
temp->next=NULL;
last=temp;
}
count--;
return 0;
}
int fdelete()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
else
{
first=temp->next;
printf("%s %s %s %d %d\n", temp->usn, temp->name,temp->branch,temp->sem,
temp->phno );
free(temp);
}
count--;
return 0;
}
int main()
{
int ch,n,i;
first=NULL;
temp = temp1 = NULL;
printf("\n 1 - Create a SLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - Delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - Delete at beg");
printf("\n 7 - Exit\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
finsert();
break;
case 2:
display();
break;
case 3:
einsert();
break;
case 4:
edelete();
break;
case 5:
finsert();
break;
case 6:
edelete();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
return;
}

8. Doubly Linked List

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int count=0;
struct node
{
struct node *prev;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void create()
{
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &ssn, name,dept,desg,&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void insertbeg()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insertend()
{
if(h==NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void displaybeg()
{
temp2 =h;
if(temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp2!= NULL)
{
printf("%d %s %s %s %f %d\n", temp2->ssn, temp2->name,temp2->dept,temp2->desg,temp2-
>sal, temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
return 0;
}
else
{
temp2=temp1->prev;
temp2->next=NULL;
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,
temp1->desg,temp1->sal, temp1->phno );
free(temp1);
}
count--;
return 0;
}
int deletebeg()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
}
else
{
h=h->next;
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept,
temp->desg,temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
h=NULL;
temp = temp1 = NULL;
printf("MENU:");
printf("\n 1 - create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}
}

9. Singly Circular Linked List (Polynomial Evaluation)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
float cf;
int px,py,pz;
int flag;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE) malloc (sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}
NODE insert_rear(float cf,float x , float y, float z,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->cf=cf;
temp->px=x;
temp->py=y;
temp->pz=z;
temp->flag=0;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("polynominal doesn't exsits\n");
return;
}
temp=head->link;
while(temp!=head)
{
printf("+ % 5.2fx^%dy^%dz^%d",temp->cf,temp->px,temp->py,temp->pz);
temp=temp->link;
}
printf("\n");
}
NODE add_poly(NODE h1,NODE h2, NODE h3)
{
NODE p1,p2;
int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;
p1=h1->link;
while(p1!=h1)
{
x1=p1->px;
y1=p1->py;
z1=p1->pz;
cf1=p1->cf;
p2=h2->link;
while(p2!=h2)
{
x2=p2->px;
y2=p2->py;
z2=p2->pz;
cf2=p2->cf;
if(x1==x2 && y1==y2 && z1==z2) break;
p2=p2->link;
}
if(p2!=h2)
{
cf=cf1+cf2;
p2->flag=1;
if(cf!=0)
h3=insert_rear(cf,x1,y1,z1,h3);
}
else
h3=insert_rear(cf1,x1,y1,z1,h3);
p1=p1->link;
}
p2=h2->link;
while(p2!=h2)
{
if(p2->flag==0)
h3=insert_rear(p2->cf,p2->px,p2->py,p2->pz,h3);
p2=p2->link;
}
return h3;
}
NODE read_poly(NODE head)
{
int i;
int px,py,pz;
float cf;
printf("enter the coeffecient as -999 to end the polynominal\n");
for(i=1;;i++)
{
printf("enter the %d term\n",i);
printf("coeff=");
scanf("%f",&cf);
if(cf==-999) break;
printf("pow x=");
scanf("%d",&px);
printf("pow y=");
scanf("%d",&py);
printf("pow z=");
scanf("%d",&pz);
head=insert_rear(cf,px,py,pz,head);
}
return head;
}
void polysum()
{
NODE h1,h2,h3;
h1=getnode();
h2=getnode();
h3=getnode();
h1->link=h1;
h2->link=h2;
h3->link=h3;
printf("enter the first polynominal\n");
h1=read_poly(h1);
printf("enter the second polynominal\n");
h2=read_poly(h2);
h3=add_poly(h1,h2,h3);
printf(" the first polynominal is\n");
display(h1);
printf("second polynominal is\n");
display(h2);
printf("the sum of two polynominal is\n");display(h3);
}
void represent_evaluate()
{
NODE e1,temp;
int x,y,z;
float sum=0.0;
e1=getnode();
e1->link=e1;
printf("enter the polynominal\n");
e1=read_poly(e1);
printf("polynominal i s \n");
display(e1);
printf("enter the values of coefficient\n");
scanf("%d%d%d",&x,&y,&z);
if(e1==NULL)
{
printf("list is empty");
}
else
{
temp=e1->link;
while(temp!=e1)
{
sum+=temp->cf*pow(x,temp->px)*pow(y,temp->py)*pow(z,temp->pz);
temp=temp->link;
}
sum+=temp->cf*pow(x,temp->px)*pow(y,temp->py)*pow(z,temp->pz);
printf("the total sum is %f\n",sum);
}
return;
}
void main()
{
int choice;
while(1)
{
printf("\n\n\n\t1.represent and evaluate...\t2.ADD TWO poly..\t3.Exit...");
printf("\n\n\n\tEnter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: represent_evaluate();break;
case 2:polysum();break;
case 3:exit(0);
default: printf("\n\n\n\tEnter proper Choice....");
}
}
}

10. Binary Search Tree

# include <stdio.h>
# include <stdlib.h>
struct BST
{
int item;
struct BST *llink, *rlink;
};
typedef struct BST* NODE;
/* This function is for creating a binary search tree */
NODE insert(NODE root)
{
NODE temp, cur, prev;
int item;
printf("\nEnter The Element : ");
scanf("%d", &item);
temp = (NODE) malloc(sizeof(struct BST));
temp->llink = NULL;
temp->rlink = NULL;
temp->item = item;
if (root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if (item < cur-> item)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item < prev->item)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
/* This function displays the tree in inorder fashion */
void inorder(NODE root)
{
if (root != NULL)
{
inorder(root->llink);
printf("\t%d", root->item);
inorder(root->rlink);
}
}
/* This function displays the tree in preorder fashion */
void preorder(NODE root)
{
if (root != NULL)
{
printf("%d\t", root->item);
preorder(root->llink);
preorder(root->rlink);
}
}
/* This function displays the tree in postorder fashion */
void postorder (NODE root)
{
if (root != NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t", root->item);
}
}
NODE search (NODE root, int key)
{
NODE cur;
if(root == NULL)
return NULL;
cur = root;
while(cur != NULL)
{
if (key == cur->item)
return cur;
if (key < cur->item)
cur = cur->llink;
else
cur = cur->rlink;
}
return NULL;
}
NODE Delete (NODE root, int item)
{
NODE cur, parent= NULL, suc,q;
if(root == NULL)
{
printf("Tree is empty,Element does not exist\n");
return root;
}
cur=root;
while(cur!=NULL)
{
if(item==cur->item) break;
parent=cur;
cur=(item<cur->item)?cur->llink:cur->rlink;
}
if(cur==NULL)
{
printf("Item not found\n");
return root;
}
if(cur->llink ==NULL)
q=cur->rlink;
else if(cur->rlink==NULL)
q=cur->llink;
else
{
suc = cur->rlink;
while(suc->llink != NULL)
suc = suc -> llink;
suc->llink = cur->llink;
q = cur->rlink;
}
if(parent == NULL)
return q;
if(cur == parent->llink)
parent->llink = q;
else
parent->rlink = q;
printf ("The deleted item is : %d", cur->item);
free(cur);
return root;
}
void main()
{
int choice, key;
NODE root = NULL, tmp, parent;
printf("Menu: \n")
printf("1.Create\n");
printf("2.Traverse the Tree in Preorder, Inorder, Postorder\n");
printf("3.Search\n");
printf("4.Delete an element from the Tree\n");
printf("5.Exit\n");
while(1)
{
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
root = insert(root);
break;
case 2:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key);if(tmp == NULL)
printf("Element does not exist\n");
else
printf("\nThe element %d found", tmp->item);
break;
case 4: printf("\nEnter Element to be deleted : ");
scanf("%d", &key);
root = Delete(root, key);
break;
default: exit(0);
}
}
}

11. Graph

#include<stdio.h>
void bfs(int n,int a[10][10],int src)
{
int i,s[10],q[10],front,rear,u,v;
for(i=0;i<n;i++)
s[i]=0;
s[src]=1;
front=0;
rear=-1;
q[++rear]=src;
while(front<=rear)
{
u=q[front++];
for(v=0;v<n;v++)
{
if(a[u][v]==1&&s[v]==0)
{
s[v]=1;
q[++rear]=v;
}
}
}
for(i=0;i<n;i++)
{
if(s[i]==0)
printf("%d is not reachable \n",i);
else
printf("%d is reachable\n",i);
}
}
void dfs(int n,int a[10][10],int u,int s[10])
{
int v;
s[u]=1;
for(v=0;v<n;v++){
if(a[u][v]==1 && s[v]==0)
dfs(n,a,v,s);
}
}
void main()
{
int n,i,j,src,a[10][10], flag, s[10], source, connected;
printf("Enter the no. of nodes in graph: ");
scanf("%d",&n);
printf("\nEnter the adj. matrix\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter source :\n ");
scanf("%d",&src);
bfs(n,a,src);
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
s[i]=0;
dfs(n,a,j,s);
flag=0;
for(i=0;i<n;i++)
{
if(s[i]==0)
flag=1;
}
if(flag==0)
connected=1;
}
if(connected==1)
printf("graph connected\n");
else
printf("graph not connected\n");
}

12. Hash Table

#include<stdio.h>
#include<stdlib.h>
#define MAX_ADDR 5
struct employee
{
int emp_id, emp_age;
char emp_name[25];
}
emp[MAX_ADDR];
void main()
{
int i, ch, count = 0, index, haddr, id, flag = 0;
for(;;)
{
printf (" Hash Function \n");
printf ("==============\n");
printf("1: Insert Record \n2: Search Record\n3: Exit\n");
printf ("Enter option : ");
scanf("%d", &ch);
switch(ch)
{
case 1: if(count == MAX_ADDR)
{
printf("No free address space\n");
break;
}
printf("Enter employee id : ");
scanf("%d", &id);
haddr = hash(id);
printf("HAome address is %d\n", haddr);
for(i = 0; i<MAX_ADDR; i++)
{
index = (haddr + i) % MAX_ADDR;
if(emp[index].emp_id == 0)
{
emp[index].emp_id = id;
printf("Enter the employee name: ");
scanf("%s", emp[index].emp_name);
printf("Enter the employee age: ");
scanf("%d", &emp[index].emp_age);
count++;
printf ("Successfully inserted at Actual Address %d\n", index);
break;
}
}
break;
case 2: printf("Enter employee id to be searched: ");
scanf("%d", &id);
haddr = hash(id);
for(i=0; i<MAX_ADDR; i++)
{
index = (haddr+i)%MAX_ADDR;
if(emp[index].emp_id == 0)
{
flag = 1;
break;
}
else if(emp[index].emp_id == id)
{
printf("Employee id is: %d\n", emp[index].emp_id);
printf("Employee name is: %s\n",
emp[index].emp_name);
printf("Employee age is: %d\n",
emp[index].emp_age);
printf("Search Length is: %d\n", ++i);
break;
}
}
if(flag == 1 || i == MAX_ADDR)
{
printf("Key not present\n");
}
break;
default: exit(0);
}
}
}
int hash (int key)
{
return key % MAX_ADDR;
}

Potrebbero piacerti anche