Lukas M.
Lukas M.

Reputation: 163

linked list with function

I'm trying to create a program which creates and display linked list.

Now i'm having trouble with my create_list() function, it doesn't create any list.

What i'm doing wrong ?

Sorry for bad english :/

CODE :

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

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

int main(){

     node *start;
     start = NULL;
     int a,n,on = 1;

     while(on == 1){
        printf(" \n choose: \n 1 --- create list \n 2 --- display list \n");
        scanf("%d",&n);
        switch(n){
            case 1:
                printf("-------------------------------------------- \n");
                printf(" Enter the elements. The last element is 0 \n");
                printf("-------------------------------------------- \n");

                Create_list();
                Display_list(start);
                break;

            case 2:
                 Display_list(start);
                break;
        }
    }

    system("pause");
    return 0;
}

void Display_list(node *curr){
    if(curr){
        while (curr->next != NULL){
                  printf("%d \n",curr->data);
                   curr=curr->next;
        }
    } else {
        printf(" \n The list is not created ! \n");
    }
}

void Create_list(node *curr){

    int i;
    node *start = NULL;



    if (start == NULL){
        curr = (node *)malloc(sizeof(node));
        start=curr;

       while ( i != 0){
            scanf("%d",&i);
            if(i == 0){
                curr->next=NULL;
                curr=start;
            } else {
                curr->data=i;
                curr->next=(node *)malloc(sizeof(node));
                curr=curr->next;
            }
        }

    } else {
          printf(" \n list already exists ! \n");
    }
}                     

Upvotes: 0

Views: 14941

Answers (2)

SureshS
SureshS

Reputation: 609

The function Create_List(node *curr) needs some arguments. You are not passing any arguments from main(). Did your code compile?

The function Create_List(node *curr) needs some arguments. You are not passing any arguments from main(). Did your code compile?

What you should do is take a node in main which will store location of first node of the linked list.

void Insert(struct node **q, int num) //Num is the data to be added and **q is the pointer to the first node of the list.
{
struct node *temp, *r;
temp = *q;
if (*q == NULL) {
    temp = ((struct node *)malloc(sizeof(struct node)));
    temp->data = num;
    temp->link = NULL;
    *q = temp;
}
else    {
    while (temp->link != NULL)
        temp = temp->link;

    r = ((struct node *)malloc(sizeof(struct node)));
    r->data = num;
    r->link = NULL;
    temp->link = r;
}
}

Upvotes: 1

cHao
cHao

Reputation: 86505

The start in Create_list is not related to the start in main. Since both are local to their respective functions, one can't even see the other. So setting start doesn't actually set start, if you will. :P

You'll need to either bring start outside of the functions and make it global, or pass &start (as a node**) from main into Create_list and modify *start to set the list head. (The latter is generally preferable, as globals are often trouble waiting to happen.)

Upvotes: 0

Related Questions