0

We have a cake with n stories put on a silver plate. We have to put it on a golden plate, but the cake is too big to do this directly, so we need to use an additional plate, a bronze one.

#include <stdio.h>
#include <stdlib.h>

struct silver_plate
{
    int story;
    struct silver_plate *next;
};

struct silver_plate *head;
head=NULL;

struct golden_plate
{
    int story;
    struct golden_plate *next;
};

struct golden_plate *head1;
head1=NULL;


void push(struct silver_plate **head_ref,int story)
{
    struct silver_plate *next_node =
            (struct silver_plate*)malloc(sizeof(struct(silver_plate)));

    new_node->story=story;
    new_node->next=(*head_ref);

    (*head_ref)=new_node;
}

void move(struct golden_plate *head_ref)
{
    struct golden_plate *new_node =
            (struct golden_plate *)malloc(sizeof(struct(golden_plate)));

    new_node1->story=new_node->story;
    new_node1->next=(*head_ref1);

    (*head_ref1)=new_node1;
}

I expect the output to be a list arranged with the n stories placed on the golden plate.

kalehmann
  • 4,821
  • 6
  • 26
  • 36
Marin3l
  • 9
  • 1
  • 3
    Sounds like the [Tower of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) puzzle. – Tom Karzes May 29 '19 at 13:05
  • 2
    Is this a [Tower of Hanoi](https://stackoverflow.com/questions/1223305/tower-of-hanoi-recursive-algorithm) problem? – Weather Vane May 29 '19 at 13:05
  • So the linked list is, effectively, a stack? – Weather Vane May 29 '19 at 13:07
  • it can be used as a stack. – Marin3l May 29 '19 at 13:29
  • OT: regarding: `struct silver_plate *next_node = (struct silver_plate*)malloc(sizeof(struct(silver_plate)));` 1) The returned type is `void*` which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc 2) Always check (!=NULL) the returned value to assure the operation was successful. – user3629249 May 30 '19 at 13:41
  • in the `push()` function, there is a 'malloc()` for a pointer named `next_node` Then all the following lines are referencing `new_node` This is an error – user3629249 May 30 '19 at 13:45
  • in the `move()` function, there is a `malloc()` for a pointer named `new_node` All the following lines are referencing a `new_node1` This is an error. – user3629249 May 30 '19 at 13:48
  • when asking a question about a run-time problem, as this question is doing, always post a [mcve] so we can reproduce the problem and help you debug it. – user3629249 May 30 '19 at 13:49

0 Answers0