Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:57:19+00:00 2026-06-06T23:57:19+00:00

I have a linked list, and I need to create a node right after

  • 0

I have a linked list, and I need to create a node right after the head..

it means I have something like this:

node *head = NULL;

and my linked list in the end should be like :

head -> node -> NULL…

but when I use a normal addNode function, it gives me a runtime error(not sure which, my debug has problems)…

this is what I wrote:

void addNode(node *head)
{
node *temp = head; // a temp to not move the head
node *newNode = (node*)malloc(sizeof(node)); // The new node

while (temp -> next != NULL)
{
    temp = temp -> next // Getting to the last node
}

temp -> next= newNode; // Adding the new node into the linked list insted of the NULL
newNode -> next = NULL; // Adding the NULL after the new node
}

This code works great to me when I have a linked list with already 1 or more nodes ,but if the linked list only has a head, it does problems to me… how can I solve the problem?

(if you didnt understand my problem – With the addNode function I wrote here, i’m getting a runtime error for adding a new node into a head that points to NULL already)..

Thanks, Amit 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:57:20+00:00Added an answer on June 6, 2026 at 11:57 pm

    You have to check if Head is null. Otherwise when you try to check

    head->next != NULL
    

    head is NULL so you are referring to random place in memory

    You can’t add node after head if head is NULL. You have to allocate memory for head and then set ‘next’ pointer. By the way why u want to set head->next while head is null?

    EDIT

    Mayby you should try add flag to nodes like bool active and set it false when you want to pass it.

    I’ll try to say it in another way. You can’t set head->next because head is NULL. NULL means, that it’s just a pointer, to nowhere. It’s a variable where u can place some address, but nothing else. If u want to have there a structure, like node, you have to place there address of new object of type Node:

    Node element = malloc(sizeof(Node));
    head = element;
    

    After that u will have in head address of Node object and u will be able to revoke to variables (like Node *next) inside this structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a Linked List from a text file that looks like
I have a singly linked list with node and list structs defined as: typedef
I have a vector of Linked list pointers. Each LinkedList has a head pointer
I have a linked list of Nodes, each Node is defined as: struct Node
I have the following struct for a linked list: struct Node { int type;
This is tailing off my other question which was successfully answered: stackoverflow.com/questions/8597929/need-to-create-li-with-list-of-different-links-using-php-explode-method so now
Suppose I have a doubly linked list. I create it as such: MyList list
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct
I have a linked list, which stores groups of settings for my application: typedef
Say you have a linked list structure in Java. It's made up of Nodes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.