As the title says, I have a singly linked list which has stored all the elements in backwards, would I have to create a new List and restore the data into that from the original list?
As the title says, I have a singly linked list which has stored all the elements in backwards, would I have to create a new List and restore the data into that from the original list?
No. Just reverse the list. ( not the data, but the pointers)
How?
Sorry I'm a bit weak when it comes to Lists.
Do you truly wish to physically reverse the actual list or are you simply talking about displaying the data in a reverse order? If you just wish to display the data in reverse order you might not have to do anything to the list itself... a simple way would be to write a recursive function that call itself until it gets to the end of the list and then does a print statement. If you really do need to reverse the list, then you can do basically the same thing... write a recursive function that loops through the list until the end and then calls the insert function to take the current node's value and insert it into a second (new) list.
Or you can fix the problem at the source and not store the data in reverse order. Your insert function is storing items by creating a new node and setting the new node's next pointer to the start of the existing node. This results in a backwards list. What you want to do is set the last node's next pointer of the existing list to be equal to the new node you've just created (remember to set the new node's next pointer to NULL).
You can also use a double-linked list instead of a single-linked list (previous and next pointers). Then it wouldn't really matter if the list was forwards or backwards... just start at either end and loop through until you are done. It's not to much extra work maintaining the additional pointers.
Those are some ideas.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Ok I opted for this approach but am getting confused. Is it possible you can help me implement it in this code:Originally Posted by hk_mp5kpdw
Code:List *insertlist(char word, List * b) { List *c = calloc(1, sizeof(List)); c->x = word; c->next = b; return c; }
No it can't be done. Is the short answer, that why doubley linked lists
are used.
The sensible thing to do would be to create a double linkedl ist in the
first place, rather than stand on you head and jump through hoops to
achieve the same thing.
This is not true; reversing a singly-linked list is simple to do.Originally Posted by esbo
Code:node * reverse_in_place(node *list) { node *tail = NULL; node *tmp; while (list) { tmp = list->next; list->next = tail; list = tmp; } return tail; }
Last edited by Rash; 05-08-2006 at 03:49 PM.
Can somebody help me just store the data in the correct order instead of messing around with it later on?
Also another question, how do I access the end of the List, ie the tail?
Make pointers that point to them. As far as making sure it's in the "correct order", simply pick how you want it stored, and break it down into rules to check when inserting. Simply loop through the list, starting at the head, and insert it wherever you need to.
Quzah.
Hope is the first step on the road to disappointment.
u can have a tail pointer which points to the end of the list or u have to traverse all the way from the head to the end of the list thats isAlso another question, how do I access the end of the List, ie the tail?
ssharish2005Code:struct .... *temp; temp = head; while(temp->link != NULL) temp = temp->link;
If you only know the data from one element in the listOriginally Posted by Rash
you can never go back up the list to the start.
If you wanted to go backwards you should make it a double
linked list.
How do you create a pointer to the tail?
there are two ways that u can this tail pointer to be.
1. u can make the tail to grow towards the right as u keep on adding the node to the end of the list and updating the tail ppointer to the new node which is just been appended. keeping head const
2. u can have head growing towards the left where the new node is added at the font of the list and updating the head pointer to point to the new node which is just been added. keeping tail const
ssharish2005
Last edited by ssharish2005; 05-08-2006 at 04:43 PM.
eh??