jwong
jwong

Reputation: 358

C code: cannot print out the whole linked list as I expected

I've been staring at those code for so long and it still doesn't work as I expected it to. The code should just enable the user to initialize a linked list by entering the length of it and the data of every node, and it prints out all the data afterwards.

#include "stdio.h"
#include "stdlib.h"

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

int n,x,i;
Node* head = (Node*)malloc(sizeof(Node));

void Init(int x, Node* temp){
    Node* newNode = (Node*)malloc(sizeof(Node));
    temp->data = x;
    temp->next = newNode;
    temp = newNode;
}

void Print(){
    Node* temp = head;
    while(temp != NULL){
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

int main(){
    printf("Please enter the number of nodes in the linked list:\n");
    scanf("%d", &n);
    Node* temp = head;
    printf("Please enter the data of the node:\n");
    for(i=0;i<n;i++){
        scanf("%d", &x);
        Init(x,temp);
    }
    Print();
}

I think maybe there's some problem with the head node. As I run the code, the data seems to be inserted without a problem but it cannot print out everything from the very beginning from the Print() function.

This is what it looks like when I run it.

Please enter the number of nodes in the linked list:
4
Please enter the data of the node:
1
2
3
4
4 -842150451 Press any key to continue

I guess I may have missed something very basic here, sorry if I have since I'm new to learning C and data structure.

Upvotes: 1

Views: 1130

Answers (2)

ani627
ani627

Reputation: 6057

There is problem with your Init function, you need to add the new node at the end of the linked list.

Try the fixed code:


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

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

void Init(int x, Node* head)
{
    Node *temp;
    //Add the new node to end of the list
    for(temp=head;temp->next!=NULL;temp=temp->next); 
    Node* newNode = malloc(sizeof(Node));
    if(!newNode)
    {
        fprintf(stderr, "out of mem\n");
        exit(EXIT_FAILURE);
    }
    temp->data = x;
    temp->next = newNode;
    temp = newNode;
}

void Print(Node* head)
{
    Node* temp = head;
    while(temp->next != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}

int clean_stdin()
{
    while (getchar()!='\n');
    return 1;
}

int main()
{
    int n,x,i;
    int c;
    Node *head = malloc(sizeof(Node));
    if(!head)
    {
        fprintf(stderr, "out of mem\n");
        exit(EXIT_FAILURE);
    }
    head->data = 0;
    head->next = NULL;

    printf("Please enter the number of nodes in the linked list: ");
    while(scanf("%d",&n) != 1)
    {
        printf("Please enter the number of nodes in the linked list(Integer value): ");
        while(getchar() != '\n');
    }
    //Clear input buffer
    while ( (c = getchar()) != '\n' && c != EOF );

    printf("Please enter the data of the node:\n");
    for(i=0; i<n; i++)
    {
        while(scanf("%d", &x) != 1)
        {
            printf("Please input an integer\n");
            while(getchar() != '\n');
        }
        //Clear input buffer
        while ( (c = getchar()) != '\n' && c != EOF );
        Init(x,head);
    }
    Print(head);
    return(EXIT_SUCCESS);
}

Upvotes: 1

R Sahu
R Sahu

Reputation: 206567

This function

void Init(int x, Node* temp){
    Node* newNode = (Node*)malloc(sizeof(Node));
    temp->data = x;
    temp->next = newNode;
    temp = newNode;
}

doesn't change the Node in the calling function. Not only that, newNode is now a memory leak.

Your code needs a bit of re-working. Init() can be designed to add items at the end of the list or at the start of the list. You have to decide which behavior you want to implement. After that, Init needs to be reworked a bit to match the expected behavior.

Upvotes: 1

Related Questions