data structure lab
data structure lab
1a.
#include<stdio.h>
#include<string.h>
struct Day
char name[20];
int date;
char activity[100];
};
main()
strcpy(calendar[0].name,"Monady");
calendar[0].date=1;
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[4].name,"Friday");
calendar[4].date=5;
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;
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];
};
int i;
for(i=0;i<7;i++)
printf("Date:");
scanf("%d",&calendar[i].date);
printf("Activity:");
By Chaitana A Page 2
Data Structure & Applications Lab BCSL305
scanf("%s",&calendar[i].activity);
FILE*file=fopen("calendar.txt","r");
if(file==NULL)
return;
int i;
for(i=0;i<7;i++)
fscanf(file,"%d",&calendar[i].date);
fscanf(file,"%s",&calendar[i].activity);
fclose(file);
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()
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;
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];
gets(str);
gets(pat);
gets(rep);
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)
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
scanf("%d",&item);
top++;
stk[top]=item;
void pop()
if(top==-1)
printf("Stack is Underflow");
else
top--;
By Chaitana A Page 7
Data Structure & Applications Lab BCSL305
void display()
int i;
if(top==-1)
printf("Stack is Empty");
else
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
int main()
int choice;
while(1)
printf("\n\n---MENU---\n");
printf("\n1:Push\n2:Pop\n3:Display\n4:Palindrome\n5:Exit\n");
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>
switch(symbol)
case'+':
case'-':return 2;
case'*':
case'/':
case'%':return 4;
case'^':
case'$':return 5;
case'(':return 0;
case'#':return-1;
default:return 8;
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;
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];
scanf("%s",infix);
infix_postfix(infix,postfix);
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>
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;
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--];
return 0;
5b.
#include<stdio.h>
#include<conio.h>
if(n==0)
return;
tower(n-1,src,disk,temp);
By Chaitana A Page 15
Data Structure & Applications Lab BCSL305
tower(n-1,temp,src,disk);
int main()
int n;
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)
else
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
else
item=queue[front];
if(front==rear)
front=rear=-1;
else
front=(front+1)%size;
void display()
int i;
if(front==-1)
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");
scanf("%d",&choice);
switch(choice)
case 1:add();
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
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];
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;
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");
By Chaitana A Page 23
Data Structure & Applications Lab BCSL305
scanf("%d",&ch);
switch(ch)
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];
};
NODE temp,temp1,cur,first;
void create()
int ssn,phno;
char name[20],dept[10],desg[20];
float sal;
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;
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");
while(1)
scanf("%d",&ch);
switch(ch)
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;
};
NODE getnode()
NODE x;
if(x==NULL)
printf("\nOut of Memory\n");
exit(0);
return x;
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;
int i;
int px,py,pz;
int cf;
for(i=1;;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 x,y,z,sum=0;
NODE poly;
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;
NODE temp;
if(head->link==head)
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");
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");
scanf("%d",&ch);
switch(ch)
By Chaitana A Page 34
Data Structure & Applications Lab BCSL305
head=readpoly(head);
break;
case 2:sum=evaluate(head);
display(head);
printf("The Result=%d\n",sum);
break;
display(head);
break;
h1=readpoly(h1);
h2=readpoly(h2);
h3=addpoly(h1,h2,h3);
display(h1);
display(h2);
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;
};
NODE getnode()
NODE temp;
temp->llink=NULL;
temp->rlink=NULL;
return 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);
if(root==NULL)
printf("\nSearch Unsuccessful\n");
else if(key==root->info)
else if(key<root->info)
search(root->llink,key);
else
search(root->rlink,key);
if(temp!=NULL)
inorder(temp->llink);
printf("%d\t",temp->info);
inorder(temp->rlink);
if(temp!=NULL)
printf("%d\t",temp->info);
preorder(temp->llink);
preorder(temp->rlink);
By Chaitana A Page 37
Data Structure & Applications Lab BCSL305
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;
for(;;)
printf("\n1:Insert\n2:Search\n3:Traversal\n4:Exit\n");
scanf("%d",&ch);
switch(ch)
scanf("%d",&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;
scanf("%d",&key);
search(root,key);
break;
case 3:if(root==NULL)
else
inorder(root);
preorder(root);
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>
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)
By Chaitana A Page 40
Data Structure & Applications Lab BCSL305
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
break;
scanf("%d",&src);
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");
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);
scanf("%Id",&s.num);
if(s.num<1000&&s.num>9999)break;
key=(s.num)%10;
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;
scanf("%d",&ans);
}while(ans);
rewind(fp);
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