Sie sind auf Seite 1von 6

CHANDIGARH UNIVERSITY

ASSIGNMENT – 2

Data Structures

BY – MUDIT JAIN TO – Deepakshi Mam


UID – 18BCS6529
CLASS – CSE 1(B)
Q1. Consider a standard Circular Queue 'q' implementation (which has
the same condition for Queue Full and Queue Empty) whose size is 11
and the elements of the queue are q[0], q[1], q[2].....,q[10]. The front
and rear pointers are initialized to point at q[2] . In which position will
the ninth element be added?

Ans1:-
Circular queue whose total size is 11, front and rear pointers are initialized to point at
q[2]:

Therefore, 9th element will be added at pointer q[0].

Q2. What is the postfix form of the following prefix expression -


A/B*C$DE ?

Ans 2.

The postfix expression is ABC&*/-.

A code to convert postfix form to prefix form is as below:-


#include<stdio.h>
#include<string.h>
void push(char item[],int *top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
void *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void pre_post(char prefix[],char postfix[])
{
char s[20][20];
int top,i;
char symbol,temp[2];
char *op1,*op2;
top=-1;
strrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch (symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:
push(temp,&top,s);
}
}
}

int main()
{
char prefix[20];
char postfix[20];
printf("\n\n Enter the prefix expression \n\n");
scanf("%s",prefix);
pre_post(prefix,postfix);
printf("\n\n The postfix expression is %s \n\n",postfix);
return 0;
}
3. Write a algorithm which gives solution to Towers of Hanoi Problem
for n disks. Test the algorithm using n =3.

Ans 3.

This process gives us a recurcive solution to the tower of hanoi


problem for n disks

1. Tower(n,beg,aux,end)
2. If n==1
(a)beg->end
(b)return
End of if statement
3. Move n-1 disk from reg beg to reg aux
Call tower(n-1,beg,end,aux)
4. Beg->end
5. Move n-1 disk from reg aux to reg end
Call tower(n-1,aux ,beg,end)
6. return

4. What is the output of following function for start pointing to first


node of following linked list? 1->2->3->4->5->6
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
Ans.
fun() prints alternate nodes of the given Linked List, first from
head to end, and then from end to head. If Linked List has even
number of nodes, then skips the last node.

Das könnte Ihnen auch gefallen