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

  • Home
  • SEARCH
  • 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 530381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:11:26+00:00 2026-05-13T09:11:26+00:00

Visual Studio 2008 C What I can’t understand about this linked list is the

  • 0

Visual Studio 2008 C

What I can’t understand about this linked list is the adding to the tail in the else part of the if statement.

When the head and tails is assigned the memory address of the node_temp to both tail and head both point to the same memory location.

However, in the else part is the head in fact still pointing to the tail. There is just something I can’t explain and don’t understand about the else part?

I hope some one can explain better for me.

static struct convert_temp
{
    size_t cel;
    size_t fah;
    struct convert_temp *next;
} *head = NULL, *tail = NULL;

/** Add the new converted temperatures on the list */
void add(size_t cel, size_t fah)
{
    struct convert_temp *node_temp = NULL; /* contain temp data */

    node_temp = malloc(sizeof(*node_temp));

    if(node_temp == NULL)
    {
        fprintf(stderr, "Cannot allocate memory [ %s ] : [ %d ]\n",
            __FUNCTION__, __LINE__);
        exit(0);
    }

    /* Assign data */
    node_temp->cel = cel;
    node_temp->fah = fah;
    node_temp->next = NULL;

    if(head == NULL)
    {
        /* The list is at the beginning */
        head = node_temp;   /* Head is the first node = same node */
        tail = node_temp;   /* Tail is also the last node = same node */
    }
    else
    {
        /* Append to the tail */
        tail->next = node_temp;
        /* Point the tail at the end */
        tail = node_temp; 
    }
}
  • 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-13T09:11:26+00:00Added an answer on May 13, 2026 at 9:11 am

    The first time you add an element (let’s call it A) to the list, head is null and you go through the if part. That means that both head and tail are set to point to A when adding that first element.

    Now let’s add another element B. This time, head is not null so it goes through the else part, setting tail to point to B but leaving head pointing at A.

    This is as expected, you now have head pointing to A, A pointing to B, B pointing to nothing (null) and tail pointing to B.

    Let’s take it step by step.

    Initial state:  head -+-> null
                          |
                    tail -+
    
    Insert item A:  head -+-> A ---> null
                          |
                    tail -+
    
    Insert item B:  head ---> A -+-> B ---> null
                                 |
                    tail --------+
    
    Insert item C:  head ---> A ---> B -+-> C ---> null
                                        |
                    tail ---------------+
    

    You can see at each stage (other than the initial) that the current tail is set to point to the new node (which already points to NULL for its next node) then the tail pointer is updated to point to the new last node.

    In fact, let’s go through the addition of C in even more detail (line by line) so you can see what each line of code is doing (I’ve renamed node_temp to node just to help with formatting):

    Starting state:                head ---> A -+-> B ---> null
                                                |
                                   tail --------+
    
    node = malloc(sizeof(*node));  node ---> C ----------> ?
     (allocate node C)             head ---> A -+-> B ---> null
                                                |
                                   tail --------+
    
    node->next = NULL;             node ---> C --------+
     (ignore payload cel/fah                           |
      for now since it's not       head ---> A -+-> B -+-> null
        relevant to the list                    |
                   structure)      tail --------+
    
    tail->next = node;             node ---------------+
     (first in else clause)                            |
                                   head ---> A -+-> B -+-> C ---> null
                                                |
                                   tail --------+
    
    tail = node;                   node ---------------+
     (second in else clause)                           |
                                   head ---> A ---> B -+-> C ---> null
                                                       |
                                   tail ---------------+
    

    Then eventually node disappears since it’s a local variable and you have your final state:

                                   head ---> A ---> B -+-> C ---> NULL
                                                       |
                                   tail ---------------+
    

    The advantage of maintaining a tail pointer in a singly-linked list is to avoid having to step through the entire list to find the end when you’re trying to add an item to the end.

    Traversing the entire list makes insertion at the end an O(n) time operation (time taken is dependent on the number of items in the list). The use of the tail pointer makes that an O(1) time operation (same amount of time irrespective of the list size).

    As an aside, a doubly linked list has an extra use for a tail pointer – it gives the ability to quickly begin a traversal from the end of the list to the start, using tail and the prev pointers in lieu of head and the next pointers.

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

Sidebar

Related Questions

Is there a way that I can configure Visual Studio 2008 to understand CamelCase?
In Visual Studio 2008 I can add a project reference and set Copy Local
Is there a setting in Visual Studio 2008 that I can turn on which
In Visual Studio 2008, I can see all my project files in my Solution
Can Visual Studio 2005 Team edition for Developer coexist peacefully with Visual Studio 2008
Why I can use Object Initializers in Visual Studio 2008 Windows projects, etc targeted
I have visual studio 2008 installed on my PC. Can anyone tell me what
How can I exclude the bin folder from SourceSafe in a Visual Studio 2008
I would like to know if I can install say Visual Studio 2008 Pro
I'm a C++ developer and using Visual Studio 2008. How can I reduce *.obj

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.