Doubly linked list crashing after trying to print from end

It crashes after choosing option 3.

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
  #include <iostream>

using namespace std;

struct node{
    int data;
    struct node *next;
    struct node *prev;
};

    void create_list(int num);
    void display_dlist();
    void reverse_display_dlist();

    struct node *head = NULL;

int main()
{
    int choice, value, position;

    while (1){

        cout<<endl<<"----------------------------"<<endl;
        cout<<endl<<"Operations on Doubly linked list"<<endl;
        cout<<endl<<"----------------------------"<<endl;
        cout<<"1.Create doubly linked list"<<endl;
        cout<<"2.Print from start"<<endl;
        cout<<"3.Print from end"<<endl;

        cin>>choice;

        switch (choice){
    case 1:
        cout<<"Enter value: ";
        cin>>value;
        create_list(value);
        cout<<endl;
        break;
    case 2:
        display_dlist();
        break;
    case 3:
        reverse_display_dlist();
        break;
    default:
        cout<<"Option doesnt exist"<<endl;
        break;
        }
    }

    return 0;
}
   void create_list(int num){
        struct node *temp, *temp1;
        temp = new node;
        temp->data = num;
        temp->next = NULL;
        if(head == NULL){
            temp->prev = NULL;
            head = temp;
        }
        else{
            temp1 = head;
            while(temp1->next != NULL)
                temp1 = temp1->next;
            temp1->next = temp;
            temp->prev = temp1;
        }

   }

   void display_dlist(){
        struct node *temp = head;

        while(temp != NULL){
            cout<<temp->data<<"  ";
            temp = temp->next;
        }
        cout<<endl;
   }

   void reverse_display_dlist(){
        struct node *temp = head;
        while(temp != NULL){
            temp = temp->next;
        }
        do{
            cout<<temp->data<<"  ";
            temp = temp->prev;
        }while(temp != head);

        cout<<endl;
   }
Time to learn about debuggers.
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
$ g++ -Wall -Wextra -g foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:19:24: warning: unused variable ‘position’ [-Wunused-variable]
     int choice, value, position;
                        ^
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) run
Starting program: /home/sc/Documents/a.out 



Operations on Doubly linked list


1.Create doubly linked list
2.Print from start
3.Print from end
1
Enter value: 22




Operations on Doubly linked list


1.Create doubly linked list
2.Print from start
3.Print from end
3

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400bf3 in reverse_display_dlist () at foo.cpp:88
88	            cout<<temp->data<<"  ";
(gdb) p temp
$1 = (node *) 0x0
(gdb) list
83	        struct node *temp = head;
84	        while(temp != NULL){
85	            temp = temp->next;
86	        }
87	        do{
88	            cout<<temp->data<<"  ";
89	            temp = temp->prev;
90	        }while(temp != head);
91	
92	        cout<<endl;
(gdb) 

Your first while loop needs to stop at the last node, not fall off the end.
Last edited on
Thanks for your help.

Here's the working 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
#include <iostream>

using namespace std;

struct node{
    int data;
    struct node *next;
    struct node *prev;
};

    void create_list(int num);
    void display_dlist();
    void reverse_display_dlist();

    node *head = NULL;
    node *tail= NULL;

int main()
{
    int choice, value, position;

    node *head = NULL;
    node *tail= NULL;

    while (1){

        cout<<endl<<"----------------------------"<<endl;
        cout<<endl<<"Operations on Doubly linked list"<<endl;
        cout<<endl<<"----------------------------"<<endl;
        cout<<"1.Create doubly linked list"<<endl;
        cout<<"2.Print from start"<<endl;
        cout<<"3.Print from end"<<endl;

        cin>>choice;

        switch (choice){
    case 1:
        cout<<"Enter value: ";
        cin>>value;
        create_list(value);
        cout<<endl;
        break;
    case 2:
        display_dlist();
        break;
    case 3:
        reverse_display_dlist();
        break;
    default:
        cout<<"Option doesnt exist"<<endl;
        break;
        }
    }

    return 0;
}
   void create_list(int num){
        struct node *temp, *temp1;
        temp = new node;
        temp->data = num;
        temp->next = NULL;
        if(head == NULL){
            temp->prev = NULL;
            head = temp;
        }
        else{
            temp1 = head;
            while(temp1->next != NULL)
                temp1 = temp1->next;
            temp1->next = temp;
            temp->prev = temp1;
            tail = temp;
        }

   }

   void display_dlist(){
        struct node *temp = head;

        while(temp != NULL){
            cout<<temp->data<<"  ";
            temp = temp->next;
        }
        cout<<endl;
   }

   void reverse_display_dlist(){
        struct node *temp = tail;

        do{
            cout<<temp->data<<"  ";
            temp = temp->prev;
        }while(temp != head);
        temp = head;
        cout<<temp->data;

        cout<<endl;
   }

Better.
Now do it without using any global variables.

Okay, I will try.
Would it be hard to remove global variables later?
I want to finish the whole program using my method first.
Topic archived. No new replies allowed.