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 6688799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:42+00:00 2026-05-26T05:26:42+00:00

I’ve seen a few topics about linked lists, and I’ve read enough to glean

  • 0

I’ve seen a few topics about linked lists, and I’ve read enough to glean how to do it generally, but I want to make sure I’m not wrongly interpreting the information I’ve gathered.

Let’s say I have:

struct element 
{
    int       val;
    element   *next;
};

The element *next is a pointer to an element, which will be used to connect the elements together in order to form the linked list.

I’ve also gathered that you need to have some kind of “tie off” so you don’t lose your list. My professor describes it as a trail of balloons, and if you dont have a “tie off” your list can get lost.

So I start off with creating the “tie off” pointer:

element first;
first -> *next = 0; // or null

This is where I get lost… How do I add to the head of a linked list? Ordering it isn’t important at this point, I just need to start with an unordered list, I’ll get more complicated later.

Something like this?

element addMe;
addMe -> val = 100;

first -> next = *addMe;

Is that right?

What would I do to add something to a non-empty list?

Thanks for the time!

Edit: This is not homework. We have gone over linked lists, and have done an assignment with them. I did not get a very good grade on the assignment, so I am trying to strengthen my understanding before the next assignment. We will use linked lists again.

Edit2: Thanks!

I do not know if this would be considered “homework” or not. I personally do not think it is, considering I will not be using the code I posted here.

  • 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-05-26T05:26:43+00:00Added an answer on May 26, 2026 at 5:26 am

    With singly-linked lists, the efficient way to add new elements is putting them at the beginning of the list. For your design, this would be:

    element newHead;
    newHead.next = &list;
    

    You’ll notice that newHead is now the first element, and list no longer represents the whole list. This leads to a more functional programming style, where you are creating new lists all the time (see: the cons function in Lisp).

    Procedural languages like C++ generally use some wrapper structure like this:

    struct list
    {
        element * first;
        void prepend(element * elt)
        {
            elt->next = first;
            first = elt;
        }
    }
    

    so list prepends are expressed as changing an existing list instead of creating a new one.

    With such an auxiliary structure, it is also trival to keep track of the list size, and keep a pointer to the last element for fast appends. These both come at the cost of a few extra instructions for every list operation.

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

Sidebar

Related Questions

No related questions found

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.