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);
}
you have an error in the add of a new element in the middle or in the end of the linked list.
in your
addStoredItemfunction 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 newIteminitial linked list befor calling
addStoredItemLinked list afetr calling
addStoredItemin order to add item3so 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 headThe same thing if you try to add item at the end of the linked list
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 thisthe fixed lines are indicated with
//FIXED