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

typedef struct Node {
    int data;
    struct Node* next;
} Node, *LinkedList;

void CreateList(LinkedList N, int n)
{
    N = (LinkedList)malloc(sizeof(Node));
    N->next = NULL;
    LinkedList new = N;
    Node *p;
    for (int i = 0; i < n; ++i) {
        p = (Node *)malloc(sizeof(Node));
        scanf("%d", &(p->data));
        new->next = p;
        new = p;
    }
    new->next = NULL;
}

int main()
{
    LinkedList list;
    CreateList(list, 20);
    printf("%d", list->data);
    return 0;
}

As you can see, I want to create a linkedlist and make it a function.

But when I "printf" linkedlist's data, it can't appear what i want.

Can you help me?

Dr. X
  • 2,890
  • 2
  • 15
  • 37
  • What do u want to print really? Exemplify. – Dr. X Oct 10 '17 at 08:09
  • 1
    Your problem is that the linked list `N` is created inside `CreateList`, but that the changes aren't made to `list` in `main`. Look at the many linked-list examples here for how to do it. As is, `list` is still an uninitialised pointer when you try to access it. – M Oehm Oct 10 '17 at 08:12
  • I think my linked-list has 0 to 19. And I want to print my one of my linked-lists' data, in other words i want to print a number from 0 to 19. – KashingLiu Oct 10 '17 at 08:12
  • It seems that 'N' is local, and the changes on 'N' cannot reflect on the 'list' which I create in 'function main' ??? – KashingLiu Oct 10 '17 at 08:16
  • It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Oct 10 '17 at 08:36
  • Please read and understand [the question on why not to cast the return value of `malloc()` and family in C](/q/605845). – Toby Speight Oct 10 '17 at 08:36

2 Answers2

2

The direct problem, as M. Oehm notes, is that you pass the list object to the create function. The create function creates the list, but because the list object is not returned to main, main cannot see the list. To achieve what you want, do:

In main, declare the list as:

LinkedList *N;    // a pointer

declare create as:

void CreateList(LinkedList **N, int n)    // address of a pointer that receives the value

and dereference it in create:

    *N = malloc(sizeof(Node));    // assign the value to the pointer in main

and now call it from main as:

    CreateList(&N, 20);    // pass the address of the pointer

I further note that you pass create an int, the number of elements in the list, but a list is typically made for an unknown number of elements. So you should read until end-of-file.

(All other required modifications in create I leave to you.)

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    Grumble, grumble... [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714), e.g. `*N = malloc (sizeof **N);`? – David C. Rankin Oct 10 '17 at 08:32
  • No, David, there is no need. It is often discouraged (it is a left-over from when there was no `void` and `malloc` returned `char *`). – Paul Ogilvie Oct 10 '17 at 08:49
  • I knew you knew (and it was a copy/paste), and already voted you up. It was more for the OP's benefit `:)`. – David C. Rankin Oct 10 '17 at 08:50
0

Thank you all!! I solved this problem, and this is my code.

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

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void create(Node* *head, int n)
{
    *head = malloc(sizeof(Node));
    (*head)->next = NULL;
    Node* new = *head;
    Node* p;
    for (int i = 0; i < n; ++i) {
        p = malloc(sizeof(Node));
        scanf("%d", &(p->data));
        new->next = p;
        new = p;
    }
}

int main()
{
    Node* list;
    create(&list,20);
    printf("%d", ((list->next)->next)->data);   //for test
    return 0;
}