Jump to content

Linked List - Theory

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
I have doubts on the theory of doubly linked list.
In this code

aux->info = value;
aux->next = *list;
aux->prev = NULL:
(*list)->prev = aux;
*list = aux;

See if I'm correct:

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

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

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

(*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
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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 ;)

#3
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

cod3b3ast said:

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

#4
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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?

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 ;)

#5
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
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:

#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:

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:

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 ;)

#6
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Thank you