Professional Documents
Culture Documents
Da1-Dsa 19bce1148
Da1-Dsa 19bce1148
19BCE1148
DATA STRUCTURES AND ALGORITHMS
DIGITAL ASSIGNMENT-1
1. A palindrome is a string that can be read backward and forward with the same result. Example :
Malayalam Write a C program to test whether the given string is a palindrome using linked list
implementation of stack.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
char data;
};
int n,i;
char value,d[10];
if(headL==NULL)
*headL=newnode;
else
p1=*headL;
*headL=newnode;
newnode->next=p1;
int main()
int flag=1;
head=NULL;headL1=NULL;headL2=NULL;
fflush(stdin);
scanf("%s",d);
printf("string is %s",d);
n=strlen(d);
for(i=0;i<n;i++)
newnode=malloc(sizeof(struct node));
newnode->data=d[i];
newnode->next=NULL;
insertbeg(&head);
printf("Display");
p=head;
i=0;
while(p!=NULL)
if(p->data!=d[i++])
flag=0;
p=p->next;
if(flag==1)
printf("\n Palindrome");
else
}
2. Assume that you are given the following infix expressions. Read the values from the user. i. (a -b
) + (c –d / e) + f )) ii.(a * b + c) –((d / e) + f ) Develop appropriate modules in C to
c. If the expression is balanced, convert the given expression into its equivalent postfix expression.
d. Evaluate the postfix expression.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int top=-1,top1=-1;
char st[20];
int stackint[20];
//printf("\nPush %c\n",data);
if(top!=20)
st[++top]=data;
if(top==10)
exit(0);
char pop()
{
if(top>-1)
return(st[top--]);
if(top1!=20)
stackint[++top1]=data;
int pop_value()
//printf("\nPop %c\n",st[top]);
return(stackint[top1--]);
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
if(x=='^')
return 3;
char *post;
char *e, x;
int i=0;
e = inf;
strcpy(post,"");
while(*e != '\0')
if(isalnum(*e))
post[i++]=*e;
push(*e);
post[i++]=x;
else
post[i++]=pop();
push(*e);
}
e++;
while(top != -1)
post[i++]=pop();
return(post);
char *e;
int value;
e=eval;
while(*e!='\0')
if(isalpha(*e))
scanf("%d",&value);
push_value(value);
else
{
switch (*e)
e++;
return pop_value();
main()
int i,n,m,p;
char exp[10];
char s,ps[20];
gets(exp);
printf("Expression %s - ",exp);
i=0;
n=strlen(exp);
while(i<n)
if(exp[i]=='(')
push(exp[i]);
else if (exp[i]==')')
if(top==-1)
printf("Unbalanced Parantheses\n");
exit(0);
else
s=pop();
i++;
printf("Balanced Parantheses\n");
strcpy(ps,infixtopostfix(exp));
}
3. Assume that, the receptionist at a hotel asks the name of the person with their ID number (as
given in their ID proof) when they arrive, and maintains this information. The customers are asked
to wait for the room allotment. Once the room is allotted, it is handed over to the customer after
adding this key number(XX) and status (‘Not Available’) in their record. As they have only 10
rooms, she can allot a room, only if it is vacant (status=’Available’) or when a key is returned. Use
a suitable data structure to assist the receptionist to regulate them. She has to do the following.
a. To add the customer ID along with their names, room key number and status in the list.
c. To inform the manager about the number of rooms occupied at a particular time
d. To inform the manager about the person who is staying in the room number XX.
e. To give the room key to the customer when he comes back from outing by asking his ID
number.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct room
int id;
char cname[10];
int roomno;
};
int rooms[10]={11,22,33,44,55,66,77,88,99,10};
char status[10];
char value,d[10];
void insertbeg()
//printf("Insert node\n");
if(head==NULL)
head=newnode;
else
p1=head;
head=newnode;
newnode->next=p1;
void display()
p=head;
while(p!=NULL)
p=p->next;
}
}
void addcustomer()
int nc,id,k;
char name[20];
scanf("%d",&nc);
head=NULL;
for(i=0;i<nc;i++)
scanf("%d",&id);
gets(name);
newnode=malloc(sizeof(struct room));
newnode->id=id;
strcpy(newnode->cname,name);
for(k=0;k<10;k++)
if(status[k]=='V')
newnode->roomno=rooms[k];
status[k]='O';
totrooms+=1;
break;
}
}
newnode->next=NULL;
if(k==10)
else
insertbeg();
void custdetails()
int cid;
scanf("%d",&cid);
p=head;
while(p->id!=cid)
p=p->next;
if(p->id==cid)
if(option==4)
else
else
printf("\nCustomer Id is invalid\n");
void vacate()
int cid;
scanf("%d",&cid);
p=head;
while(p->next!=NULL)
if(p->next->id==cid)
totrooms+=1;
if(p->next->next!=NULL)
p->next=p->next->next;
else
p->next=NULL;
break;
p=p->next;
display();
}
int main()
int i;
for(i=0;i<n;i++)
status[i]='V';
while(1)
scanf("%d",&option);
switch(option)
case 4: { custdetails();break;}
case 5: { custdetails();break;}
default: exit(0);break;
}
}
}
4. Implement a doubly linked list and add the following names into the list: Aakash, Banu,
Darshan, Guna, Kevin, Maneesh, Manohar. Perform the following functions.
a. Print the content of the list in the alphabetical order (use forward link)
e. Print the names that appear before “Harsh” starting from the closest name. (ie. Darshan, Banu,
Aakash)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
char cname[15];
};
char value,d[10];
newnode=malloc(sizeof(struct node));
strcpy(newnode->cname,cnam);
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=newnode;
else
newnode->next=head;
head->prev=newnode;
head=newnode;
void display()
p=head;
while(p!=NULL)
p=p->next;
}
void sort()
int i, j;
char x[15];
temp2 = head;
temp4 = head;
if (temp2 == NULL)
return;
if (strcmp(temp2->cname,temp4->cname)>0)
strcpy(x,temp2->cname);
strcpy(temp2->cname,temp4->cname);
strcpy(temp4->cname,x);
}
}
p=head;
while(p!=NULL)
if(strcmp(p->cname,str1)==0)
strcpy(p->cname,str2);break;
p=p->next;
void disp()
p=head;
while(p!=NULL)
if(strcmp(p->cname,"Harsh")==0)
break;
p=p->next;
do
printf("%s->", p->prev->cname);
p=p->prev;
}while(p!=head);
int main()
int i;
head=NULL;
insertbeg("Aakash");insertbeg("Banu");insertbeg("Darshan");insertbeg("Guna");
insertbeg("Kevin");insertbeg("Maneesh");insertbeg("Manohar");
sort();
display();
replace("Guna","Harsh");
display();
disp();