Program Freezes When Inserting A New Node To A Linked List

Anfernee

I've made a function to insert a node to a linked list while preserving the list. However, after inserting 2 elements, if the next element is higher than all the others, the program seems to loop infinitely. My conditions show allow the program to break the loop if something happens but it doesn't;

Here's the function. Note that head is a pointer to the first element in the list and is NULL if the list is empty:

//defines the node structure
typedef struct node{

int n;
struct node* next;
}node;

bool insert_node(int value)
{
int t=0;


//If list is empty
if (head==NULL){
    node* first = malloc(sizeof(node));

    //error checking
    if (first==NULL)
        return false;

    head = first;
    first->n=value;
    first->next=NULL;
    t=1;
}

else{
    node* body=malloc(sizeof(node));
    if (body==NULL){
        t=9;
        return false;
        }

    body->n=value;
    node* ptr=head;
    node*pre=head;

    //putting new node into list
    while(t==0){
        //error checking
        if (ptr==NULL)
            break;

        //insertion
        else if (value<=ptr->n||ptr->next==NULL){
            body->next=ptr;
            pre->next=body;
            t=1;
        }

        //next node
        else if(value>ptr->n){
            pre=ptr;
            ptr=ptr->next;
        }

        //If all goes wrong
        else 
            t=9; //breaks loop
        }
    }

if (t==1)
    return true;

return false;
}
Brad Budlong

You need to handle the case where the entry that you are adding needs to be at the head of the list. Right now you only add to the head when the list is empty. That is handled by changing the if as follows:

if (head==NULL || value < head->n){
    node* first = malloc(sizeof(node));

    //error checking
    if (first==NULL)
        return false;

    first->next=head;
    first->n=value;
    head = first;
    t=1;
}

Now that the addition to the beginning of the list is taken care of, the else needs a change to get pre and ptr initialized correctly and the insertion condition needs to be changed as follows:

node* ptr=head->next;
node* pre=head;

//putting new node into list
while(t==0){
    //insertion
    if (ptr==NULL || value<=ptr->n){
        body->next=ptr;
        pre->next=body;
        t=1;
    }

    //next node
    else if(value>ptr->n){
        pre=ptr;
        ptr=ptr->next;
    }

    //If all goes wrong
    else 
        t=9; //breaks loop
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linked list - Inserting in middle, linking new node

From Dev

Inserting Node into a Linked List

From Dev

Inserting a new node into a doubly linked list after a given node

From Dev

inserting node to linked list in c

From Dev

Inserting Node in a Sorted linked list

From Dev

inserting node at beginning of linked list

From Dev

Inserting a node at the end of linked list

From Dev

Inserting a node at the end of a linked list

From Dev

inserting node at beginning of linked list

From Dev

Inserting a node in the end of a linked list

From Dev

Program inserting an element in single linked list

From Dev

Inserting a node in a pre sorted linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

Singly linked list in C(Inserting a node)

From Dev

Inserting a node at the end of linked list, .exe crashes

From Dev

Inserting a node at the end of a doubly linked list

From Dev

Inserting a node into a sorted doubly linked list

From Dev

inserting a node at the end of linked list recursivly

From Dev

Inserting a node at a specific position in a Linked list

From Dev

Inserting node at beginning of singly linked list

From Dev

Python Linked list new node

From Dev

Inserting a node before a given node in doubly linked list

From Dev

Unexpected value after inserting node at the end of linked list

From Dev

Inserting Into Linked List Python

From Dev

Inserting linked list at the end

From Dev

Program crashes when trying to insert new nodes to linked list. Why?

From Dev

Create new Node for Singly Linked List in Java

From Dev

Adding new Node to a linked list with two classes

From Dev

Adding a new node to the end of a linked list in C

Related Related

HotTag

Archive