C tower of hanoi - Stack Implementation using structures .

  • Thread starter Thread starter aashish.v
  • Start date Start date
  • Tags Tags
    Structures Tower
AI Thread Summary
The discussion revolves around a code implementation of the Tower of Hanoi problem using a stack structure in C, which is encountering a segmentation fault error. The original poster seeks assistance in debugging their code, specifically questioning the allocation of multiple node instances and the initialization of tower indices that are not used later. Suggestions are made to revise the push and pop functions for better stack operation handling and to introduce a maketower function for disk initialization. Ultimately, the original poster resolves their issue independently.
aashish.v
Messages
13
Reaction score
0
I've written a code for the problem but I'm contantly getting segmentation fault, core dump error, kindly help.
I'm using gcc from ubuntu 12.04,


Here is my code...
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
struct node
{
int ind;
int ele;
struct node *next;
}*t1,*t2,*t3;
int g;
void push(struct node *,int);
int pop(struct node *);
void disp(struct node *);
void makeDisk(int n,struct node *,struct node *,struct node *);
int main()
{
int n=3;

printf("\nEnter the number of plates in tower1...");
scanf("%d",&n);
printf("\nThe moves are...");
t1=(struct node *)malloc(sizeof(struct node)*n);
t1->next=NULL;
t2=(struct node *)malloc(sizeof(struct node)*n);
t2->next=NULL;
t3=(struct node *)malloc(sizeof(struct node)*n);
t3->next=NULL;
t1->ind=1;
t2->ind=2;
t3->ind=3;
makeDisk(n,t1,t2,t3);
return 0;
}

void makeDisk(int n,struct node *t1,struct node *t2,struct node *t3)
{
if(n==1)
{
printf("\nMove from tower %d to %d...",t1->ind,t2->ind);
g=pop(t1);
push(t2,g);
}
else
{
makeDisk(n-1,t1,t3,t2);
printf("\nMove from tower %d to %d...",t1->ind,t2->ind);
g=pop(t1);
push(t2,g);
makeDisk(n-1,t3,t2,t1);
}
}


void push(struct node *head,int x)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->ele=x;
temp->next=head->next;
head->next=temp;
}

int pop(struct node *head)
{
struct node *p;
int t=0;
p=head->next;
t=p->ele;
head->next=head->next->next;
free(p);
return t;
}

void disp(struct node *head)
{
struct node *p;
p=head->next;
printf("\nThe ele are...");
while(p!=NULL)
{
printf("%d\t",p->ele);
p=p->next;
}
}
 
Physics news on Phys.org
To see your code better, use [ code ] before and [ /code ] after your code (without the spaces next to []).

I don't understand why you allocate "n" instances of node for t1, t2, and t3. I don't understand why you initialize t1->ind, t2->ind, and t3->ind without ever referring to those values again.

Stack operations:

A "push" should take a pointer to a list and a pointer to a node as parameters, then prefix the list with the node. It doesn't need to return anything.

A "pop" should take a pointer to a list as a parameter, remove the first node from the list (unless the list is emptry) and return a pointer to that node (or return NULL).

Other functions:

You probably need a maketower() function to create a list of disks on one of the tower lists.
 
rcgldr said:
To see your code better, use [ code ] before and [ /code ] after your code (without the spaces next to []).

I don't understand why you allocate "n" instances of node for t1, t2, and t3. I don't understand why you initialize t1->ind, t2->ind, and t3->ind without ever referring to those values again.

Stack operations:

A "push" should take a pointer to a list and a pointer to a node as parameters, then prefix the list with the node. It doesn't need to return anything.

A "pop" should take a pointer to a list as a parameter, remove the first node from the list (unless the list is emptry) and return a pointer to that node (or return NULL).

Other functions:

You probably need a maketower() function to create a list of disks on one of the tower lists.

Thanks for the reply... I got it figured out... :)
 

Similar threads

Replies
1
Views
10K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
1
Views
3K
Replies
1
Views
1K
Replies
3
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
Back
Top