0% found this document useful (0 votes)
5 views

data structure lab

The document contains multiple C programming code snippets demonstrating various data structures and algorithms, including a weekly calendar, string replacement, stack operations, infix to postfix conversion, postfix evaluation, Tower of Hanoi, circular queue implementation, and linked list operations. Each section includes function definitions and main functions to execute the respective tasks. The code is structured to illustrate fundamental programming concepts and data structure manipulations.

Uploaded by

achaithana35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

data structure lab

The document contains multiple C programming code snippets demonstrating various data structures and algorithms, including a weekly calendar, string replacement, stack operations, infix to postfix conversion, postfix evaluation, Tower of Hanoi, circular queue implementation, and linked list operations. Each section includes function definitions and main functions to execute the respective tasks. The code is structured to illustrate fundamental programming concepts and data structure manipulations.

Uploaded by

achaithana35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Data Structure & Applications Lab BCSL305

1a.
#include<stdio.h>

#include<string.h>

struct Day

char name[20];

int date;

char activity[100];

};

main()

struct Day calendar[7];

strcpy(calendar[0].name,"Monady");

calendar[0].date=1;

strcpy(calendar[0].activity,"Work from 9AM to 5PM");

strcpy(calendar[1].name,"Tuesday");

calendar[1].date=2;

strcpy(calendar[1].activity,"Meeting at 10AM");

strcpy(calendar[2].name,"Wednesday");

calendar[2].date=3;

strcpy(calendar[2].activity,"Gym at 6PM");

strcpy(calendar[3].name,"Thurday");

calendar[3].date=4;

strcpy(calendar[3].activity,"Dinner with friends at 7PM");

strcpy(calendar[4].name,"Friday");

calendar[4].date=5;

strcpy(calendar[4].activity,"Movie night at 8PM");

strcpy(calendar[5].name,"Saturday");

calendar[5].date=6;

strcpy(calendar[5].activity,"Weekend gateway");

strcpy(calendar[6].name,"Sunday");

By Chaitana A Page 1
Data Structure & Applications Lab BCSL305

calendar[6].date=7;

strcpy(calendar[6].activity,"Relax and Recharge");

printf("Calendar for the week:\n");

int i;

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

printf("%s(Date;%d):%s\n",calendar[i].name,calendar[i].date,calendar[i].activity);

getch();

return 0;

1b.
#include<stdio.h>

#include<string.h>

struct Day

char name[20];

int date;

char activity[100];

};

void create(struct Day calendar[7])

int i;

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

printf("Enter details for %s:\n",calendar[i].name);

printf("Date:");

scanf("%d",&calendar[i].date);

printf("Activity:");

By Chaitana A Page 2
Data Structure & Applications Lab BCSL305

scanf("%s",&calendar[i].activity);

void read (struct Day calendar[7])

FILE*file=fopen("calendar.txt","r");

if(file==NULL)

printf("Error opening the File:\n");

return;

int i;

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

fscanf(file,"%d",&calendar[i].date);

fscanf(file,"%s",&calendar[i].activity);

fclose(file);

void display(struct Day calendar[7])

printf("\nCalendar for the week:\n");

int i;

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

printf("%s(Date;%d):%s\n",calendar[i].name,calendar[i].date,calendar[i].activity);

int main()

struct Day calendar[7];

By Chaitana A Page 3
Data Structure & Applications Lab BCSL305

strcpy(calendar[0].name,"Monday");

strcpy(calendar[1].name,"Tuesday");

strcpy(calendar[2].name,"Wednesday");

strcpy(calendar[3].name,"Thursday");

strcpy(calendar[4].name,"Friday");

strcpy(calendar[5].name,"Saturday");

strcpy(calendar[6].name,"Sunady");

int choice;

printf("1:Create Calendar\n2:Read Calendar from file\n");

printf("\nEnter your choice:");

scanf("%d",&choice);

scanf("%d",&choice);

switch(choice)

case 1:

create(calendar);

break;

case 2:

read(calendar);

break;

default:printf("Invalid choice\n");

display(calendar);

getch();

By Chaitana A Page 4
Data Structure & Applications Lab BCSL305

2.
#include<stdio.h>

#include<string.h>

int main()

int i=0,j=0,k=0,c=0,m=0,flag=0;

char str[100],pat[20],rep[20],rstr[100];

printf("Enter any string:\t");

gets(str);

printf("Enter the character to replace:\t");

gets(pat);

printf("Enter character to replace%s with:\t",pat);

gets(rep);

printf("\nString before replacing\n%s\n",str);

while(str[i]!='\0')

if(str[m]==pat[j])

j++; m++;

if(pat[j]=='\0')

flag=1;

for(k=0;rep[k]!='\0';k++,c++)

rstr[c]=rep[k];

i=m; j=0;

else

By Chaitana A Page 5
Data Structure & Applications Lab BCSL305

rstr[c]=str[i];

c++;i++;m=i;j=0;

rstr[c]='\0';

if(flag=1)

printf("String after replacing\n%s\n",rstr);

else

printf("Not Found");

getch();

By Chaitana A Page 6
Data Structure & Applications Lab BCSL305

3.
#include<stdio.h>

#include<stdlib.h>

#define SIZE 4

int stk[SIZE],rev[SIZE],top=-1;

void push()

int item;

if(top==SIZE-1)

printf("Stack Overflow");

else

printf("Enter the Element to Push\n");

scanf("%d",&item);

top++;

stk[top]=item;

void pop()

if(top==-1)

printf("Stack is Underflow");

else

printf("Popped element is %d",stk[top]);

top--;

By Chaitana A Page 7
Data Structure & Applications Lab BCSL305

void display()

int i;

if(top==-1)

printf("Stack is Empty");

else

printf("Element in stack are\n");

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

printf("%d\t",stk[i]);

void palindrome()

int i,count=-1,j=-1;

if(top==-1)

printf("Stack is Empty");

else

for(i=top;i>=0;i--)

j++;

rev[j]=stk[i];

By Chaitana A Page 8
Data Structure & Applications Lab BCSL305

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

if(stk[i]==rev[i])

count++;

if(count==top)

printf("\tStack is Palindrome");

else

printf("\tStack is not Palindrome");

int main()

int choice;

while(1)

printf("\n\n---MENU---\n");

printf("\n1:Push\n2:Pop\n3:Display\n4:Palindrome\n5:Exit\n");

printf("Enter your choice:\t");

scanf("%d",&choice);

switch(choice)

case 1:push();

break;

case 2:pop(0);

By Chaitana A Page 9
Data Structure & Applications Lab BCSL305

break;

case 3:display();

break;

case 4:palindrome();

break;

case 5:exit(0);

break;

default:printf("\tInvalid Choice");

getch();

By Chaitana A Page 10
Data Structure & Applications Lab BCSL305

4.
#include<stdio.h>

#include<string.h>

#include<conio.h>

int F(char symbol)

switch(symbol)

case'+':

case'-':return 2;

case'*':

case'/':

case'%':return 4;

case'^':

case'$':return 5;

case'(':return 0;

case'#':return-1;

default:return 8;

int G(char symbol)

switch(symbol)

case'+':

case'-':return 1;

case'*':

case'/':

case'%':return 3;

case'^':

case'$':return 6;

By Chaitana A Page 11
Data Structure & Applications Lab BCSL305

case'(':return 9;

case')':return 0;

default:return 7;

void infix_postfix(char infix[],char postfix[])

int top=-1,i,j;

char symbol,s[30];

s[++top]='#';

j=0;

for(i=0;i<strlen(infix);i++)

symbol=infix[i];

while(F(s[top])>G(symbol))

postfix[j++]=s[top--];

if(F(s[top])!=G(symbol))

s[++top]=symbol;

else

top--;

while(s[top]!='#')

postfix[j++]=s[top--];

postfix[j]='\0';

int main()

By Chaitana A Page 12
Data Structure & Applications Lab BCSL305

char infix[20],postfix[20];

printf("Enter the Infix Expression:\n");

scanf("%s",infix);

infix_postfix(infix,postfix);

printf("The Post Expression is\n");

printf("%s\n",postfix);

getch();

By Chaitana A Page 13
Data Structure & Applications Lab BCSL305

5a.
#include<stdio.h>

#include<math.h>

#include<string.h>

#include<conio.h>

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

switch(symbol)

case'+':return(op1+op2);

case'-':return(op1-op2);

case'*':return(op1*op2);

case'/':return(op1/op2);

case'%':return(abs(op1)%abs(op2));

case'^':return pow(op1,op2);

return 0;

int main()

double s[20];

double res;

double op1;

double op2;

int top;

int i;

char postfix[20];

char symbol;

printf("Enter the postfix expression\n");

scanf("%s",postfix);

top=-1;

By Chaitana A Page 14
Data Structure & Applications Lab BCSL305

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 %f\n",res);

return 0;

5b.
#include<stdio.h>

#include<conio.h>

void tower(int n,char src,char temp,char disk)

if(n==0)

return;

tower(n-1,src,disk,temp);

printf("\n Move Disc from %d from %c to %c",n,src,disk);

By Chaitana A Page 15
Data Structure & Applications Lab BCSL305

tower(n-1,temp,src,disk);

int main()

int n;

printf("Enter the Number of Disks:\t");

scanf("%d",&n);

tower(n,'A','B','C');

getch();

By Chaitana A Page 16
Data Structure & Applications Lab BCSL305

6.
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define size 5

int item,queue[size];

int front=-1,rear=-1;

void add()

if(front==(rear+1)%size)

printf("\nThe Circular Queue is Full and Overflow");

else

printf("\nEnter the element:\n");

scanf("%d",&item);

if(front==-1)

front=rear=0;

else

rear=(rear+1)%size;

queue[rear]=item;

void del()

if(front==-1)

By Chaitana A Page 17
Data Structure & Applications Lab BCSL305

printf("\nThe Queue is Empty and Underflow\n");

else

item=queue[front];

if(front==rear)

front=rear=-1;

else

front=(front+1)%size;

printf("\nThe deleted item is %d\n",item);

void display()

int i;

if(front==-1)

printf("\nThe Queue is Empty\n");

return;

i=front;

while(i!=rear)

printf("%d\t",queue[i]);

i=(i+1)%size;

printf("%d\t",queue[i]);

int main()

By Chaitana A Page 18
Data Structure & Applications Lab BCSL305

int choice;

for(;;)

printf("\n---MENU---");

printf("\n1:Insert\n2:Delete\n3:Display\n4:Exit");

printf("\nEnter your choice:\t");

scanf("%d",&choice);

switch(choice)

case 1:add();

break;

case 2:del();

break;

case 3:display();

break;

case 4:exit(0);

default: printf("\nInvalid Choice\n");

return 0;

By Chaitana A Page 19
Data Structure & Applications Lab BCSL305

7.
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int count=0;

struct node

int sem;

int phno;

char name[20],branch[10],usn[20];

struct node*next;

*first=NULL,*last=NULL,*temp=NULL,*temp1;

void create()

int sem;

int phno;

char name[20],branch[10],usn[20];

temp=(struct node*)malloc(sizeof(struct node));

printf("Enter USN,Name,Branch,Sem,Phno of the student:\n");

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 insertfirst()

By Chaitana A Page 20
Data Structure & Applications Lab BCSL305

if(first==NULL)

create();

first=temp;

last=first;

else

create();

temp->next=first;

first=temp;

void insertlast()

if(first==NULL)

create();

first=temp;

last=first;

else

create();

last->next=temp;

last=temp;

void display()

temp1=first;

By Chaitana A Page 21
Data Structure & Applications Lab BCSL305

if(temp1==NULL)

printf("\nList empty\n");

return;

printf("\nLinked list element from the beginning:\n");

while(temp1!=NULL)

printf("\n%s\n%s\n%s\n%d\n%d\n\n",temp1->usn,temp1->name,temp1-
>branch,temp1->sem,temp1->phno);

temp1=temp1->next;

printf("No of students:%d\n",count);

void deleteend()

struct node*temp;

temp=first;

if(temp->next==NULL)

free(temp);

first=NULL;

else

while(temp->next!=last)

temp=temp->next;

printf("%s\n%s\n%s\n%d\n%d\n",last->usn,last->name,last->branch,last->sem,last-
>phno);

free(last);

temp->next=NULL;

last=temp;

By Chaitana A Page 22
Data Structure & Applications Lab BCSL305

count--;

void deletefront()

struct node*temp;

temp=first;

if(temp->next==NULL)

free(temp);

first=NULL;

else

first=temp->next;

printf("%s\n%s\n%s\n%d\n%d\n",temp->usn,temp->name,temp->branch,temp-
>sem,temp->phno);

free(temp);

count--;

int main()

int ch,n,i;

first=NULL;

temp=temp1=NULL;

while(1)

printf("\n---MENU---\n");

printf("1:Create SLL\n2:Display\n3:Insert at end\n4:Delete at end\n5:Insert at


front\n6:Delete at front\n7:Exit\n");

printf("Enter your choice:");

By Chaitana A Page 23
Data Structure & Applications Lab BCSL305

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter the no of students:\n");

scanf("%d",&n);

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

insertfirst();

break;

case 2:display();

break;

case 3:insertlast();

break;

case 4:deleteend();

break;

case 5:insertfirst();

break;

case 6:deleteend();

break;

case 7:exit(0);

break;

default:printf("Invalid choice\n");

return 0;

By Chaitana A Page 24
Data Structure & Applications Lab BCSL305

8.
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int count=0;

struct node

struct node*llink;

struct node*rlink;

int info,ssn,phno;

float sal;

char name[20],dept[10],desg[20];

};

typedef struct node*NODE;

NODE temp,temp1,cur,first;

void create()

int ssn,phno;

char name[20],dept[10],desg[20];

float sal;

temp=(struct node*)malloc(sizeof(struct node));

temp->llink=NULL;

temp->rlink=NULL;

printf("\nEnter SSN,Name,Department,Designation,Salary,Phone No of
Employee\n");

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;

By Chaitana A Page 25
Data Structure & Applications Lab BCSL305

temp->phno=phno;

count++;

void insertfront()

if(first==NULL)

create();

first=temp;

temp1=first;

else

create();

temp->rlink=first;

first->llink=temp;

first=temp;

void insertrear()

if(first==NULL)

create();

first=temp;

temp1=first;

else

create();

temp1->rlink=temp;

By Chaitana A Page 26
Data Structure & Applications Lab BCSL305

temp->llink=temp1;

temp1=temp;

void display()

cur=first;

if(cur==NULL)

printf("\nList is Empty\n");

return;

printf("\n The Elements are\n");

while(cur!=NULL)

printf("\n%d\t%s\t%s\t%s\t%f\t%d\t\n",cur->ssn,cur->name,cur->dept,cur-
>desg,cur->sal,cur->phno);

cur=cur->rlink;

printf("\nNumber of Employee=%d\n",count);

int deleterear()

struct node*temp;

temp=first;

if(temp->rlink==NULL)

free(temp);

first=NULL;

return 0;

By Chaitana A Page 27
Data Structure & Applications Lab BCSL305

else

cur=temp1->llink;

cur->rlink=NULL;

printf("\n%d\t%s\t%s\t%s\t%f\t%d\t",temp1->ssn,temp1->name,temp1-
>dept,temp1->desg,temp1->sal,temp1->phno);

free(temp1);

count--;

return 0;

int deletefront()

struct node*temp;

temp=first;

if(temp->rlink==NULL)

free(temp);

first=NULL;

else

first=first->rlink;

printf("\n%d\t%s\t%s\t%s\t%f\t%d\t",temp->ssn,temp->name,temp-
>dept,temp->desg,temp->sal,temp->phno);

free(temp);

count--;

return 0;

int main()

By Chaitana A Page 28
Data Structure & Applications Lab BCSL305

int ch,n,i;

first=NULL;

temp=temp1=NULL;

printf("\n---MENU---\n");

printf("\n1:Create DLL of N Employee\n2:Display\n3:Insert Front\n4:Insert


Rear\n5:Delete Front\n6:Delete Rear\n7:Exit\n");

while(1)

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter the no of Employee\t");

scanf("%d",&n);

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

insertrear();

break;

case 2:display();

break;

case 3:insertfront();

break;

case 4:insertrear();

break;

case 5:deletefront();

break;

case 6:deleterear();

break;

case 7:exit(0);

break;

}return 0;}

By Chaitana A Page 29
Data Structure & Applications Lab BCSL305

9.
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<string.h>

int flag;

struct node

int cf;

int px,py,pz;

struct node*link;

};

typedef struct node*NODE;

NODE getnode()

NODE x;

x=(struct node*)malloc(sizeof(struct node));

if(x==NULL)

printf("\nOut of Memory\n");

exit(0);

return x;

NODE insertrear(int cf,int x,int y,int z,NODE head)

NODE temp,cur;

temp=getnode();

temp->cf=cf;

temp->px=x;

temp->py=y;

By Chaitana A Page 30
Data Structure & Applications Lab BCSL305

temp->pz=z;

cur=head->link;

while(cur->link!=head)

cur=cur->link;

cur->link=temp;

temp->link=head;

return head;

NODE readpoly(NODE head)

int i;

int px,py,pz;

int cf;

printf("\nEnter the code as -999 to End the Polynomial\n");

for(i=1;;i++)

printf("\nEnter the %d term:",i);

printf("Coef=");

scanf("%d",&cf);

if(cf==-999)

break;

printf("Pow x=");

scanf("%d",&px);

printf("Pow y=");

scanf("%d",&py);

printf("Pow z=");

scanf("%d",&pz);

head=insertrear(cf,px,py,pz,head);

return head;

By Chaitana A Page 31
Data Structure & Applications Lab BCSL305

int evaluate(NODE head)

int x,y,z,sum=0;

NODE poly;

printf("Enter the value of x,y and z:\t");

scanf("%d%d%d",&x,&y,&z);

poly=head->link;

while(poly!=head)

sum=sum+poly->cf*pow(x,poly->px)*pow(y,poly->py)*pow(z,poly->pz);

poly=poly->link;

return sum;

void display(NODE head)

NODE temp;

if(head->link==head)

printf("\nPoly Does not Exist\n");

return;

temp=head->link;

while(temp!=head)

printf("+%dx^%dy^%dz^%d",temp->cf,temp->px,temp->py,temp->pz);

temp=temp->link;

printf("\n");

NODE addpoly(NODE h1,NODE h2,NODE h3)

By Chaitana A Page 32
Data Structure & Applications Lab BCSL305

NODE p1,p2;

int x1,x2,y1,y2,z1,z2,cf1,cf2,cf;

int flag;

p1=h1->link;

while(p1!=h1)

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;

if(cf!=0)

h3=insertrear(cf,x1,y1,z1,h3);

else

h3=insertrear(cf1,x1,y1,z1,h3);

p1=p1->link;

By Chaitana A Page 33
Data Structure & Applications Lab BCSL305

p2=h2->link;

while(p2!=h2)

if(p2!=h2)

h3=insertrear(p2->cf,p2->px,p2->py,p2->pz,h3);

p2=p2->link;

return h3;

int main()

NODE h1,h2,h3,head;

int sum,ch;

head=getnode();

head->link=head;

h1=getnode();

h2=getnode();

h3=getnode();

h1->link=h1;

h2->link=h2;

h3->link=h3;

for(;;)

printf("\n1:Read Polynomial\n2:Evaluate\n3:Display\n4:Add
Poly\n5:Exit\n");

printf("Enter the choice:\t");

scanf("%d",&ch);

switch(ch)

By Chaitana A Page 34
Data Structure & Applications Lab BCSL305

case 1:printf("Enter the Polynomial\n");

head=readpoly(head);

break;

case 2:sum=evaluate(head);

printf("The Given Polynomial is \n");

display(head);

printf("The Result=%d\n",sum);

break;

case 3:printf("The Given Polynomal is \n");

display(head);

break;

case 4:printf("Enter the First Poly\n");

h1=readpoly(h1);

printf("Enter the Second Poly\n");

h2=readpoly(h2);

h3=addpoly(h1,h2,h3);

printf("The First Poly is\n");

display(h1);

printf("The Sec Poly is\n");

display(h2);

printf("Sum of Two Poly is\n");

display(h3);

break;

case 5:exit(0);

break;

default:printf("\nInvalid Choice\n");

return 0;

By Chaitana A Page 35
Data Structure & Applications Lab BCSL305

10.
#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node*llink;

struct node*rlink;

};

typedef struct node*NODE;

NODE getnode()

NODE temp;

temp=(struct node*)malloc(sizeof(struct node));

temp->llink=NULL;

temp->rlink=NULL;

return temp;

void insert(NODE root,NODE temp)

if(temp->info<root->info)

if(root->llink==NULL)

root->llink=temp;

else

insert(root->llink,temp);

if(temp->info>root->info)

if(root->rlink==NULL)

root->rlink=temp;

By Chaitana A Page 36
Data Structure & Applications Lab BCSL305

else

insert(root->rlink,temp);

void search(NODE root,int key)

if(root==NULL)

printf("\nSearch Unsuccessful\n");

else if(key==root->info)

printf("\nSearch Success Item %d Found\n",key);

else if(key<root->info)

search(root->llink,key);

else

search(root->rlink,key);

void inorder(NODE temp)

if(temp!=NULL)

inorder(temp->llink);

printf("%d\t",temp->info);

inorder(temp->rlink);

void preorder(NODE temp)

if(temp!=NULL)

printf("%d\t",temp->info);

preorder(temp->llink);

preorder(temp->rlink);

By Chaitana A Page 37
Data Structure & Applications Lab BCSL305

void postorder(NODE temp)

if(temp!=NULL)

postorder(temp->llink);

postorder(temp->rlink);

printf("%d\t",temp->info);

int main()

int ch,key,i,n;

NODE temp,root;

NODE getnode();

root=NULL;

printf("\nProgram for BST\n");

for(;;)

printf("\n1:Insert\n2:Search\n3:Traversal\n4:Exit\n");

printf("Enter your choice:\n");

scanf("%d",&ch);

switch(ch)

case 1:printf("Enter the no of elements:\n");

scanf("%d",&n);

printf("Enter the elemnts:\n");

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

temp=getnode();

By Chaitana A Page 38
Data Structure & Applications Lab BCSL305

scanf("%d",&temp->info);

if(root==NULL)

root=temp;

else

insert(root,temp);

break;

case 2:printf("\nEnter the Element to be Search:\t");

scanf("%d",&key);

search(root,key);

break;

case 3:if(root==NULL)

printf("\nTree is not created");

else

printf("\nThe Inorder Traversal\n");

inorder(root);

printf("\n The Preorder Traversal\n");

preorder(root);

printf("\n The Postorder Traversal\n ");

postorder(root);

break;

case 4:exit(0);

return 0;

By Chaitana A Page 39
Data Structure & Applications Lab BCSL305

11.
#include<stdio.h>

#include<stdlib.h>

void bfs(int src,int adj[10][10],int n)

int q[20],front=0,rear=-1,v,u,visited[10]={10};

q[++rear]=src;

visited[src]=1;

printf("%d\t",src);

while(front<=rear)

u=q[front++];

for(v=1;v<=n;v++)

if(adj[u][v]==1&&visited[v]==0)

q[++rear]=v;

printf("%d\t",v);

visited[v]=1;

int main()

int ch,i,j,src;

int adj[10][10],n;

while(1)

printf("\nProgram to Perform Graph Operation\n");

By Chaitana A Page 40
Data Structure & Applications Lab BCSL305

printf("\n1:Create a graph of N cities\n2:To print reachable nodes from source node


using BFS\n3:Exit\n");

printf("Enter your choice:\t");

scanf("%d",&ch);

switch(ch)

case 1:printf("enter the number of cities\t");

scanf("%d",&n);

printf("Enter the adjacency matrix\n");

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

for(j=1;j<=n;j++)

scanf("%d",&adj[i][j]);

break;

case 2:printf("Enter the source vertex to start traversal:\n");

scanf("%d",&src);

printf("Vertices visited are:\n");

bfs(src,adj,n);

break;

case 3:exit(0);

default:printf("\nInvalid Choice\n");

return 0;

12.

By Chaitana A Page 41
Data Structure & Applications Lab BCSL305

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#define MAX 10

struct rec

int num;

}r,s;

void main()

int i,ans=1,done=0,flag=0,ret;

int num,n,*p,key;

char*data=NULL;

FILE*fp;

fp=fopen("f2.dat","w+b");

printf("Collision Handling by Linear Probing:\n");

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

printf("%d\t",ftell(fp));

r.num=999;

fwrite(&r,1,sizeof(struct rec),fp);

do{

done=0;

rewind(fp);

printf("\nEnter the Number:\t");

scanf("%Id",&s.num);

if(s.num<1000&&s.num>9999)break;

key=(s.num)%10;

printf("Hash key %d\n",key);

if(flag==0)

By Chaitana A Page 42
Data Structure & Applications Lab BCSL305

fseek(fp,key*sizeof(struct rec),0);

else

fseek(fp,key*sizeof(struct rec),0);

printf("Insert Pos{%d}\n",ftell(fp));

ret=fread(&r,1,sizeof(struct rec),fp);

if(r.num<1000)

if(flag==0)

fseek(fp,key*sizeof(struct rec),SEEK_SET);

flag=1;

fwrite(&s,1,sizeof(struct rec),fp);

else

fseek(fp,-ret,SEEK_CUR);

fwrite(&s,1,sizeof(struct rec),fp);

else

printf("%d",r.num);

printf("\nCollision Deleted......!!!!!\n");

fseek(fp,0,SEEK_CUR);

while(!done)

fread(&r,1,sizeof(struct rec),fp);

if(r.num<1000)

By Chaitana A Page 43
Data Structure & Applications Lab BCSL305

fseek(fp,-4,SEEK_CUR);

fwrite(&s,1,sizeof(struct rec),fp);

done=1;

printf("\nDo You Wish to Continue?(1/0)\t");

scanf("%d",&ans);

}while(ans);

rewind(fp);

printf("File Contents are\n");

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

fread(&r,1,sizeof(struct rec),fp);

printf("%d\t",r.num);

getch();

THANK YOU

By Chaitana A Page 44

You might also like