Dsa (Aids)
Dsa (Aids)
a)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day {
};
int main() {
int i;
switch (i) {
case 0:
calendar[i]->name = strdup("Monday");
calendar[i]->date = 1;
calendar[i]->activity = strdup("Work");
break;
case 1:
calendar[i]->name = strdup("Tuesday");
calendar[i]->date = 2;
calendar[i]->activity = strdup("Meeting");
break;
case 2:
calendar[i]->name = strdup("Wednesday");
calendar[i]->date = 3;
calendar[i]->activity = strdup("Gym");
break;
case 3:
calendar[i]->name = strdup("Thursday");
calendar[i]->date = 4;
calendar[i]->activity = strdup("Study");
break;
case 4:
calendar[i]->name = strdup("Friday");
calendar[i]->date = 5;
break;
case 5:
calendar[i]->name = strdup("Saturday");
calendar[i]->date = 6;
calendar[i]->activity = strdup("Family outing");
break;
case 6:
calendar[i]->name = strdup("Sunday");
calendar[i]->date = 7;
calendar[i]->activity = strdup("Relax");
break;
printf("Calendar:\n");
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
return 0;
Expected Output:
Calendar:
b)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day {
};
int i;
calendar[i]->name = NULL;
calendar[i]->activity = NULL;
char temp[100];
scanf("%s", temp);
calendar[i]->name = strdup(temp);
scanf("%d", &calendar[i]->date);
scanf("%s", temp);
calendar[i]->activity = strdup(temp);
int i;
printf("Calendar:\n");
int main() {
create(calendar);
display(calendar);
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
return 0;
Expected Output:
Calendar:
Program:
#include<stdio.h>
#include<stdlib.h>
char str[100],pat[100],rep[100],ans[100];
int i,j,c,m,k,flag=0;
void stringmatch()
i=m=j=k=c=0;
while(str[c]!='\0')
if(str[m]==pat[i])
i++;
m++;
if(pat[i]=='\0')
flag=1;
for(k=0;rep[k]!='\0';k++)
ans[j]=rep[k];
j++;
i=0;
c=m;
else
{
ans[j]=str[c];
j++;
c++;
m=c;
i=0;
ans[j]='\0';
int main()
gets(str);
gets(pat);
gets(rep);
stringmatch();
if(flag==1)
else
return 0;
PROGRAM -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)
f. Exit
Support the program with appropriate functions for each of the above
operations.
Program:
#include<stdio.h>
#include<stdlib.h>
#define max_size 5
int stack[max_size],top=-1,i,status=0,count=0,item;
void push();
void pop();
void display();
void palin();
int main()
int choice;
while(choice)
printf("\n1.PUSH\n2.POP\n3.PALINDROME\n4.DISPLAY\n5.EXIT\n");
scanf("%d",&choice);
switch(choice)
case 1: push();
display();
break;
case 2: pop();
display();
break;
case 3:
palin();
break;
case 4: display();
break;
case 5: exit(0);
break;
default:printf("invalid choice\n");
break;
return 0;
void push()
int item;
if(top==(max_size-1))
else
scanf("%d",&item);
top=top+1;
stack[top]=item;
count++;
void pop()
{
if(top==-1)
else
item=stack[top];
top=top-1;
count--;
void palin()
int temp;
for(i=0,temp=count-1;i<count,temp>=0;i++,temp--)
if(stack[i]==stack[temp])
status++;
if(status>=count)
printf("palindrome");
else
printf("Not A Palindrome");
void display()
int i;
if(top==-1)
else
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
Design, Develop and Implement a menu driven Program in C for the
following
Array operations
e. Exit.
Support the program with functions for each of the above operations.
Program:
#include<stdio.h>
#include<stdlib.h>
int n,*a;
int count=0;
void create()
int i;
a=(int*)malloc(n*sizeof(int));
if(a==NULL)
exit(0);
}
printf("\n array created successfully");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
void display()
int i;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
void insert(ele,pos)
int j=n-1;
while(j>=pos)
a[j+1]=a[j];
j--;
a[pos]=ele;
n++;
void delete(pos)
int j,item;
item=a[pos];
for(j=pos;j<n-1;j++)
a[j]=a[j+1];
n--;
if(n==0)
int main()
int ch,ele,pos;
while(1)
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%d",&n);
create(n);
break;
case 2:
display();
break;
case 3:
scanf("%d",&ele);
scanf("%d",&pos);
if(pos>=0&&pos<n)
insert(ele,pos);
else
goto label;
break;
case 4:
scanf("%d",&pos);
if((pos>=0)&&(pos<n))
delete(pos);
else
goto label2;
break;
default:exit(0);
return 0;
PROGRAM-4
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
switch(symbol)
case'+':
case'-': return 2;
case'*':
case'/': return 4;
case'^':
case'$': return 5;
case'(': return 0;
default: return 8;
switch(symbol)
case'+':
case'-': return 1;
case'*':
case'/': return 3;
case'^':
case'$': return 6;
case'(': return 9;
case')': return 0;
default: return 7;
int i,j,top;
char symbol,s[30];
top=-1;
j=0;
s[++top]='#';
for(i=0;i<strlen(infix);i++)
symbol=infix[i];
while(f(s[top])>g(symbol))
postfix[j]=s[top--];
j++;
if(f(s[top])!=g(symbol))
s[++top]=symbol;
}
else
top--;
while(s[top]!='#')
postfix[j++]=s[top--];
postfix[j]='\0';
int main()
char infix[50],postfix[50];
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("%s\n",postfix);
return 0;
}
PROGRAM-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)
e. Exit
Support the program with appropriate functions for each of the above
operations
Program:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 4
int ch,front=0,rear=-1,count=0;
char q[MAXSIZE],item;
void insert()
if(count==MAXSIZE)
{
else
rear=(rear+1)%MAXSIZE;
q[rear]=item;
count++;
void delete()
if(count==0)
else
item=q[front];
front=(front+1)%MAXSIZE;
count--;
}
void display()
int i,j;
if(count==0)
else
j=front;
printf(" %c\t",q[j]);
j=(j+1)%MAXSIZE;
int main()
do
scanf("%d",&ch);
switch(ch)
case 1:
scanf(" %c",&item);
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
while(ch!=4);
return 0;
PROGRAM-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
b. Display the status of SLL and count the number of nodes in it c. Perform
Insertion / Deletion at End of SLL
e. Exit
Program:
#include<stdio.h>
#include<stdlib.h>
int MAX=4,count;
struct student
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[10];
};
NODE temp;
count=0;
temp=first;
while(temp!=NULL)
count++;
temp=temp->link;
return count;
NODE temp;
temp=(NODE)malloc(sizeof(struct student));
printf("enter usn:\n");
scanf("%s",temp->usn);
scanf("%s",temp->name);
scanf("%s",temp->branch);
scanf("%d",&temp->sem);
scanf("%s",temp->phno);
temp->link=NULL;
first=temp;
return first;
NODE temp;
if(first==NULL)
else
temp=first;
printf("\r...STUDENT DATA..\n");
while(temp!=NULL)
temp=temp->link;
{
NODE temp;
if(first==NULL)
temp=getnode(first);
first=temp;
else
temp=getnode(first);
temp->link=first;
first=temp;
return first;
if(countnodes(first)==MAX)
printf("list is full/overflow!!\n");
else
first=create(first);
return first;
NODE temp,cur;
cur=first;
if(countnodes(first)==MAX)
printf("\nlist is full:!");
else
if(first==NULL)
temp=getnode(first);
first=temp;
else
temp=getnode(first);
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return first;
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
case 1 : first=insert_front(first);
break;
case 2 : first=insert_rear(first);
break;
display(first);
return first;
NODE temp;
if(first==NULL)
printf("list is empty/underflow!!\n");
else
temp=first;
first=first->link;
free(temp);
return first;
NODE prev,cur;
cur=first;
prev=NULL;
if(first==NULL)
printf("list is empty/underflow!!\n");
return first;
if(first->link==NULL)
free(first);
return NULL;
else
while(cur->link!=NULL)
prev=cur;
cur=cur->link;
}
prev->link=NULL;
free(cur);
return first;
int ch;
while(1)
scanf("%d",&ch);
switch (ch)
case 1:first=delete_front(first);
break;
case 2:first=delete_rear(first);
break;
display(first);
return first;
}
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
case 1:first=insert_front(first);
break;
case 2:first=delete_front(first);
break;
display(first);
return first;
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
case 1:first=insert_rear(first);
break;
case 2:first=delete_front(first);
break;
display(first);
return first;
int main()
int ch,i,n;
NODE first;
first=NULL;
printf("...STUDENT DATABASE...");
while(1)
{
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=0;i<n;i++)
first=create(first);
break;
case 2:display(first);
break;
case 3:first=insert_node(first);
break;
case 4:first=deletenode(first);
break;
case 5:first=stack(first);
break;
case 6:first=queue(first);
break;
case 7:exit(0);
default:printf("invalid option\n");
return 0;
PROGRAM-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
b. Display the status of DLL and count the number of nodes in it c. Perform
Insertion and Deletion at End of DLL
f. Exit
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int MAX=4,count;
struct node
int ssn;
char name[20];
char dept[10];
char design[10];
int sal;
char phno[10];
};
NODE temp;
count=0;
temp=first;
while(temp!=NULL)
count++;
temp=temp->rlink;
return count;
NODE getnode()
{
NODE temp;
temp=(NODE)malloc(sizeof(struct node));
temp->rlink=NULL;
temp->llink=NULL;
printf("enter SSN\n");
scanf("%d",&temp->ssn);
printf("enter name\n");
scanf("%s",temp->name);
printf("enter dept\n");
scanf("%s",temp->dept);
printf("enter designation\n");
scanf("%s",temp->design);
printf("enter salary\n");
scanf("%d",&temp->sal);
printf("enter phno\n");
scanf("%s",temp->phno);
return temp;
NODE temp;
if(first==NULL)
else
{
temp=first;
printf("employee data\n");
while(temp!=NULL)
printf("\n%d\t%s\t%s\t%s\t%d\t%s",temp->ssn,temp-
>name,temp->dept,temp->design,temp->sal,temp->phno);
temp=temp->rlink;
NODE temp,cur;
if(first==NULL)
temp=getnode();
first=temp;
return first;
else
temp=getnode();
cur=first;
while(cur->rlink!=NULL)
cur=cur->rlink;
cur->rlink=temp;
temp->llink=cur;
return first;
NODE temp;
if(countnodes(first)==MAX)
printf("list is full/overflow\n");
else
if(first==NULL)
temp=getnode();
first=temp;
else
{
temp=getnode();
temp->rlink=first;
first->llink=temp;
first=temp;
return first;
if(countnodes(first)==MAX)
printf("list is full/overflow\n");
else
first=create(first);
return first;
int ch;
while(1)
{
printf("\n1.insert front\n 2.insert rear\n 3.exit\n");
scanf("%d",&ch);
switch(ch)
case 1:first=insert_front(first);
display(first);
break;
case 2:first=insert_rear(first);
display(first);
break;
return first;
NODE temp;
if(first==NULL)
printf("list is empty/underflow\n");
else if(first->rlink==NULL)
{
free(first);
first=NULL;
return first;
else
temp=first;
first=first->rlink;
first->llink=NULL;
free(temp);
return first;
NODE cur,prev;
if(first==NULL)
printf("list is empty/underflow\n");
else if(first->rlink==NULL)
free(first);
return first;
else
cur=first;
prev=NULL;
while(cur->rlink!=NULL)
prev=cur;
cur=cur->rlink;
prev->rlink=NULL;
free(cur);
return first;
int ch;
while(1)
switch(ch)
case 1:first=delete_front(first);
display(first);
break;
case 2:first=delete_rear(first);
display(first);
break;
return first;
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
case 1:first=insert_rear(first);
display(first);
break;
case 2:first=insert_front(first);
display(first);
break;
case 3:first=delete_rear(first);
display(first);
break;
case 4:first=delete_front(first);
display(first);
break;
return first;
int main()
int ch,i,n;
NODE first=NULL;
printf("\n\temployee database\n");
while(1)
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=0;i<n;i++)
first=create(first);
break;
case 2:display(first);
break;
case 3:first=insertnode(first);
break;
case 4:first=deletenode(first);
break;
case 5:first=dequeue(first);
break;
case 6:exit(0);
default:printf("invalid choice\n");
return 0;
}
PROGRAM-9
2xyz3
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
int coeff,expx,expy,expz,flag;
};
NODE getnode();
NODE getnode()
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
printf("insufficient memory\n");
exit(0);
return x;
int expx,expy,expz,coeff,ch=1;
while(ch!=0)
scanf("%d",&coeff);
scanf("%d",&expx);
scanf("%d",&expy);
printf("enter the power z\n");
scanf("%d",&expz);
head=insert_rear(coeff,expx,expy,expz,head);
scanf("%d",&ch);
return head;
NODE temp,cur;
temp=getnode();
temp->coeff=coeff;
temp->expx=expx;
temp->expy=expy;
temp->expz=expz;
temp->link=NULL;
cur=head->link;
while(cur->link!=head)
cur=cur->link;
cur->link=temp;
temp->link=head;
return head;
}
NODE temp;
if(head->link==head)
else
temp=head->link;
while(temp!=head)
printf("%dx^%dy^%dz^%d",temp->coeff,temp-
>expx,temp->expy,temp->expz);
if(temp->link!=head)
printf("+");
temp=temp->link;
printf("\n");
}
NODE add_poly(NODE head1,NODE head2,NODE head3)
NODE temp1,temp2;
int x1,x2,y1,y2,z1,z2,coeff1,coeff2,coeff;
temp1=head1->link;
while(temp1!=head1)
x1=temp1->expx;
y1=temp1->expy;
z1=temp1->expz;
coeff1=temp1->coeff;
temp2=head2->link;
while(temp2!=head2)
x2=temp2->expx;
y2=temp2->expy;
z2=temp2->expz;
coeff2=temp2->coeff;
if(x1==x2&&y1==y2&&z1==z2)
break;
temp2=temp2->link;
if(temp2!=head2)
coeff=coeff1+coeff2;
temp2->flag=1;
if(coeff!=0)
head3=insert_rear(coeff,x1,y1,z1,head3);
else
head3=insert_rear(coeff1,x1,y1,z1,head3);
temp1=temp1->link;
temp2=head2->link;
while(temp2!=head2)
if(temp2->flag==0)
head3=insert_rear(temp2->coeff,temp2->expx,temp2-
>expy,temp2->expz,head3);
temp2=temp2->link;
return head3;
NODE temp;
int x,y,z;
double result=0;
printf("enter x,y,z values\n");
scanf("%d%d%d",&x,&y,&z);
temp=head->link;
while(temp!=head)
result=result+(temp->coeff*pow(x,temp->expx)*pow(y,temp-
>expy)*pow(z,temp->expz));
temp=temp->link;
int main()
NODE head,head1,head2,head3;
int ch;
head=getnode();
head1=getnode();
head2=getnode();
head3=getnode();
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link=head3;
while(1)
{
scanf("%d",&ch);
switch(ch)
head=read_poly(head);
display(head);
evaluate(head);
break;
head1=read_poly(head1);
head2=read_poly(head2);
head3=add_poly(head1,head2,head3);
display(head1);
display(head2);
display(head3);
break;
case 3:exit(0);
default:printf("invalid choice\n");
}
}
return 0;
PROGRAM-11
b. Print all the nodes reachable from a given starting node in a digraph
using
DFS/BFS method
Program:
#include<stdio.h>
#include<stdlib.h>
int a[10][10],src,visited1[10],visited2[10],n;
void create();
void bfs(int);
void dfs(int);
void check();
int main()
int i;
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
case 1: create();
break;
scanf("%d",&src);
bfs(src);
break;
scanf("%d",&src);
dfs(src);
for(i=1;i<=n;i++)
if(visited2[i]==0)
{
printf("Graph is not connected \n");
exit(0);
break;
case 4: exit(0);
return 0;
void create()
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
}
int q[10],f=0,r=-1,i,j;
visited1[src]=1;
q[++r]=src;
while(f<=r)
i=q[f];
f++;
printf("%d\t",i);
for(j=1;j<=n;j++)
visited1[j]=1;
q[++r]=j;
for(i=1;i<=n;i++)
if(visited1[i]==0 )
{
printf("%d\t",i);
visited1[i]=0;
int j;
visited2[src]=1;
for(j=1;j<=n;j++)
printf("%d------->%d\n", src,j);
dfs(j);
PROGRAM-12
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 5
int id;
char name[20];
}EMPLOYEE;
int i;
a[i].id=0;
}
int H(int k)
return k%HASH_SIZE;
hvalue=H(id);
index=(hvalue+i)%HASH_SIZE;
if(a[index].id==0)
a[index].id=id;
strcpy(a[index].name, name);
break;
if(i==HASH_SIZE)
return 0;
return 0;
}
int search_hashtable(int key, EMPLOYEE a[])
hvalue=H(key);
index=(hvalue+i)%HASH_SIZE;
if(key==a[index].id)return 1;
if(a[index].id==0) return 0;
int i;
int main()
EMPLOYEE a[10];
char name[20];
initial_hashtable(a);
while(1)
printf("\nMENU\n1.Insert\n2.Search\n3.Display\n4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
scanf("%d", &n);
scanf("%d", &id);
scanf("%s", name);
break;
scanf("%d", &key);
flag=search_hashtable(key, a);
if(flag==0)
break;
display_hashtable(a, HASH_SIZE);
printf("\n");
break;
case 4: exit(0);
return 0;