• Support PF! Buy your school textbooks, materials and every day products Here!

C tower of hanoi - Stack Implementation using structures .

  • Thread starter aashish.v
  • Start date
  • #1
13
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;
}
}
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,682
520
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.
 
  • #3
13
0
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... :)
 

Related Threads on C tower of hanoi - Stack Implementation using structures .

Replies
2
Views
6K
  • Last Post
Replies
3
Views
12K
Replies
2
Views
2K
Replies
10
Views
2K
Replies
1
Views
672
Replies
1
Views
820
  • Last Post
Replies
2
Views
25K
  • Last Post
Replies
6
Views
2K
Replies
0
Views
2K
Replies
0
Views
951
Top