C tower of hanoi - Stack Implementation using structures .

  • Thread starter Thread starter aashish.v
  • Start date Start date
  • Tags Tags
    Structures Tower
Click For Summary
SUMMARY

The discussion focuses on a C implementation of the Tower of Hanoi problem using a stack structure. The user encountered a segmentation fault while using GCC on Ubuntu 12.04 due to improper memory allocation and management in their code. Key issues identified include unnecessary allocation of multiple node instances for the towers and the lack of a function to initialize the disks on the towers. The user successfully resolved their issues after receiving feedback on their code structure and logic.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of data structures, specifically linked lists
  • Familiarity with stack operations (push and pop)
  • Experience with memory management in C (malloc and free)
NEXT STEPS
  • Implement a maketower() function to initialize the disks on a tower
  • Refactor the push() function to accept a pointer to a node
  • Enhance the pop() function to handle empty lists gracefully
  • Explore debugging techniques for segmentation faults in C
USEFUL FOR

C programmers, computer science students, and software developers interested in data structures and algorithm implementations, particularly those working with stack-based solutions.

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 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K