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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:15:15+00:00 2026-05-23T17:15:15+00:00

I’m creating a linked list data structure in C. However, I’m receiving some weird

  • 0

I’m creating a linked list data structure in C. However, I’m receiving some weird behavior in my implementation of an addLast function. It seems that the added element doesn’t appear until my next call to addLast. My code (I’ll explain through inline comments how I think my code is working):

Helper code:

typedef struct LinkedList linkedlist;
typedef int ListElement;

struct LinkedList{
  ListElement data;
  linkedlist *next;
};

//Initializes a list;
void CreateList(linkedlist *list, ListElement contents){
  list->data = contents;
  list->next = NULL;
}

//Prints the items of the list, head first.
void displayList(linkedlist *list){
  printf("(%d", list->data);
  linkedlist *node = list->next;

  if(node == NULL){
  }
  else{
    while(node->next != NULL){
      printf(" %d", node->data);
      node = node->next;
    }
  }

  printf(")");
}

Problematic code:

//Adds an element at the tail of the list
void addLast(linkedlist *list, ListElement forAdding){
  linkedlist *node = list;
  linkedlist *NewNode = (linkedlist *) malloc(sizeof(linkedlist));

  //Go to the last element in the list
  while(node->next != NULL){
    node = node->next;
  }

  //Prepare the node we will add
  NewNode->data = forAdding;
  NewNode->next = NULL;

  //Since node is pointing to the tail element, set its
  //next to the NewNode---the new tail
  node->next = NewNode;
}

//Special attention to this function!
void List(ListElement items[], linkedlist *list, int numItems){
  int i = 0;

  while(i < numItems){
    addLast(list, items[i]);
    printf("Before ");
    displayList(list);
    printf("\n");
    printf("Added %d", items[i]);
    displayList(list);
    printf("\n");
    i++;
  }
}

Main function:

int main(){
  linkedlist *l= (linkedlist *) malloc(sizeof(linkedlist));
  CreateList(l, 0);
  int a_list[5] = {1, 2, 3, 5, 6};
  List(a_list, l, sizeof(a_list)/sizeof(a_list[0]));
  printf("A list of five elements: %d", sizeof(a_list)/sizeof(a_list[0]));
  displayList(l);
  removeLast(l);
  addLast(l, 7);
  printf("\nAdded something at last position: ");
  displayList(l);
  printf("\n");
}

For which I get the output:

Before (0)
Added 1(0)
Before (0)
Added 2(0 1)
Before (0 1)
Added 3(0 1 2)
Before (0 1 2)
Added 5(0 1 2 3)
Before (0 1 2 3)
Added 6(0 1 2 3 5)
A list of five elements: 5(0 1 2 3 5)
Added something at last position: (0 1 2 3 5 6)

As you see, it seems that the item added will only appear on my next call to addLast.

I’ve so far figured out that it is actually there though for some reason it won’t get printed. If, for instance, I do another addLast(list, 6); call just before I close function List (but outside the loop, of course!), the output line Added something at last position... (which happens after a call to addLast(l, 7); will actually display Added something at last position: (0 1 2 3 5 6 6).

So, what am I doing wrong?

Thanks!

  • 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-23T17:15:15+00:00Added an answer on May 23, 2026 at 5:15 pm

    The problem is not in AddLast() it is simply your displayList() function :). You stop printing 1 element before the last element.

    Change the displayList() function like that:

    void displayList(linkedlist *list){
      printf("(");
      linkedlist *node = list;
    
      while(node != NULL){
        printf(" %d", node->data);
        node = node->next;
      }   
    
      printf(")");
    }
    

    This function can print empty lists too.

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.