Hello, I'm working on a C++ program and I'm trying to create an insert function that alphabetizes a linked list. Here's my code for my insert function:
void insert(word* node, word* newNode){ // insert newNode after node
newNode->next = node->next;
node->next = newNode;
newNode->previous=node->previous;
newNode->previous=node;
}
Here's my code inside my int main():
for(int i=1;i
My next and previous pointers are not pointing to anything and I'm not sure why? If more information is needed on my code I will provide it. The arrays are taking in string words from a linked list. Any help is appreciated Thank you!
I do not understand your code. I assume you have left out some of it. Can you post your entire file (use attach file button) but on the face of it, it looks wrong.
If newNode is supposed to be linked in before node, then node->next doesn't change.
Hello! I have attached my code and the instructions as a word document. I thought my code might be a little confusing without posting some of the instructions. Sorry I hadn't done this sooner! I haven't created any display function so that is not visible in my code; I also have not added any comments yet. Please let me know if you would like me to add any of these. What I'm working on is step 4. I think my code also covers step 2 subsections 2 and 3 in subsection 1 so I did not include them. I have made some changes. Thank you guys so much!
Your code for inserting the records alphabetically into a linked list is not correct. you want to test the current record words[i] against the entries in the linked list (referenced by w). Here is an example (it looks more like C than C++) but it shows what I mean.
Thank you! I'm looking over the code and trying to understand how it works right now, however I'm having trouble with "struct." I did not learn this in class and I was wondering if it is essential to use it in my code if I want to alphabetize it and create my insert function?
No, you don't need the struct. It is a way for pure C to define a data type that is made up of a composite of different types. Your definition of word has the same effect. The main point is to understand the logic. At the highest level, 1) we start by assuming we have a list of items linked together in alphabetical order. 2) we get a new record (wordss[i] in your case, for the current value of i 3) we look at the first node in the linked list. If the new record should be before this node, then we insert the new record before it. 4) otherwise, we go to the next node and repeat step 3) 5) at the end, we check if the pointer to the last node we looked at is NULL. If it is not null, that means we have inserted the new record, and executed the "break" command (exited from the loop early), and we are done with that new record. If the pointer is null, we reached the end of the linked list and never inserted the new record. So insert it.
Of course, the devil is in the details. We have to be sure the logic works for the first case when the linked list is not defined (start is null). And for the case when the new record becomes the new first node in the linked list, or conversely, the last node in the linked list. Note I did not make the linked list "double linked" with a previous and next pointer. But you can tweak that part. Because the logic uses the previous pointer, it would look a bit cleaner to keep track of it in the record.
Thank you so much! I'll attempt to work on it now! I'll log back in later on to see if I got it.
I would test your program using just a a few records. also, I would print out the records you read in then print out the linked list you created. once the program works for 2 or 3 items, it will probably work for 100's
Ok I'll try that. I'll log in later, since this might take me a while.
Thank you very much phi! I'm sorry I responded very late. This assignment was assigned and due during finals week so I did not have much time to work on it and log back in, but your hint was very helpful.
Join our real-time social learning platform and learn together with your friends!