Hello, the below code for the Towers of Hanoi problem was taken from class notes
ID: 3621099 • Letter: H
Question
Hello, the below code for the Towers of Hanoi problem was taken from class notes. We were given the recursive and non-recursive solutions to the problem along with a solution that uses stacks.For the stacks solution, I tried copying the code from various slides and then running it, but I had a few problems.
-> My compiler (DEV-C++) doesn't recognize "return TRUE" or "return FALSE". I'm not sure what to replace the TRUE and FALSE with..
Here is the code (it may be missing parts for all I know, but this is all that was in the notes):
#include<stdio.h>
#include<stdlib.h>
#define DEFAULT_SIZE 7
struct stack{
int top;
int elements[DEFAULT_SIZE];
};
typedef struct stack stack;
//Function prototypes
stack init_stack(stack s, char *name);
int push(stack *s,unsigned int i);
int pop(stack *s);
int isempty(stack s);
int isfull(stack s);
//Main function*************************************************************************
void
towersOfHanoi(int n, stack *src, stack *aux, stack *dst) {
if( n > 0 ) {
towersOfHanoi(n-1,src,dst,aux);
push(dst, pop(src));
print_state();
towersOfHanoi(n-1,aux,src,dst);
}
}
//Functions******************************************************************************
stack
init_stack(stack s, char *name) {
s.top = 0;
return s;
}
void
dispose(stack s){
free(&s.elements);
free(&s);
}
int
push(stack *s, unsigned int i) {
if(isfull(*s)){
printf("Warning: Push on full stack ");
return FALSE;
}
s->elements[s->top] = i;
s->top++;
return TRUE;
}
unsigned int
pop(stack *s) {
if(isempty(*s))
printf("Warning: Pop from empty stack ");
s->top--;
return s->elements[s->top];
int
isempty(stack s){
if(s.top == 0)
return TRUE;
else
return FALSE;
}
int
isfull(stack s){
if(s.top < DEFAULT_SIZE - 1){
return FALSE;
}
else
return TRUE;
I would appreciate any help with editing the code so that it runs.
Thanks
Explanation / Answer
You are missing a "main" function (the function that tells the program where to enter), as well as the "print_state()" function. I have altered the program to a point where it will compile and execute, but it will not print anything because I do not have "print_state" (I made a blank function) as well as the implementation of calling the function "towersOfHanoi" (which would go in the blank "main" function that I created). Here is the code (let me know if you need anything further): #include #include #define DEFAULT_SIZE 7 struct stack{ int top; int elements[DEFAULT_SIZE]; }; typedef struct stack stack; //Function prototypes stack init_stack(stack s, char *name); int push(stack *s,unsigned int i); unsigned int pop(stack *s); int isempty(stack s); int isfull(stack s); void print_state(); //Main function************************************************************************* void main() { } void towersOfHanoi(int n, stack *src, stack *aux, stack *dst) { if( n > 0 ) { towersOfHanoi(n-1,src,dst,aux); push(dst, pop(src)); print_state(); towersOfHanoi(n-1,aux,src,dst); } } //Functions****************************************************************************** void print_state() { } stack init_stack(stack s, char *name) { s.top = 0; return s; } void dispose(stack s){ free(&s.elements); free(&s); } int push(stack *s, unsigned int i) { if(isfull(*s)){ printf("Warning: Push on full stack "); return false; } s->elements[s->top] = i; s->top++; return true; } unsigned int pop(stack *s) { if(isempty(*s)) printf("Warning: Pop from empty stack "); s->top--; return s->elements[s->top]; } int isempty(stack s){ if(s.top == 0) return true; else return false; } int isfull(stack s){ if(s.topRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.