Closed Thread
Results 1 to 6 of 6

Thread: Linked List - Theory

  1. #1
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Linked List - Theory

    I have doubts on the theory of doubly linked list.
    In this code

    Code:
    aux->info = value;
    aux->next = *list;
    aux->prev = NULL:
    (*list)->prev = aux;
    *list = aux;
    See if I'm correct:

    Code:
    aux->info = value;
    Get the value in variable aux

    Code:
    aux->next = *list;
    Pointer next of the variable aux points to list (What list? Points to the entire list that already exists?)

    Code:
    aux->prev = NULL;
    Will insert the value at the beginning then prev is null

    Code:
    (*list)->prev = aux;
    *list = aux;
    I do not understand that part of the code

    What I do not understand is when you point to: *list, aux (ie without the prev or next)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: Linked List - Theory

    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

  4. #3
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Re: Linked List - Theory

    Quote Originally Posted by cod3b3ast View Post
    I guess I don't understand what you are asking. Are you asking what the last lines of that program do?

    I would like to understand what each line does

  5. #4
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: Linked List - Theory

    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

  6. #5
    cod3b3ast's Avatar
    cod3b3ast is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    74
    Rep Power
    0

    Re: Linked List - Theory

    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:

    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 first 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;
    }
    Here is the second variation of the main method:

    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

  7. #6
    Apprentice123 is offline Programming Expert
    Join Date
    Jun 2008
    Posts
    397
    Rep Power
    0

    Re: Linked List - Theory

    Thank you

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. linked list
    By Hamed in forum C and C++
    Replies: 21
    Last Post: 01-06-2011, 09:19 PM
  2. Linked List in C
    By crazycaw in forum C and C++
    Replies: 1
    Last Post: 07-10-2010, 08:54 PM
  3. Replies: 2
    Last Post: 06-28-2010, 08:45 PM
  4. Linked List help
    By alpdog14 in forum Java Help
    Replies: 0
    Last Post: 10-07-2009, 12:53 PM
  5. linked list
    By Apprentice123 in forum General Programming
    Replies: 13
    Last Post: 09-14-2009, 12:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts