Home > other >  While running my linked list code the compiler does not give any outputs after giving a print functi
While running my linked list code the compiler does not give any outputs after giving a print functi

Time:01-13

I have given insert and a print function to insert data in a linked list and then print it. But somehow it does not give any output and keeps running for a infinite time. What is wrong?

Here is the code I have written. This is a simple program to create a linked list using loops and functions.

#include<iostream>
using namespace std;
struct node{
    int data;
    struct node* next;
};
struct node* head;

void insert(int data){
    struct node* temphead=head;
    if (temphead == NULL)
    {    
        node* temp = new node();
        temp->data=data;
        temp->next=NULL;
        while (temphead == NULL){
            head==temp;
        }
    }
    
    else if (temphead != NULL)
    {
        node* temp = new node();
        temp->data=data;
        temp->next=NULL;
        while (temphead != NULL)
        {
            temphead->next= temp;
            temphead=temphead->next;
        }
    }
}

void print(){
    struct node* tempptr = head;
    while (tempptr->next != NULL)
    {
        cout<<tempptr->data<<"_";
        tempptr=tempptr->next;
    }
    
}

int main(){
    head=NULL;

    insert(2);
    insert(4);
    insert(8);
    insert(6);
    //list - 2_4_8_6
    print();
    return 0;
}

CodePudding user response:

There were few bugs in your code and also typos. Please read the comments marked with // CHANGE HERE for the description of the changes I did:

#include <iostream>
using namespace std;
struct node{
    int data;
    struct node* next;
};
struct node* head;

void insert(int data){
    struct node* temphead = head;
    if (temphead == nullptr)
    {    
        node* temp = new node();
        temp->data = data;
        temp->next = nullptr;
        // CHANGE HERE: removed unnecessary while loop
        // Directly assign temp to head
        head = temp;
    }
    else
    {
        node* temp = new node();
        temp->data=data;
        temp->next=nullptr;
        // CHANGE HERE: check for temphead->next instead of temphead
        while (temphead->next != nullptr)
        {
            // CHANGE HERE: remove unnecessary line: temphead->next= temp;
            temphead=temphead->next;
        }
        // CHANGE HERE: assign temp to temphead->next (i.e. to last node)
        temphead->next = temp;
    }
}

void print(){
    struct node* tempptr = head;
    // CHANGE HERE: check for tempptr instead of tempptr->next
    while (tempptr != nullptr)
    {
        cout<<tempptr->data<<"_";
        tempptr=tempptr->next;
    }
    
}

int main(){
    head=nullptr;

    insert(2);
    insert(4);
    insert(8);
    insert(6);
    //list - 2_4_8_6
    print();
    return 0;
}

NOTE: Your code uses new for dynamic memory allocation but doesn't use delete to de-allocate the memory when not required. If you want to avoid using new/delete, you can explore about smart pointers.

  •  Tags:  
  • Related