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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:27:04+00:00 2026-05-31T17:27:04+00:00

So I’m playing around with linked lists, trying to get my brain around them,

  • 0

So I’m playing around with linked lists, trying to get my brain around them, and I decided to add “ordinal” values to each node in my list. This way, I can delete by ordinal rather than value, and do some other cool stuff later on.

Only, when I delete an element from the list all the ordinals get thrown out of whack (fairly obvious, that), so I thought “ok, I’ll just run up the list in a different function, resetting all of the ordinals”. I could technically start this function at the element that got deleted and pass it in the ordinal from that node to save time, as the previous nodes should be untouched, but for now I’m doing it the less elegant way because I said so.

“What is your question?”. I knew you’d ask that! “Get on with it!” I knew you’d say that!

The test program, which I can link to or include here, just creates a list of 5 nodes, removes the 3rd node and then adds a node onto the end.

The expected output is: DEBUG: resetting ordinals: 0 1 2 3 4

The actual output is: DEBUG: resetting ordinals: 1 2 4 5 5

So, without any further ado, here is my question: Why is the actual output different from my expectations?

void ll_fix(node_t* list)
{
  node_t* root = list;
  int ordinal = 0;
  printf("DEBUG: resetting ordinals: ");
  while(list->next != NULL)
  {
    list->ordinal = ordinal;
    list = (node_t*)list->next;
    printf("%d ",list->ordinal);
    ordinal++;
  }
  printf("%d\n",list->ordinal);
  list = root; // rewind the list
}
  • 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-31T17:27:05+00:00Added an answer on May 31, 2026 at 5:27 pm

    You are printing the ordinal before the change to it. So you see the previous values.

    Should probably be:

    list->ordinal = ordinal;
    printf("%d ",list->ordinal);
    list = (node_t*)list->next;
    ordinal++;
    

    This will not change the last value though, a better fix is:

    while(list != NULL) { // as per wildplasser comment, I moved the check to the beginning
        list->ordinal = ordinal;
        printf("%d ",list->ordinal);
        ordinal++;
        list = list->next;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.