Can you review my code for a simple linked list?

I been practicing programming with c++ as my first language and the topic i am on is on : data structures one of them being linked list.

concerns:
*
I am wondering whether i have implemented this basic linked list correctly.
no traversing,insert,delete etc... yet. Just basically creating the structure of the list allocating memory for my nodes which there are 4 of them head,next_node1,next_node2,and tail_node .

**
I wanted to add another link/next into my node class(spice things up a bit =) to a newb) to point to the previous node.Is this how you basically do it?
***
I have seen code that has another pointer called root for example to point to the actual linked list at the head (what i understood of that is that they do it to not lose the list? is that correct?or is there anything extra info for why they do this?)




*****
Do you have any links of where i can learn more about linked list in c++? or anything you might know about linked list would be helpful. Thanks







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#include <iostream>
#include <string>

using namespace std;


class node
{
  public:
     node( ) {cout<<"contructing node"<<endl<<endl;}

  int   keyvalue;
  node * next;
  node * previous_nodeP;


};


int main()
{
    //HEAD NODE
node * head_node = NULL;

// MIDDLE NODES
node * next_node1 = NULL;
node * next_node2 = NULL;

//TAIL NODE
node * tail_node = NULL;


// ALLOCATING MEMORY OFF THE HEAP
head_node= new node;
next_node1= new node;
next_node2=new node;
tail_node= new node;


//INITIALIZING  HEAD_NODE VALUES
head_node-> keyvalue = 5;
head_node->next=next_node1;
head_node->previous_nodeP=NULL;   //changed to NULL 

//PRINTING HEAD_NODE MEMBERS
cout<<"HEAD_NODE's address: "<<head_node<<endl;
cout<<"The DATA that HEAD_NODE carries is: " <<head_node->keyvalue   <<endl;
cout<<"next \"node\" that head_node  points to: "<< head_node->next<<endl<<endl;




//INITIALIZING NEXT_NODE MEMBERS
next_node1->keyvalue = 9;
next_node1->next =next_node2;
next_node1->previous_nodeP=head_node;

cout<<"NEXT_NODE1's addresss: "<<next_node1<<endl;
cout<<"the DATA that next_node1 carries is: "<< next_node1->keyvalue <<endl;
cout<< "next \"node\" that next_node1  points to: "<<next_node1->next <<endl;
cout<<" pointer to the previous \"node\" head_node: "<<next_node1->previous_nodeP<<endl<<endl;

// INITIALIZING NEXT_NODE2 MEMBERS
next_node2->keyvalue = 3;
next_node2->next = tail_node;
next_node2->previous_nodeP=next_node1;

cout<<"NEXT_NODE2's address :"<<next_node2<<endl;
cout<<"the DATA that next_node2 carries is: "<<next_node2->keyvalue<<endl;
cout<<"next \"node\" that next_node2 points to: "<<next_node2->next<<endl;
cout<<" pointer to the previous \"node\" node:  "<<next_node2->previous_nodeP<<endl<<endl;

//INITIALIZING TAIL_NODE MEMBERS
tail_node->keyvalue = 20;
tail_node->next=NULL;
tail_node->previous_nodeP=next_node2; /*added  after fun2code's eagle eye caught it =)  */

cout<<"TAIL_NODE's address: "<< tail_node <<endl;
cout<<"the DATA that tail_node carries is: "<<tail_node->keyvalue <<endl;
cout<<"next \"node\" that tail_node points to: "<<tail_node->next <<endl;
cout<<"~END OF LINKED LIST~ "<<endl;

//DEALLOCATING MEMORY
delete head_node;
delete next_node1;
delete next_node2;
delete tail_node;

return 0;
}

-------------------------------------------------
OUTPUT:
contructing node

contructing node

contructing node

contructing node

HEAD_NODE's address: 0x804e178
The DATA that HEAD_NODE carries is: 5
next "node" that head_node points to: 0x804e188

NEXT_NODE1's addresss: 0x804e188
the DATA that next_node1 carries is: 9
next "node" that next_node1 points to: 0x804e198
pointer to the previous "node" head_node: 0x804e178

NEXT_NODE2's address :0x804e198
the DATA that next_node2 carries is: 3
next "node" that next_node2 points to: 0x804e1a8
pointer to the previous "node" node: 0x804e188

TAIL_NODE's address: 0x804e1a8
the DATA that tail_node carries is: 20
next "node" that tail_node points to: 0
~END OF LINKED LIST~
----------------------------------------------
Last edited on
closed account (D80DSL3A)
Not bad!
I see 2 small problems.

You should be able to iterate forward (from head to tail via the next pointers) OK as all the next assignments are good.
If you try to iterate backwards though (from tail to head via the prev pointers) there are 2 errors which will cause problems:
1) You forgot to assign a value to tail_node->previous_nodeP.
It should be: tail_node->previous_nodeP = next_node2;
2) Line 44: head_node->previous_nodeP=head_node;
Why did you assign head to point to itself? This would cause an infinite loop.

If you want the list to be structured like this:

NULL <-- head_node <--> next_node1 <--> next_node2 <--> tail_node --> NULL

Then you should have: head_node->previous_nodeP=NULL;

Iterating through the list is easy:
1
2
3
4
5
6
7
// forwards
for( node* p_Node = head_node; p_Node != NULL; p_Node = p_Node->next )
    cout << p_Node->keyvalue << " ";

// backwards
for( node* p_Node = tail_node; p_Node != NULL; p_Node = p_Node->previous_nodeP )
    cout << p_Node->keyvalue << " ";

Try that to test out the list linkage.
Also, another constructor can make creating the list MUCH easier, but I'll save that for next time.
Hope this helps.
Oh yes line 44...i forgot to remove that before submitting it looked kind of silly when i typed it and was thinking about it(brainfart).
Thank you for catching that! i didn't know it was missing that.

for iterating through the list
you're creating a pointer of type "node" to point to the head of the list,iterating through each node's "next" data member until it finds NULL ? ah i see now.


Thank you very much for your help.
Topic archived. No new replies allowed.