0

I was trying to create a single linked list in C, unfortunately something goes wrong. Here's my code:

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

int size,i;

struct list
{
    int val;
    struct list *next;
};
typedef struct list element;

void add(element *head, int value)
{
    element *current;
    current = (element*) malloc(sizeof(element));
    current->val = value;
    current->next = head;
    head = current;
}

void displayList(element *first)
{
    element *curr = first;
    while (curr != NULL)
    {
        printf("%d",curr->val);
        curr = curr->next;
    }
}

int main()
{
    element *head, *curr;
    head = NULL;

    /* This works fine, although I would like to insert this into a function add
    curr = (element*) malloc(sizeof(element));
    curr->val = 65;
    curr->next = head;
    head = curr;
    */

    add(head, 15);          ////Function add doesn't work, although the code is same as above
    displayList(head);
}

For now I would just like to add a single number to the list just to see if it works using add function. The commented part works fine and program returns '65' until I put it in the function. When I try to use add function the result is "Process returned 0 (0x0)" and nothing more. I suppose there's something wrong with passing the list's head to add function, but I can't find any mistake.

3
  • compile with all warnings and debug info (e.g. gcc -Wall -g). Then, use the debugger (e.g. gdb) Commented Mar 30, 2014 at 15:37
  • You should pass head as element **headRef so that it can be modified in the function Commented Mar 30, 2014 at 15:40
  • You need to pass head by reference into add so that you can set the new head value and have it available in main. Commented Mar 30, 2014 at 15:41

3 Answers 3

1

/*Your problem was the head pointer didn't get the updated value after adding new node, since you declare the head pointer in main and updating the local variable head in the add function */

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

int size,i;

struct list
 {
  int val;
  struct list *next; 
 };

typedef struct list element; element *head,curr;*
void add(element *h, int value) { element current; current = (element) malloc(sizeof(element)); current->val = value; current->next = h; head = current;

   }

void displayList(element *first)
{
    element *curr = first;
      while (curr != NULL)
        {
      printf("%d",curr->val);
      curr = curr->next;
        }
 }

int main()
{

head = NULL;

/* This works fine, although I would like to insert this into a function add
curr = (element*) malloc(sizeof(element));
curr->val = 65;
curr->next = head;
head = curr;
*/

add(head,15);
add(head,34);          ////Function add doesn't work, although the code is same as above
displayList(head);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Look, the head variable is the address of your first cell. So in your function you pass just the address of your first cell, but you should modify it from the function (head = curr), and there is the problem, because the function just copies this address. So instead, pass it as pointer:

void add(element **head, int value)
{
    element *current;
    current = (element*) malloc(sizeof(element));
    current->val = value;
    current->next = *head;
    *head = current;
}

add(&head, 15);

Comments

0

You should pass head as element **headRef so that it can be modified in the function. C is pass by value. So the reference to the head pointer should be passed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.