You are on page 1of 32

[Data Structures Laboratory-15CSL38 ]

1. Design, Develop and Implement a menu driven Program in C for the following Array operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Inserting an Element (ELEM) at a given valid Position (POS)
d. Deleting an Element at a given valid Position (POS)
e. Exit.
Support the program with functions for each of the above operations.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[20],n,val,pos,i;
void create();
void display();
void insert();
void del();
void main()
{
int choice;
clrscr();
for(;;)
{
printf("\n.....menu.....\n");
printf("1.create\n 2.display\n");
printf("3.insert\n 4.delete\n");
printf("5.exit\n");
printf("enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:create();break;
case 2:display();break;
case 3:insert();break;
case 4:del();break;
case 5:exit(0);
}
}
}
void create()
{
printf("enter the value of n:");
scanf("%d",&n);
printf("enter element one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
[CSE Department, CIT-Mandya] Page 1
[Data Structures Laboratory-15CSL38 ]

void display()
{
printf("array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void insert()
{
printf("enter the elements to be insert:");
scanf("%d",&val);
printf("enter the position:");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
void del()
{
printf("enter the position to delete:");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
printf("delete elements=%d",val);
n=n-1;
}

[CSE Department, CIT-Mandya] Page 2


[Data Structures Laboratory-15CSL38 ]

2. Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR
with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in
STR.
Support the program with functions for each of the above operations. Don't use Built-in functions.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[100],pat[20],rep[20],ans[100];
int i,j,k,c,m,flag=0;
clrscr();
printf ("enter the main string\n");
gets(str);
printf("enter the pattern string\n");
gets(pat);
printf("enter the replace string\n");
gets(rep);
i=j=k=m=c=0;
while(str[c]!='\0')
{
if (str[m]==pat[i])
{
m++; i++;
if(pat[i]=='\0')
{
flag=1;
for(k=0;rep[k]!='\0';j++,k++)
{
ans[j]=rep[k];
}
c=m; i=0;
}
}
else
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
}
}
if (flag==0)

[CSE Department, CIT-Mandya] Page 3


[Data Structures Laboratory-15CSL38 ]

printf("pattern string not found in main string");


else
{
ans[j]='\0';
printf("resultant string is %s",ans);
}
getch();
}

[CSE Department, CIT-Mandya] Page 4


[Data Structures Laboratory-15CSL38 ]

3. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 4
int s[MAX],top=-1;
void push(int item )
{
if(top==MAX-1)
{
printf("stack overflow\n");
return;
}
top=top+1;
s[top]=item;
}
int pop()
{
if(top==-1)
{
printf("stack underflow\n");
return top;
}
return s[top--];
}
void display()
{
int i;
if(top==-1)
{
printf("stack empty");
return;
}
printf("stack contents are:");
for(i=top;i>=0;i--)
{
printf("%d\t",s[i]);
}
}
[CSE Department, CIT-Mandya] Page 5
[Data Structures Laboratory-15CSL38 ]

voidpallindrome()
{
intd,x,n;
top=-1;
printf("enter the number:");
scanf("%d",&n);
x=n;
while(n!=0)
{
d=n%10;
push(d);
n=n/10;
}
while(x%10==pop()&&top!=-1)
{
x=x/10;
}
if (top==-1)
printf("number is pallindrome\n");
else
printf("number is not a pallindrome");
top=-1;
}
void main()
{
intchoice,item,del;
clrscr();
for(;;)
{
printf("\n________stack operations_________\n");
printf("1.push\n2.pop\n3.display\n4.palindrome\n5.exit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the item to be inserted:");
scanf("%d",&item);
push(item);
break;
case 2:del=pop();
if (del!=-1)
printf("deleted item=%d\n",del); break;
case 3:display();break;
case 4:pallindrome();break;
case 5:exit(0);
}
}

[CSE Department, CIT-Mandya] Page 6


[Data Structures Laboratory-15CSL38 ]

}
4. Design, Develop and Implement a Program in C for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized expressions with
the operators: +, -, *, /, %( Remainder), ^ (Power) and alphanumeric operands.

#include<stdio.h>
#include<conio.h>
char infix[20],postfix[20];
intsp(charsym)
{
switch(sym)
{
case'+':
case'-':return 2;
case'*':
case'/':
case'%':return 4;
case'^':return 5;
case'(':return 0;
case'#':return-1;
default:return 8;
}
}
intip(charsym)
{
switch(sym)
{
case'+':
case'-':return 1;
case'*':
case'/':
case'%':return 3;
case'^':return 6;
case'(':return 9;
case')':return 0;
default:return 7;
}
}
voidinfixtopostfix()
{
char s[20],sym;
int top=-1;
inti,j=0;
s[++top]='#';
for(i=0;infix[i]!='\0';i++)
{
sym=infix[i];

[CSE Department, CIT-Mandya] Page 7


[Data Structures Laboratory-15CSL38 ]

while (sp(s[top])>ip(sym))
postfix[j++]=s[top--];
if(sp(s[top])!=ip(sym))
s[++top]=sym;
else
top--;
}
while (s[top]!='#')
postfix[j++]=s[top--];
postfix[j]='\0';
}
void main()
{
clrscr();
printf("enter the infix expression:");
gets(infix);
infixtopostfix();
printf("input expression=%s",infix);
printf("postfix expression=%s",postfix);
getch();
}

[CSE Department, CIT-Mandya] Page 8


[Data Structures Laboratory-15CSL38 ]

5. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int s[20],top=-1;
void push(int item)
{
s[++top]=item;
}
int pop()
{
return s[top--];
}
int evaluate(char postfix[])
{
int i,x,op1,op2,res;
char sym;
for(i=0;postfix[i]!='\0';i++)
{
sym=postfix[i];
if(isalpha(sym))
{
printf("enter the value for %c",sym);
scanf("%d",&x);
push(x);
}
else if(isdigit(sym))
{
push(sym-'0');
}
else
{
op2=pop();
op1=pop();
switch(sym)
{
case'+': res=op1+op2;break;
case'-': res=op1-op2;break;
case'*': res=op1*op2;break;
case'/': res=op1/op2;break;
case'%': res=op1%op2;break;
case'^': res=pow(op1,op2);break;
default: printf("invalid operator");

[CSE Department, CIT-Mandya] Page 9


[Data Structures Laboratory-15CSL38 ]

getch();exit(0);
}
push(res);
}
}
return s[top--];
}
void main()
{
char postfix[20];
int res;
clrscr();
printf("enter the suffix expression:");
gets(postfix);
res=evaluate(postfix);
printf ("result the expression=%d",res);
getch();
}

5 b. Solving Tower of Hanoi problem with n disks.

#include<stdio.h>
#include<conio.h>
void towers(int,char,char,char);
void main()
{
int n;
clrscr();
printf("enter how many disks:");
scanf("%d",&n);
towers(n,'A','B','C');
getch();
}
void towers(int n,char src,char temp,char dest)
{
if(n==1)
{
printf("move %d disk from %c peg to %c peg\n",n,src,dest);
return;
}
towers(n-1,src,dest,temp);
printf("move %d disk from %c peg to %c peg\n",n,src,dest);
towers(n-1,temp,src,dest);
}

[CSE Department, CIT-Mandya] Page 10


[Data Structures Laboratory-15CSL38 ]

6. Design, Develop and Implement a menu driven Program in C for the following operations on
Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 4
char q[MAX];
int count=0,f=0,r=-1;
voidCQinsert (char item)
{
if (count==MAX)
{
printf("circular queve overflow\n");
return;
}
r=(r+1)%MAX;
q[r]=item;
count=count+1;
}
voidCQdelete()
{
if (count==0)
{
printf("circular queve underflow\n");
return;
}
printf("delete item=%c",q[f]);
f=(f+1)%MAX;
count=count-1;
}
void display()
{
inti,j;
if (count==0)
{
printf("circular queve is empty\n");
return;
}
j=f;
printf("circular queve contents are:");
for (i=0;i<count;i++)
[CSE Department, CIT-Mandya] Page 11
[Data Structures Laboratory-15CSL38 ]

{
printf(" %c",q[j]);
j=(j+1)%MAX;
}
}
void main()
{
int choice;
char item;
clrscr();
for(;;)
{
printf("\n_____CircularQueve Operations______\n");
printf(" 1.Insert\n 2.Delete\n 3.Display\n 4.Exit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the character to be inserted:\n");
scanf(" %c",&item);
CQinsert(item);
break;
case 2:CQdelete();break;
case 3:display();break;
case 4:exit(0);
}
}
}

[CSE Department, CIT-Mandya] Page 12


[Data Structures Laboratory-15CSL38 ]

7. Design, Develop and Implement a menu driven Program in C for the following operations on
Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
char name[20],USN[15],branch[20];
intsem;
longintphno;
struct node *link;
};
typedefstruct node *NODE;
NODE first=NULL,nn;
int count=0;
void allocate()
{
nn=(NODE)malloc(sizeof (struct node));
printf("enter name:");
scanf("%s",nn->name);
printf("enter USN:");
scanf("%s",nn->USN);
printf("enter branch:");
scanf("%s",nn->branch);
printf("enter sem:");
scanf("%d",&nn->sem);
printf("enter phone number:");
scanf("%ld",&nn->phno);
nn->link =NULL;
}
voidinsert_front()
{
allocate();
count++;
if(first==NULL)
{
first=nn;
return;
}
nn->link=first;
first=nn;
[CSE Department, CIT-Mandya] Page 13
[Data Structures Laboratory-15CSL38 ]

}
voidinsert_rear()
{
NODE cur;
allocate();
count++;
if(first==NULL)
{
first=nn;
return;
}
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=nn;
}
voiddelete_front()
{
NODE cur;
if(count==0)
{
printf("empty record");
return;
}
cur=first;
first=first->link;
printf("\n deleted student information\n");
printf("Name=%s\t USN=%s\t Branch=%s\t Sem=%d\t Phone=%ld\n",cur-
>name,cur->USN,cur->branch,cur->sem,cur->phno);
free(cur);
count--;
}
voiddelete_rear()
{
NODE cur,prev;
if(count==0)
{
printf("empty record");
return;
}
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;

[CSE Department, CIT-Mandya] Page 14


[Data Structures Laboratory-15CSL38 ]

cur=cur->link;
}
printf("\n deleted student information \n");
printf("name=%s\t USN=%s\t Branch=%s\t SEM=%d\t phone=%ld\n",cur-
>name,cur->USN,cur->branch,cur->sem,cur->phno);
prev->link=NULL;
free(cur);
count--;
}

void display()
{
NODE cur;
if(count==0)
{
printf("empty list");
return;
}
cur=first;
printf("\nstudent details\n");
printf("\nName\t USn\t Branch\t Sem\t phone\n");
while(cur!=NULL)
{
printf("%s\t %s\t %s\t %d\t %ld\n",cur->name,cur->USN,cur-
>branch,cur->sem,cur->phno);
cur=cur->link;
}
printf("total no of students info=%d",count);
}
void main()
{
intchoice,n,i;
clrscr();
for(;;)
{
printf(" \n singly linked list operations \n");
printf("1.create\n 2.display\n 3.insert front\n 4.delete front\n
5.insert rear\n 6.delete rear\n 7.exit\n");
printf("enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter how many students:");
scanf("%d",&n);
for(i=0;i<n;i++)
insert_front();
break;

[CSE Department, CIT-Mandya] Page 15


[Data Structures Laboratory-15CSL38 ]

case 2: display();
break;
case 3: insert_front();
break;
case 4: delete_front();
break;
case 5: insert_rear();
break;
case 6: delete_rear();
break;
case 7: exit(0);
}
}
}

[CSE Department, CIT-Mandya] Page 16


[Data Structures Laboratory-15CSL38 ]

8. Design, Develop and Implement a menu driven Program in C for the following operations on
Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal,
PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
char name[20],ssn[20],dept[20],desg[20];
intsal;
longintphno;
struct node*llink,*rlink;
};
typedefstruct node*NODE;
NODE first=NULL,nn;
int count=0;
void allocate()
{
nn=(NODE)malloc(sizeof(struct node));
printf("enter name:");
scanf("%s",nn->name);
printf("enter ssn:");
scanf("%s",nn->ssn);
printf("enter department:");
scanf("%s",nn->dept);
printf("enter designation:");
scanf("%s",nn->desg);
printf("enter salary:");
scanf("%d",&nn->sal);
printf("enter phonenumber:");
scanf("%ld",&nn->phno);
nn->llink=nn->rlink=NULL;
}
voidinsert_front()
{
allocate();count++;
if(first==NULL)
{
first=nn;
return;
[CSE Department, CIT-Mandya] Page 17
[Data Structures Laboratory-15CSL38 ]

}
nn->rlink=first;
first->llink=nn;
first=nn;
}
voidinsert_rear()
{
NODE cur;
allocate();
count++;
if (first==NULL)
{
first=nn;
return;
}
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
cur->rlink=nn;
nn->llink=cur;
}
voiddelete_front()
{
NODE cur;
if(count==0)
{
printf("empty list");
return;
}
cur=first;
first=first->rlink;
printf("deleted employee details\n");
printf("name=%s\t ssn=%s\t dept=%s\t designation=%s\t sal=%d\t
phone=%ld\n",cur->name,cur->ssn,cur->dept,cur->desg,cur->sal,cur-
>phno);
free(cur);
count--;
}
voiddelete_rear()
{
NODE cur,prev;
if(count==0)
{
printf("empty list");
return;

[CSE Department, CIT-Mandya] Page 18


[Data Structures Laboratory-15CSL38 ]

}
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
prev=cur->llink;
prev->rlink=NULL;
printf("deleted employee details\n");
printf("name=%s\t ssn=%s\t dept=%s\t desg=%s\t sal=%d\t phone=
%ld\n",cur->name,cur->ssn,cur->dept,cur->desg,cur->sal,cur-
>phno);
free(cur);
count--;
}
void display()
{
NODE cur;
if(count==0)
{
printf("empty list");
return;
}
cur=first;
printf("name\t ssn\t dept\t desg\t sal\t phone\n");
while(cur!=NULL)
{
printf("%s\t%s\t%s\t%d\t%ld\n",cur->name,cur->ssn,cur->dept,cur-
>desg,cur->sal,cur->phno);
cur=cur->rlink;
}
printf("total no. of employee=%d\n",count);
}
void main()
{
intchoice,i,n;
clrscr();
for(;;)
{
printf("*** doubly linked list operation ***\n");
printf("1.create\n 2.insert_front\n 3.delete_front\n");
printf("4.insert_rear\n 5.delete_rear\n 6.display\n 7.exit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{

[CSE Department, CIT-Mandya] Page 19


[Data Structures Laboratory-15CSL38 ]

case 1:printf("enter no. of employee");


scanf("%d",&n);
for(i=0;i<n;i++)
insert_rear();
break;
case 2:insert_front();break;
case 3:delete_front();break;
case 4:insert_rear();break;
case 5:delete_rear();break;
case 6:display();break;
case 7:exit(0);
}
}
}

[CSE Department, CIT-Mandya] Page 20


[Data Structures Laboratory-15CSL38 ]

9. Design, Develop and Implement a Program in C for the following operationson Singly Circular
Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#include<math.h>
struct node
{
intce;
intxe,ye,ze;
struct node *link;
int flag;
};
typedefstruct node *NODE;
NODE head,head1,head2,head3,nn;
void allocate()
{
nn=(NODE)malloc(sizeof(struct node));
printf("enter coeff of term\n");
scanf("%d",&nn->ce);
printf("enter the expo of x term\n");
scanf("%d",&nn->xe);
printf("enter expo of y term\n");
scanf("%d",&nn->ye);
printf("enter expo of z term\n");
scanf("%d",&nn->ze);
nn->flag=0;
nn->link=NULL;
}
NODE insert_rear(NODE head)
{
NODE temp;
if(head->link==head)
{
nn->link=head;
head->link=nn;
return head;
}
temp=head;
while(temp->link!=head)
{

[CSE Department, CIT-Mandya] Page 21


[Data Structures Laboratory-15CSL38 ]

temp=temp->link;
}
temp->link=nn;
nn->link=head;
return head;
}
NODE read(NODE head)
{
intn,i;
printf("enter the number of terms in polynomial\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
allocate();
head=insert_rear(head);
}
return head;
}
void display(NODE head)
{
NODE cur;
cur=head->link;
while(cur!=head)
{
printf("%dx^ %dy^ %dz^ %d",cur->ce,cur->xe,cur->ye,cur->ze);
cur=cur->link;
if(cur->ce>=0&&cur!=head)
printf("+");
}
printf("\n");
}
void evaluate()
{
intx,y,z,answer=0;
NODE cur;
head=read(head);
printf("evaluated polynomial is:");
display(head);
printf("enter values of x,y and z\n");
scanf("%d %d %d",&x,&y,&z);
cur=head->link;
while(cur!=head)
{
answer+=(cur->ce)*pow(x,cur->xe)*pow(y,cur->ye)*pow(z,cur->ze);
cur=cur->link;
}
printf("The result of evaluated polynomial is %d\n",answer);

[CSE Department, CIT-Mandya] Page 22


[Data Structures Laboratory-15CSL38 ]

}
NODE add(NODE head1,NODE head2)
{
NODE cur1,cur2;
head3->link=head3;
for(cur1=head1->link;cur1!=head1;cur1=cur1->link)
{
for(cur2=head2->link;cur2!=head2;cur2=cur2->link)
{
if((cur1->xe==cur2->xe)&&(cur1->ye==cur2->ye)&&(cur1->ze==cur2-
>ze))
{
nn=(NODE)malloc(sizeof(struct node));
nn->ce=(cur1->ce)+(cur2->ce);
nn->xe=cur1->xe;
nn->ye=cur1->ye;
nn->ze=cur1->ze;
head3=insert_rear(head3);
cur1->flag=1;
cur2->flag=1;
}
}
}
cur1=head1->link;
cur2=head2->link;
while(cur1!=head1)
{
if(cur1->flag==0)
{
nn=(NODE)malloc(sizeof(struct node));
nn->ce=(cur1->ce);
nn->xe=cur1->xe;
nn->ye=cur1->ye;
nn->ze=cur1->ze;
head3=insert_rear(head3);
}
cur1=cur1->link;
}
while(cur2!=head2)
{
if(cur2->flag==0)
{
nn=(NODE)malloc(sizeof(struct node));
nn->ce=(cur2->ce);
nn->xe=cur2->xe;
nn->ye=cur2->ye;
nn->ze=cur2->ze;

[CSE Department, CIT-Mandya] Page 23


[Data Structures Laboratory-15CSL38 ]

head3=insert_rear(head3);
}
cur2=cur2->link;
}
return head3;
}
void addition()
{
int i,n1,n2;
NODE cur1,cur2;
head1->link=head1;
head1=read(head1);
head2->link=head2;
head2=read(head2);
printf("polynomial 1 is:");
display(head1);
printf("polynomial 2 is:");
display(head2);
head3=add(head1,head2);
printf("resultant polynomial is:");
display(head3);
}
void main()
{
int choice;
head=(NODE)malloc(sizeof(struct node));
head1=(NODE)malloc(sizeof(struct node));
head2=(NODE)malloc(sizeof(struct node));
head->link=head;
clrscr();
for(;;)
{
printf("1:Represent and Evaluate 2:Sum of two polynomials 3:exit\
n");
printf("Enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:evaluate();
break;
case 2:addition();
break;
case 3:exit(0);
break;
}
}
}

[CSE Department, CIT-Mandya] Page 24


[Data Structures Laboratory-15CSL38 ]

10. Design, Develop and Implement a menu driven Program in C for the following operations on
Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int info;
struct node *llink,*rlink;
};
typedefstruct node *NODE;
NODE root=NULL;
NODE insert(NODE root,int item)
{
NODE temp,prev,cur;
temp=(NODE)malloc(sizeof(struct node));
temp->info=item;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
return temp;
cur=root;
prev=NULL;
while(cur!=NULL)
{
prev=cur;
if(item==cur->info)
return root;
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(item<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
return root;
}
void preorder(NODE root)
{

[CSE Department, CIT-Mandya] Page 25


[Data Structures Laboratory-15CSL38 ]

if(root==NULL)
return;
printf("%d\t",root->info);
preorder(root->llink);
preorder(root->rlink);
}
voidinorder(NODE root)
{
if(root==NULL)
return;
inorder(root->llink);
printf("%d\t",root->info);
inorder(root->rlink);
}
voidpostorder(NODE root)
{
if(root==NULL)
return;
postorder(root->llink);
postorder(root->rlink);
printf("%d\t",root->info);
}
void traversal(NODE root)
{
printf("preorder traversal of tree\n");
preorder(root);
printf("\n inorder traversal of tree\n");
inorder(root);
printf("\n postorder traversal of tree\n");
postorder(root);
}
void search(NODE root,int item)
{
NODE cur;
cur=root;
while(cur!=NULL)
{
if(item==cur->info)
{
printf("item found\n");
return;
}
if(item<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}

[CSE Department, CIT-Mandya] Page 26


[Data Structures Laboratory-15CSL38 ]

if(cur==NULL)
{
printf("item not found\n");
return;
}
}
void main()
{
intchoice,item,n,i;
clrscr();
for(;;)
{
printf("\n 1:create bst 2:traversal 3:search 4:exit\n");
printf("enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:root=NULL;
printf("enter number of items to create tree\n");

scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter item to insert\n");
scanf("%d",&item);
root=insert(root,item);
}
break;
case 2:traversal(root);
break;
case 3:printf("enter item to search\n");
scanf("%d",&item);
search(root,item);
break;
case 4:exit(0);
break;
}
}
}

[CSE Department, CIT-Mandya] Page 27


[Data Structures Laboratory-15CSL38 ]

11. Design, Develop and Implement a Program in C for the following operations on Graph(G) of
Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
intn,s[10],a[20][20];
int source;
voidbfs_traversal()
{
intf,r,q[20],u,v,i;
for(i=0;i<n;i++)
s[i]=0;
f=r=0;
q[r]=source; s[source]=1;
while(f<=r)
{
u=q[f++];
for(v=0;v<n;v++)
{
if(a[u][v]==1&&s[v]==0)
{
s[v]=1;
q[++r]=v;
}
}}
}
void main()
{
inti,j;
clrscr();
printf("enter the number of cities \n");
scanf("%d",&n);
printf("enter the adjacency matrix for cities connection \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("enter the source city\n");
scanf("%d",&source);
bfs_traversal();
for(i=0;i<n;i++)

[CSE Department, CIT-Mandya] Page 28


[Data Structures Laboratory-15CSL38 ]

{
if(s[i]==0)
printf("city %d is not reachebale\n",i);
else
printf("city %d is reachebale\n",i);
}
getch();
}

[CSE Department, CIT-Mandya] Page 29


[Data Structures Laboratory-15CSL38 ]

12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine the
records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of m memory
locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Design and develop a Program in C that uses Hash function H: K L as
H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the
address space L. Resolve the collision (if any) using linear probing.

#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int a[MAX],key,count=10;
void hashing()
{
inti,L,flag=0;
L=(key%100);
if(a[L]==-1)
{
a[L]=key;
count++;
}
else
{
printf("collision detected for the key %d\n",key);
if(count==MAX)
{
printf("hash table full\n");
return;
}
for(i=L+1;i<MAX;i++)
if(a[i]==-1)
{
a[i]=key;
flag=1;
count++;
break;
}
for(i=10;i<L&&flag==0;i++)
if(a[i]==-1)
{a[i]=key;
count++;
break;
}
printf("collision avoided for key %d\n",key);

[CSE Department, CIT-Mandya] Page 30


[Data Structures Laboratory-15CSL38 ]

}}
void main()
{
intN,i;
clrscr();
for(i=10;i<MAX;i++)
a[i]=-1;
printf("enter the number of employee records to enter\n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("enter the 4 digit key for employee records\n");
scanf("%d",&key);
hashing();
}
printf("\n----------hash table----\n");
for(i=10;i<MAX;i++)
printf("%d\t %d\t",i,a[i]);
getch();
}

[CSE Department, CIT-Mandya] Page 31


[Data Structures Laboratory-15CSL38 ]

[CSE Department, CIT-Mandya] Page 32

You might also like