You are on page 1of 15

17BIT0076

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.

Low Level: Implement the problem using recursion [6 marks]

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]

Program Implementation with Recursion:


17BIT0076
DSA ASSESMENT 1

#include<stdio.h>

void TOH(int,char,char,char);

void main()

int n;

printf("How many plates?");

scanf("%d",&n);

TOH(n,'1','2','3');

void TOH(int n,char x,char y,char z)

if(n>0)

TOH(n-1,x,z,y);

printf("\n%c -> %c",x,y);

TOH(n-1,z,y,x);

}
17BIT0076
DSA ASSESMENT 1

Program Implementation without Recursion:

Output:
17BIT0076
DSA ASSESMENT 1

#include<stdio.h>

void TOH(int n)

int i;

for(i=1;i<(1<<n);i++)

printf("Move from Pole %d to Pole %d\n",(i&(i-1))%3,(((i |(i-1)) +


1)%3));

int main()

int m;

printf("\n Enter the Number of Disks : ");

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

int top1 = -1;

int top2 = 10;

void pushforward (char data[50])

if (top1 < top2 - 1)

{ top1++;

strcpy(array[top1],data); }

else

printf ("Stack Overload\n");

void pushback (char data[50])

if (top1 < top2 - 1)

top2--;

strcpy(array[top2],data);

else

printf ("Stack Overload\n");

void pop_forw ()

if (top1 >= 0)

char popval[50];
17BIT0076
DSA ASSESMENT 1

top1--;

strcpy(popval,array[top1--]);

pushback(popval);

printf ("Current page - %s\n", array[top2]);

else

printf ("Stack Empty\n");

void pop_back ()

if (top2 < 10)

char popval[50];

strcpy(popval,array[top2++]);

pushforward(popval);

printf ("Current page - %s\n", array[top2]);

else

printf ("Stack Empty! Cannot Pop\n");

void print_forw ()

int i;

for (i = top1; i >= 0; --i)

printf ("%s\n", array[i]);

printf ("\n");
17BIT0076
DSA ASSESMENT 1

void print_back ()

int i;

for (i = top2; i < 10; ++i)

printf ("%s\n", array[i]);

printf ("\n");

void main()

int in,cont=0;

char n[50];

printf("Browser History Program:\nEnter 1 to Open link.\nEnter 2 to Press


Back.");

printf("\nEnter 3 to Press Forward.\nEnter 4 to Print Both Stacks.\nEnter 5


to Exit\n");

while(cont==0)

printf("Enter Choice - ");

scanf("%d",&in);

switch(in)

case 1:printf("Enter Data - ");scanf("%s",&n);pushback(n);break;

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]

Text grouped in [ ] does not appear in the input file.

Output

The expressions in RPN form, one per line.

Example

Input: 3 (a+ (b*c)) ((a+b)*(z+x)) ((a+t)*((b+ (a+c)) ^(c+d)))


Output: abc*+ ab+zx+* at+bac++cd+^*
Code:
17BIT0076
DSA ASSESMENT-1

Output:
17BIT0076
DSA ASSESMENT-1

#include<stdio.h>

#include<ctype.h>

char stack[400];

int top = -1;

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;

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

return 1;

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

return 2;

if(x == '$')

return 3;

main()

char exp[400];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')
17BIT0076
DSA ASSESMENT-1

if(isalnum(*e))

printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

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

printf("%c", x);

else

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

printf("%c",pop());

push(*e);

e++;

while(top != -1)

printf("%c",pop());