Here is a linked list code in C , It does not give expected output .
Input
NUMBER OF ELEMENTS -3
but it takes only 2 element and prints them with an extra zero.
here is the output
**ENTER THE NUMBER OF ELEMENTS
3
1
2
The list contains 0
The list contains 1
The list contains 2
**

#include<stdio.h>

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



struct list* insert(struct list* node,int data)
{
  struct list* newnode=malloc(sizeof(struct list));
  if(newnode)
    {
      newnode->data=data;
      newnode->next=NULL;
    }

  if(node)
    {
      while(node->next)
    {
      node=node->next;
    }   
  node->next=newnode;

    }
  return newnode;
}


void printlist(struct list *node)
{
  if(!node)
    {
      printf("empty list\n");
    }

  while(node)
    {
      printf("The list contains %d \n",node->data);
      node=node->next;
    }
}


int main()
{
  struct list *temp;
  temp=malloc(sizeof(struct list));
  int i,n,m;
  printf("ENTER THE NUMBER OF ELEMENTS\n");
  scanf("%d",&n);
  for(i=0;i<n;i++)
    {
      scanf("%d",&m);
      insert(temp,m);
    }

  printlist(temp);
}

Recommended Answers

All 4 Replies

I am not sure, but the problem is in the

return newnode;

statement.Because that particular code is executed every time the data is inserted.

Hope this helps you...:-D

There are a number of issues including:

  • The list in main is never updated.
  • Your only reference to the list in insert is lost during the loop.
  • A null list isn't properly accounted for in insert.

Consider the following changes:

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

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

struct list* insert(struct list* node, int data)
{
    struct list* newnode = malloc(sizeof(struct list));

    if (newnode)
    {
        newnode->data = data;
        newnode->next = NULL;
    }

    if (!node)
    {
        node = newnode;
    }
    else
    {
        struct list* temp = node;

        while (temp->next)
        {
            temp = temp->next;
        }

        temp->next = newnode;
    }

    return node;
}

void printlist(struct list *node)
{
    if (!node)
    {
        printf("empty list\n");
    }

    while (node)
    {
        printf("The list contains %d \n", node->data);
        node = node->next;
    }
}

int main(void)
{
    struct list *temp = NULL;
    int i, n, m;

    printf("ENTER THE NUMBER OF ELEMENTS\n");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        scanf("%d", &m);
        temp = insert(temp, m);
    }

    printlist(temp);

    return 0;
}
commented: Could you please explain me how is the insert function working here? :) +0

Could you please explain me how is the insert function working here? :)

A new node is created. If the node passed in is NULL then replace it. Otherwise, using a temporary pointer, walk to the end of the list and tack on the new node there. Finally, return the node that was passed in, which as far as insert is concerned, is the head of the list.

The key is actually saving the result of insert back to your list head, otherwise you can't pass a null pointer in as the first argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.