Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd to Linked List in sorted order, having trouble with pointers
    primarykey
    data
    text
    <p>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: </p> <pre><code>// 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; </code></pre> <p>I have also been given a method to add an item to the beginning of a list:</p> <pre><code>// 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-&gt;p_next = p_head; // We are the new tail. p_new_item-&gt;s = s; // Set data pointer. return p_new_item; } </code></pre> <p>Now here's my code, I'll explain more after:</p> <pre><code>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-&gt;s); // now we want to look at the value contained and compare it to the value input if(aThenB(s,p_current_item-&gt;s)!=TRUE){ // if A goes after B, we want to go on to look at the next element p_current_item=p_current_item-&gt;p_next; } else if (aThenB(s,p_current_item-&gt;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;} } } </code></pre> <p>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 :-)</p> <p>What is happening with my test data (which is <code>"sheep i"</code>, 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.</p> <p>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.</p> <p>Thank you for any help on how to get my code to return properly :-)</p> <p>Edit: addSortedItem() is called in the following block in the main method:</p> <pre><code> // 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&lt;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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload