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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:27:43+00:00 2026-06-12T14:27:43+00:00

I’m trying to create a method which will add a char* to a LinkedList,

  • 0

I’m trying to create a method which will add a char* to a LinkedList, ensuring that the linkedList is always sorted alphabetically. I have been given code to define a LinkedItem struct:

// Define our list item as 'struct ListItem', and also 
// typedef it under the same name.
typedef struct ListItem {
  char *s;
  struct ListItem *p_next;
} ListItem;

I have also been given a method to add an item to the beginning of a list:

// Adds a new item to the beginning of a list, returning the new
// item (ie the head of the new list *)
ListItem* add_item(ListItem *p_head, char *s) {  
    // Allocate some memory for the size of our structure.
    ListItem *p_new_item = malloc(sizeof(ListItem));
    p_new_item->p_next = p_head;       // We are the new tail.
    p_new_item->s = s;     // Set data pointer. 
    return p_new_item;
}

Now here’s my code, I’ll explain more after:

ListItem* addSortedItem(ListItem *p_head, char *s){         
    if(p_head==NULL)//if the list is empty, we add to the beginning
        return add_item(p_head,s);
    ListItem* p_new_item = malloc(sizeof(ListItem));
    ListItem *p_current_item = p_head; //makes a pointer to the head of the list


    while (p_current_item) {    // Loop while the current pointer is not NULL
        printf("entering while loop with current=%s\n",p_current_item->s);
        // now we want to look at the value contained and compare it to the value input     
        if(aThenB(s,p_current_item->s)!=TRUE){
            // if A goes after B, we want to go on to look at the next element
            p_current_item=p_current_item->p_next;
        } else if (aThenB(s,p_current_item->s)==TRUE) {printf("entered elseif\n");      
            p_head=add_item(p_current_item,s);
            return p_head;          
        } else {printf("WHY DID WE EVER REACH THE ELSE!?"); return p_head;}
    }
}

Now, aThenB(StringA,StringB) returns TRUE if the correct sorted order of A and B is A, then B, and false otherwise- equality is also an option, I simply haven’t gotten this to work well enough to allow that 🙂

What is happening with my test data (which is "sheep i", with i from 0-10) is that either I am only returning one element back, or i am randomly skipping elements, depending on the order input. I can include more code but it’s a bit messy.

I think my problem is stemming from not fully understanding the pointers and how they work- I want to ensure that p_head is always pointing to the head, whilst p_current is roving through the list. But I’m also getting seg faults when p_current reaches the last element, so I’m not sure where I’m going wrong.

Thank you for any help on how to get my code to return properly 🙂

Edit: addSortedItem() is called in the following block in the main method:

    // The empty list is represented by a pointer to NULL.
    ListItem *p_head = NULL;
    ListItem *p_head2=NULL;
    // Now add some items onto the beginning of the list.
    int i;
    for (i=0; i<NO_ITEMS; i++) {

    // Allocate some memory for our string, and use snprintf to
    // create a formatted string (see GNU API docs) much like printf
    // but instead writing to memory rather than the screen.
    char* s = malloc(MAX_DATA_CHARS);
    snprintf(s, (size_t) MAX_DATA_CHARS, "sheep %d", i);
    p_head = addSortedItem(p_head, s);
    }
  • 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-12T14:27:44+00:00Added an answer on June 12, 2026 at 2:27 pm

    you have an error in the add of a new element in the middle or in the end of the linked list.
    in your addStoredItem function you make a pointer from your newItem to the currentItem but you do not take account to have a link from previous element to the newItem

    initial linked list befor calling addStoredItem

    item1-->item2-->item4-->item5
    

    Linked list afetr calling addStoredItem in order to add item3

    item1-->item2-->item4-->item5
                    ^
                    |
                    item3
    

    so as you can see you have added the new item at the head of the sub linked list starting from item4 but you do not make link from item2 to item3
    so that we have to keep a pointer to the previous item in order to complete the link.

    the add_item() function allow to add item in the head

    The same thing if you try to add item at the end of the linked list

    item1-->item2-->item4-->item5    NULL
                                     ^
                                     |
                                     item6
    

    the item6 has added as separate head and there is no link from item5 (previous) to item6 (new)

    so your addStoredItem() function could be fixed like this

    ListItem* addSortedItem(ListItem *p_head, char *s){         
        if(p_head==NULL)//if the list is empty, we add to the beginning
            return add_item(p_head,s);
        struct ListItem* p_new_item = malloc(sizeof(ListItem));
        ListItem *p_current_item = p_head; //makes a pointer to the head of the list
        ListItem *p_prev_item = NULL; //FIXED
    
        while (p_current_item) {    // Loop while the current pointer is not NULL
            printf("entering while loop with current=%s\n",p_current_item->s);
            // now we want to look at the value contained and compare it to the value input     
            if(aThenB(s,p_current_item->s)!=TRUE){
                // if A goes after B, we want to go on to look at the next element
                p_prev_item = p_current_item; //FIXED
                p_current_item=p_current_item->p_next;
            } else if (aThenB(s,p_current_item->s)==TRUE) {printf("entered elseif\n");      
                break;        
            } else {printf("WHY DID WE EVER REACH THE ELSE!?"); return p_head;}
        }
    
        if (p_prev_item!=NULL) //FIXED
            p_prev_item->p_next=add_item(p_current_item,s); //FIXED
        else //FIXED
            p_head=add_item(p_current_item,s);//FIXED
        return p_head; //FIXED
    }
    

    the fixed lines are indicated with //FIXED

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I

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.