Professional Documents
Culture Documents
DSA ASSESMENT 1
Question-1
Some priests are given three poles and a stack of 4 gold disks, each disk a little smaller than the
one beneath it. Their assignment is to transfer all 4 disks from one of the 3 pole to another with 2
important constraints. They can move only one disk at a time, and they can never place a larger
disk on top of a smaller one. Design a recursive program for the above Towers of Hanoi puzzle
using stack.
Middle Level:
Implement the problem using recursion and also trace the flow of execution [2 marks]
High Level: Implement without recursion at least for few disks [2 marks]
#include<stdio.h>
void TOH(int,char,char,char);
void main()
int n;
scanf("%d",&n);
TOH(n,'1','2','3');
if(n>0)
TOH(n-1,x,z,y);
TOH(n-1,z,y,x);
}
17BIT0076
DSA ASSESMENT 1
Output:
17BIT0076
DSA ASSESMENT 1
#include<stdio.h>
void TOH(int n)
int i;
for(i=1;i<(1<<n);i++)
int main()
int m;
scanf("%d",&m);
TOH(m);
return 0;
}
17BIT0076
DSA ASSESMENT 1
Question-2
To facilitate a thorough net surfing, any web browser has back and forward buttons that allow the
user to move backward and forward through a series of web pages. To allow the user to move both
forward and backward two stacks are employed. When the user presses the back button, the link
to the current web page is stored on a separate stack for the forward button. As the user moves
backward through a series of previous pages, the link to each page is moved in turn from the back
to the forward stack.
When the user presses the forward button, the action is the reverse of the back button. Now the
item from the forward stack is popped, and becomes the current web page. The previous web page
is pushed on the back stack. Simulate the functioning of these buttons using array implementation
of
Stack. Also provide options for displaying the contents of both the stacks whenever required.
Low Level: Implement either forward stack or backward stack [6 Marks]
Middle Level: Implement web browser navigation using both the stacks [2 Marks]
High Level: Use a single array to implement both the stacks [2 Marks]
Code -
17BIT0076
DSA ASSESMENT 1
17BIT0076
DSA ASSESMENT 1
Output -
17BIT0076
DSA ASSESMENT 1
#include<stdio.h>
#include<string.h>
char array[10][50];
{ top1++;
strcpy(array[top1],data); }
else
top2--;
strcpy(array[top2],data);
else
void pop_forw ()
if (top1 >= 0)
char popval[50];
17BIT0076
DSA ASSESMENT 1
top1--;
strcpy(popval,array[top1--]);
pushback(popval);
else
void pop_back ()
char popval[50];
strcpy(popval,array[top2++]);
pushforward(popval);
else
void print_forw ()
int i;
printf ("\n");
17BIT0076
DSA ASSESMENT 1
void print_back ()
int i;
printf ("\n");
void main()
int in,cont=0;
char n[50];
while(cont==0)
scanf("%d",&in);
switch(in)
case 2:pop_back();break;
case 3:pop_forw();break;
case
4:printf("Forward:\n");print_forw();printf("Backward:\n");print_back();break;
case 5:cont++;break;
}
17BIT0076
DSA ASSESMENT 1
17BIT0076
DSA ASSESMENT-1
Question-3
Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation).
Two-argument operators: +, -, *, /, ^ (priority from the lowest to the highest), brackets ( ).
Operands: only letters: a, b... z. Assume that there is only one RPN form (no expressions like
a*b*c).
Input:
t [the number of expressions <= 100] expression [length <= 400] [other expressions]
Output
Example
Output:
17BIT0076
DSA ASSESMENT-1
#include<stdio.h>
#include<ctype.h>
char stack[400];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 2;
if(x == '$')
return 3;
main()
char exp[400];
char *e, x;
scanf("%s",exp);
e = exp;
while(*e != '\0')
17BIT0076
DSA ASSESMENT-1
if(isalnum(*e))
printf("%c",*e);
push(*e);
printf("%c", x);
else
printf("%c",pop());
push(*e);
e++;
while(top != -1)
printf("%c",pop());