Problem with Doubly Linked List

Hello All,

I am facing a issue with a doubly linked list.
My code is

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
#include<iostream>
using namespace std;
void addNode(int);
void displayListForward();
void displayListBackWard();
typedef struct doubly
{
        int data;
        struct doubly *prev;
        struct doubly *next;
}DOUBLY;

DOUBLY *node=NULL;
DOUBLY **head;
    
int main()
{
    head = &node;
    //cout<<"Head Address:"<<&head<<"\nNode address"<<&node<<endl;
    addNode(10);
    addNode(11);
    addNode(12);
    addNode(13);
    addNode(14);
    displayListForward();
    displayListBackWard();
    cout<<"Press Enter to continue...."<<endl;
    cin.get();
    return 0;
}

void addNode(int value)
{
          DOUBLY *temp;
          temp = new DOUBLY;
          if(temp == NULL)
          {
                   cout<<"Memory Allocation failed."<<endl;
                   return;
          }
//          cout<<"Value is:"<<value<<endl;
          temp->prev = node;
          temp->next = NULL;
          temp->data = value;
          node = temp;
          cout<<"Element Added."<<endl;
          //cout<<"Head Address:"<<&head<<"\nNode address"<<&node<<"\nTemp Address:"<<&temp<<endl;
}


void displayListForward()
{
     DOUBLY *temp;
    
     temp = *head;
     cout<<"Traversing in forward direction."<<endl;
     // cout<<"Head Address:"<<&head<<"\nNode address"<<&node<<endl;
     if(temp == NULL)
     {
             cout<<"No elemennts in the list"<<endl;
             return;
     }
     while(temp != NULL)
     {
                cout<<temp->data<<endl;
                temp = temp->next;
     }           
     
}    


void displayListBackWard()
{
     DOUBLY *temp;
     temp = node;
     cout<<"Traversing in reverse direction."<<endl;
     if(temp == NULL)
     {
             cout<<"No elemennts in the list"<<endl;
             return;
     }
     while(temp != NULL)
     {
                cout<<temp->data<<endl;
                temp = temp->prev;
     }           
     
}    



I am trying to create a doubly linked list and trying to traverse it in both directions. The reverse direction is working fine but forward movement is not working? Please help.
Could you please declare all your variables in main? And by not working what do you mean? Is it printing anything? Is it crashing all of a sudden?
Last edited on
Sometimes small act makes a big wonder.

Please put all your functions above your main.
By Not working I mean the code is not printing the linked list in forward direction. i.e. it should print 10 11 12 13 14 using function displayListForward().

But that is not happening.
It is only printing the last element.
It is only printing the last element.
That is because head and node are equivalent in your program. On line 18 you set it so.

node points always to the last element. Maybe 'tail' would be a better name.
Yes.
But I have done that only for once i.e. at the start of execution, head and node will be same. But in the function addNode, I am moving node forward without changing the heap.

Isn't that correct?
Shouldn't head ne pointing to the first node of linked list?
Well, head contains a pointer to node. When you're doing this *head you get the content of node. That is what you observe.

Change DOUBLY **head; to DOUBLY *head = NULL;

In addNode() you need to assign node to head if head is NULL. That's the trick and there's no other way
I have tried the same, but the issue is now I am getting only the first element 10 and not the rest of array.
:(
Found the culprit.
Corrected Code:

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
using namespace std;
void addNode(int);
void displayListForward();
void displayListBackWard();
typedef struct doubly
{
        int data;
        struct doubly *prev;
        struct doubly *next;
}DOUBLY;

DOUBLY *node=NULL;
DOUBLY *head=NULL;

void addNode(int value)
{
          if(node == NULL)
          {
                  node = new DOUBLY;
                  if(node == NULL)
                  {
                          cout<<"Memory not allocated to node"<<endl;
                          return;
                  }
                  else
                  {
                      node->data = value;
                      node->next = NULL;
                      node->prev = NULL;
                      head = node;
                  }
                          
          }        
          else
          {
              
              DOUBLY *temp;
              temp = new DOUBLY;
              if(temp == NULL)
              {
                   cout<<"Memory Allocation failed."<<endl;
                   return;
              }
              node->next = temp;        
              temp->prev = node;
              temp->next = NULL;
              temp->data = value;
              node = temp;
             
              cout<<"Element Added."<<endl;
            
          }
}


void displayListForward()
{
     DOUBLY *temp;
    
     temp = head;
     cout<<"Traversing in forward direction."<<endl;

     if(temp == NULL)
     {
             cout<<"No elemennts in the list"<<endl;
             return;
     }
     while(temp != NULL)
     {
                cout<<temp->data<<endl;
                temp = temp->next;
     }           
     
}    


void displayListBackWard()
{
     DOUBLY *temp;
     temp = node;
     cout<<"Traversing in reverse direction."<<endl;
     if(temp == NULL)
     {
             cout<<"No elemennts in the list"<<endl;
             return;
     }
     while(temp != NULL)
     {
                cout<<temp->data<<endl;
                temp = temp->prev;
     }           
     
}    




    
int main()
{
    head = node;

    addNode(10);
    addNode(11);
    addNode(12);
    addNode(13);
    addNode(14);
    displayListForward();
    displayListBackWard();
    cout<<"Press Enter to continue...."<<endl;
    cin.get();
    return 0;
}
Topic archived. No new replies allowed.