Вы находитесь на странице: 1из 25

MUSKAN DHINGRA

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;

struct node *next;

};

struct node *prev,*head,*headL1,*headL2,*p,*newnode,*p1;

int n,i;

char value,d[10];

void insertbeg(struct node **headL)

if(headL==NULL)
*headL=newnode;

else

p1=*headL;

*headL=newnode;

newnode->next=p1;

int main()

int flag=1;

head=NULL;headL1=NULL;headL2=NULL;

printf("Enter the string\n");

fflush(stdin);

scanf("%s",d);

printf("string is %s",d);

n=strlen(d);

//Pushing into Stack

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;

//Popping from Stack

while(p!=NULL)

printf("%c --> ",p->data);

if(p->data!=d[i++])

flag=0;

p=p->next;

if(flag==1)

printf("\n Palindrome");

else

printf("\n Not a Palindrome");

}
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

a. Check whether the parenthesis in the given expression is balanced or not

b. If it is not balanced, print the appropriate message

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];

void push(char data)

//printf("\nPush %c\n",data);

if(top!=20)

st[++top]=data;

if(top==10)

printf("\n Exceeded the limit of stack");

exit(0);

char pop()
{

if(top>-1)

return(st[top--]);

void push_value(int data)

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;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

if(x=='^')
return 3;

char* infixtopostfix(char *inf)

char *post;

char *e, x;

int i=0;

e = inf;

strcpy(post,"");

while(*e != '\0')

if(isalnum(*e))

post[i++]=*e;

else if(*e == '(')

push(*e);

else if(*e == ')')

while((x = pop()) != '(')

post[i++]=x;

else

while(priority(st[top]) >= priority(*e))

post[i++]=pop();

push(*e);

}
e++;

while(top != -1)

post[i++]=pop();

//printf("\n length %d %d Postfix Expression: %s\n",i,strlen(post),post);

return(post);

int evaluation(char* eval)

char *e;

int value;

e=eval;

while(*e!='\0')

if(isalpha(*e))

printf("Enter the value of %c \n",*e);

scanf("%d",&value);

push_value(value);

else
{

int val1 = pop_value();

int val2 = pop_value();

switch (*e)

case '+': push_value(val2 + val1); break;

case '-': push_value(val2 - val1); break;

case '*': push_value(val2 * val1); break;

case '/': push_value(val2/val1); break;

e++;

return pop_value();

main()

int i,n,m,p;

char exp[10];

char s,ps[20];

printf("Enter the expression\n");

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));

printf("Postfix Notation2: %s",ps);

printf("\n Evaluation %d",evaluation(ps));

}
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.

b. To remove the customer details, when they vacate the room.

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;

struct room *next;

};

int rooms[10]={11,22,33,44,55,66,77,88,99,10};

char status[10];

struct room *head,*p,*newnode,*p1;


int option,n=10,i,totrooms=0;

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

printf("\n Room Allotment Details\n");

p=head;

while(p!=NULL)

printf("Id: %d, Name: %s, RoomNo: %d ==> ",p->id,p->cname,p->roomno);

p=p->next;

}
}

void addcustomer()

int nc,id,k;

char name[20];

printf("\n Enter no of customers to be added: ");

scanf("%d",&nc);

head=NULL;

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

printf("\n Enter Customer Id and Names");

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)

printf("\nRoom Not Available\n");

else

insertbeg();

void custdetails()

int cid;

printf("\nEnter the customer Id\n");

scanf("%d",&cid);

p=head;

while(p->id!=cid)

p=p->next;

if(p->id==cid)

if(option==4)

printf("\nCustomer Id: %d, Customer Name: %s, Room no %d \n",p->id,p-


>cname,p->roomno);

else

printf("\nCustomer %s stays in Room no %d \n",p->cname,p->roomno);

else
printf("\nCustomer Id is invalid\n");

void vacate()

int cid;

printf("\nEnter the customer Id\n");

scanf("%d",&cid);

p=head;

while(p->next!=NULL)

if(p->next->id==cid)

printf("\nCustomer %d %s vacated the room no %d \n",p->next->id,p->next-


>cname,p->next->roomno);

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)

printf("\n 1. Customer room booking\n");

printf("2. Customer Vacating room\n");

printf("3. No of rooms available\n");

printf("4. Customer Details\n");

printf("5.Customer- Key mapping\n");

printf("\nEnter your option ");

scanf("%d",&option);

switch(option)

case 1: {addcustomer(); display();break;}

case 2: {vacate(); break; }

case 3: printf("\nNo of rooms Available %d",(10-totrooms));break;

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)

b. Print the names that start with the letter ‘M’

c. Find whether the name “Guna” is present in the list.

d. If found, replace that name with “Harsh”

e. Print the names that appear before “Harsh” starting from the closest name. (ie. Darshan, Banu,
Aakash)

f. Print the list

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct node

char cname[15];

struct node *next;

struct node *prev;

};

struct node *head,*temp,*newnode,*temp1,*p,*temp2,*temp4;

char value,d[10];

void insertbeg(char cnam[15])

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)

printf(" %s-> ",p->cname);

p=p->next;

}
void sort()

int i, j;

char x[15];

temp2 = head;

temp4 = head;

if (temp2 == NULL)

printf("\n List empty to sort");

return;

for (temp2 = head; temp2 != NULL; temp2 = temp2->next)

for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)

if (strcmp(temp2->cname,temp4->cname)>0)

strcpy(x,temp2->cname);

strcpy(temp2->cname,temp4->cname);

strcpy(temp4->cname,x);

}
}

void replace(char str1[15],char str2[15])

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

printf("\nSorted Doubly Linked List\n");

display();

replace("Guna","Harsh");

printf("\n \n List after replacement\n");

display();

printf("\n \nPrinting Names before Harsh using backward pointer\n");

disp();

Вам также может понравиться