I have doubts on the theory of doubly linked list.
In this code
See if I'm correct:Code:aux->info = value; aux->next = *list; aux->prev = NULL: (*list)->prev = aux; *list = aux;
Get the value in variable auxCode:aux->info = value;
Pointer next of the variable aux points to list (What list? Points to the entire list that already exists?)Code:aux->next = *list;
Will insert the value at the beginning then prev is nullCode:aux->prev = NULL;
I do not understand that part of the codeCode:(*list)->prev = aux; *list = aux;
What I do not understand is when you point to: *list, aux (ie without the prev or next)
I guess I don't understand what you are asking. Are you asking what the last lines of that program do?
I don't document code. If it was hard to write, it should be hard to read
I was all excited to explain it, but then I realized that I didn't know what was going on either. lol Maybe you can answer some of my questions. Would your entire code look something like this?
Code:struct List { string info; List * next; List * prev; List() { } } int main() { List * aux; List list; string value = "list 1"; aux->info = value; aux->next = *list; aux->prev = NULL: (*list)->prev = aux; *list = aux; }
I don't document code. If it was hard to write, it should be hard to read
Well, the code you posted doesn't compile, so I wrote a couple variations of it that you might have meant. Here is the first part that is the same for both variations:
Here is the first variation of the main method:Code:#include <iostream> #include <string> #include <vector> using namespace std; struct Node { string val; Node * next; Node * prev; Node(string v) : val(v) { } void print() { cout << val << "\n"; } };
Here is the second variation of the main method:Code:int main() { Node * one = new Node("list one"); Node * two = new Node("list two"); one->next = two; one->prev = 0; two->prev = one; two = one; cout << "one->val: " << one->val << "\n"; cout << "one->next->val: " << one->next->val << "\n"; cout << "two->val: " << two->val << "\n"; return 0; }
Code:int main() { Node * one = new Node("list one"); Node two("list two"); one->next = (&two); one->prev = 0; two.prev = one; two = (*one); cout << "one->val: " << one->val << "\n"; cout << "one->next->val: " << one->next->val << "\n"; cout << "two->val: " << two.val << "\n"; return 0; }
I don't document code. If it was hard to write, it should be hard to read
Thank you
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks