C tower of hanoi - Stack Implementation using structures .

In summary, the speakers discuss a code for a problem involving towers and disks. The code causes a segmentation fault and the speakers are seeking help to fix it. The code includes functions for stack operations and creating a list of disks. They mention that they have figured out the issue.
  • #1
aashish.v
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;
}
}
 
Physics news on Phys.org
  • #2
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
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... :)
 

FAQ: C tower of hanoi - Stack Implementation using structures .

What is the C Tower of Hanoi?

The Tower of Hanoi is a mathematical puzzle consisting of three rods and a number of disks of different sizes which can slide onto any rod. The objective of the puzzle is to move the entire stack of disks from one rod to another, following the rules that only one disk can be moved at a time and a larger disk cannot be placed on top of a smaller disk.

How is the Tower of Hanoi implemented using structures in C?

In C, the Tower of Hanoi can be implemented using structures to represent the three rods and an array to store the disks. Each disk is represented by a structure containing its size and the rod it is currently on. The disk is then moved by changing its rod value and updating the array accordingly.

What are the advantages of using structures for the Tower of Hanoi implementation in C?

Using structures allows for a more organized and efficient implementation of the Tower of Hanoi in C. It also allows for easier manipulation of the disks and their positions on the rods. Additionally, using structures can make the code more readable and easier to understand for others.

What are the limitations of using structures for the Tower of Hanoi implementation in C?

One limitation of using structures is that it may not be the most memory-efficient approach, especially for larger numbers of disks. The array used to store the disks can also become cumbersome to manage if the number of disks is too large. Additionally, using structures for the Tower of Hanoi may not be the most efficient solution in terms of time complexity.

How can the Tower of Hanoi implementation using structures in C be optimized?

One way to optimize the Tower of Hanoi implementation using structures in C is to use recursion. This allows for a more elegant solution and can reduce the amount of code needed. Additionally, using a different data structure, such as a linked list, can also improve the efficiency of the implementation.

Similar threads

Replies
1
Views
9K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
12
Views
9K
Replies
8
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Back
Top