1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C tower of hanoi - Stack Implementation using structures .

  1. Sep 9, 2012 #1
    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;
    }
    }
     
  2. jcsd
  3. Sep 9, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  4. Sep 13, 2012 #3
    Thanks for the reply... I got it figured out... :)
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook




Loading...
Similar Threads for tower hanoi Stack Date
Heat exchanger and cooling towers design Nov 29, 2016
Enum problem with the Towers of Hanoi Apr 4, 2012
C++ Towers of Hanoi Problem Dec 4, 2011
Towers of Hanoi Fortran 90 Oct 9, 2011
Towers of hanoi heuristic Sep 22, 2007