-3

Been given some functions, but cant seem to get main method working (the master list). What i thought would happen is you 1 master list and insert_at_front would add to it, but it only prints out the first list (10). Anyone know how i can get a linked list going? Thanks in advance :)

#include <stdlib.h>
#include "week1.h"

void insert_at_front(List *self, int data)
{
    List newNode = (List)malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = *self;
    *self = newNode;

}

void print_list(List *self)
{
    List current = *self;

    while (current != NULL)
    {
        printf("%d\n", current->data);
        current = current->next;
    }
    printf("\n");
}

int main(void)
{

    List *master;


    insert_at_front(&master, 10);
    insert_at_front(&master, 20);

    print_list(&master);

    return 0;
}

header:

    typedef struct node
{ 
    int data; 
    struct node *next; 
} *List;

void print_list(List *self);
void insert_at_front(List *self, int data);
BriannaXD
  • 169
  • 2
  • 14
  • 1
    tutorial [here](http://pastebin.com/HC1DLK4M) – sp2danny Mar 22 '16 at 10:19
  • 1
    @manniL It's not a duplicate. OP is asking why the code is not doing what's expected. She is not asking for explanation on "How does a linked list work?". – muXXmit2X Mar 22 '16 at 10:41

1 Answers1

1

You typedefed List as a pointer to your struct node so the declaration of List *master is actually a pointer to a ponter to a node. When getting the address of master (&master) your getting a pointer to a pointer to a pointer to a node. Not quite what you want :)

You need to change the declaration of master to a pointer to a node and then getting the address of it

List master; // before: List* master

insert_at_front(&master, 10);
insert_at_front(&master, 20);

print_list(&master);

Edit:

Also include <stdio.h> for using printf.


At the moment you're also creating a memory leak since you're allocating memory by calling malloc but never calling free.

Usually the best thing you can do is to write a cleanup function for freeing memory right after you wrote something which allocated memory in the first place. A cleanup could look like this:

void delete_list(List* self)
{
  while ((*self)->next)
  {
    List tmp = *self;
    List last;
    while ( tmp->next != NULL)
    {
      last = tmp;
      tmp = tmp->next;
    }
    free(last->next); // delete the last node in the list
    last->next = NULL;
  }
  free(*self); // now delete the only existing node
}
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
  • thanks heaps, was a bit confusing with the pointer part, but drew a diagram and it all makes sense now! Thankyou so much :) also for the second part i thought all local variables will get deleted after the function call? (c is a call by call language? .. im probably wrong though) – BriannaXD Mar 28 '16 at 06:29
  • Also what is the point in having a pointer to a struct? stupid question, but just wondering whats better about having a pointer to the struct not just having a struct? thanks again :) – BriannaXD Mar 28 '16 at 06:31
  • @BriannaXD Only if a variable is allocated on the stack it will be automatically deleted if the end of its scope is reached. If you manually allocate memory on the heap you also need to free it, otherwise the local pointer gets deleted however there is still memory allocated on the heap without something pointing to it. Now to the pointer thing. You don't have to use pointers, however they are usefull if you need more memory than the stack provides. You can use a pointer to a chunk of memory on the heap which is way bigger than the stack. Alright? – muXXmit2X Mar 31 '16 at 06:44