Storing more than one data item in single linked list

9.6k views Asked by At

I am trying to store more than one data item in single linked list. I had tried with 2 data item , program complies with no error as, inserting data also work fine but program stop while printing output. I can't figure out whats wrong with code. Any help will be greatly appreciated.

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

struct node{
    float a;
    float b;
    struct node *next;
};

struct node *head;

void insert(float x,float y){
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->a = x;
    temp->b = y;
    temp->next = head;
    head= temp;
}
void print(){
    struct node *temp;
    temp= head;
    printf("\n the linked list is :");
    while(head!=NULL){
        printf("data is : %f %f",temp->a,temp->b);
        temp=temp->next;
    }
}

int main()
{
    int n,i,x,y;
    head = NULL;
    struct node *temp;
    printf("\n enter the  number of data to enter:");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        printf("\n enter x-cordinate: \n");
        scanf("%f",&x);
        printf("\n eter y-cordinate: \n");
        scanf("%f",&y);
        insert(x,y);
    }
    print();
    return 0;
}
6

There are 6 answers

0
Weather Vane On

You have a mistake in the print() function

while (head!=NULL)

should be

while (temp!=NULL)
0
Caleb An On

It's because instead of checking if temp is null in your while loop, you check for head. Change that to temp and it will not be stuck in a loop.

while(head!=NULL) 

should be

while(temp!=NULL)
0
Daniel Jour On

In your print function, in the while loop you need to check for temp != NULL because that's the pointer you use to traverse the list.

Apart from that I'd advice against using a global variable for your list. Make the head pointer a parameter of your functions.

For clarity I'd also rename temp to something more meaningful, like iterator, at least in your print function.

And as BLUEPIXY pointed out you need to change the type of some variables inside main to reflect what you want scanf to read.

0
dbush On

Two issues:

  1. You're reading in x and y as float, but the variables are of type int. Since a float is larger than an int, memory gets written to that shouldn't, resulting in undefined behavior.
  2. When printing the results, you're checking for head!=NULL instead of temp!=NULL.
0
gaurav gupta On

Adding to the above answers, you could also look to fix 2 others lines in your code:-

  1. Initialize pointer head to null. You should initialize the all pointers before using them otherwise the behavior will be unexpected.

    struct node *head = NULL;

  2. You should check the return value of amlloc in the function insert(). malloc() could return NULL, in case the memory allocation fails. In event when malloc() returns NULL, dereferencing a NULL pointer will cause the program to crash. You could change insert() like following.

    void insert(float x,float y) {

    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));
    
    if (!temp) {
        return;
    }
    
    temp->a = x;
    temp->b = y;
    temp->next = head;
    head= temp;
    

    }

1
user9024163 On

Above program one minor mistake is there, inside the void print function ,we should write while expression is while(temp!=NULL).

Balaji Kumar 29/11