MID DSA COURSE - 020 - ZRB
MID DSA COURSE - 020 - ZRB
MID DSA COURSE - 020 - ZRB
Roll #: 19014020-020
B S C S
3 rd S E M E S T E R
MID TERM
S E SS I O N 8
a) Now, consider you have 5 disks, labeled 1,2,3,4,5 where 1 is the smallest disk and 5 is
the largest disk. You need to demonstrate the process of moving disks from peg A to peg C.
To demonstrate the process, you are required to construct a table like Table 1., and
draw/write the stack at each step.
Answer:
According to statement I have 5 disks to demonstrate and construct table that is:
StackNode* Create_stack(int s)
{
StackNode* stack= new Stack;
stack->s =s;
stack->top =-1;
stack->arr = new int[s];
return stack;
}
bool CheckFull(StackNode* stack);
bool CheckEmpty(StackNode* stack);
void push(StackNode* stack, int element);
int pop(StackNode* stack);
void MoveDisc(int disc, char from_Rod, char to_Rod) ;
void MoveDisc_Helper(struct Stack *source, struct Stack*dest, char s, char d);
double pow (double A, double B);
void TowerOfHanoi(int nod, struct Stack*source, struct Stack *aux, struct Stack *dest) ;
{
if(CheckFull(stack))
return;
stack->arr[++stack->top]= element;
}
int pop(StackNode* stack){
if(CheckEmpty(stack))
return -1;
return(stack->arr[stack->top--]);
}
void MoveDisc(int disc, char from_Rod, char to_Rod)
{
int NoOfPass;
cout<<NoOfPass++<<" "<<"\nMove the disc "<<disc<<" "<<"from Peg '"<<from_Rod<<"' to Peg
'"<<to_Rod<<"'"<<endl;
}
void MoveDisc_Helper(struct Stack *source, struct Stack*dest, char s, char d)
{
int top1=pop(source);
int top2 =pop(dest);
if (top1 == -1)
{
push (source, top2);
MoveDisc(top2, d, s);
}
else if (top2 ==-1)
{
push(dest, top1);
MoveDisc(top1, s, d);
}
else if (top1 >top2)
{
push (source, top1);
push (source, top2);
MoveDisc(top2, d, s);
}
else
{
push(dest, top2);
push(dest, top1);
MoveDisc(top1, s, d);
}
}
double pow(double A , double B);
double ans=A;
int i=1;
while(i<B) {
ans = ans * A;
i++;
}
return ans;
}
void TowerOfHanoi(int nod, struct Stack*source, struct Stack *aux, struct Stack *dest)
{
char s = 'A', d = 'B',a = 'C';
if (nod % 2== 0)
{
char var =d;
d =a;
a =var;
}
int main () {
cout<<"\n************************* "<<endl;
cout<<"***** S T A R T ***** "<<endl;
cout<<"************************* "<<endl;
int n;
cout<<" NUMBER OF DISK = ";
cin>>n;
StackNode* source;
StackNode* dest;
StackNode* aux;
source =Create_stack(n);
aux =Create_stack(n);
dest =Create_stack(n);
return 0;
}
****THE END*****