Create a doubly linked list from a singly

How can I create a doubly linked list?
I overall understand the concept of the singly linked list but the doubly is giving me a hard time.

I cannot for the life of me figure how to add a node at the beginning of the list or at the middle. I have looked at examples online but they don't really seem to help. I think I have it figured out for adding at the end of the list. I also am having a hard time linking the previous nodes to one another to create a 'doubly'. I say this because obviously I am doing something wrong because I can display my list forwards correctly but when I try to display it in reverse order it will only show my last element in the list.

Also, I have to make the node in main(). So no, I cannot use the OOP approach. I also cannot use the STL list.

Here is my code so far...

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
using namespace std;

int main()
{
	struct ListNode
	{
		int value;
		ListNode *next;
		ListNode *prev;
	};

	ListNode *head = nullptr;
	ListNode *tail = nullptr;
	ListNode *newNode = nullptr;
	ListNode *nodePtr = nullptr;
	ListNode *prevPtr = nullptr;
	ListNode *nextNode = nullptr;
	ListNode *temp = nullptr;

	char ch = 'q';
	int val;

	do {
		cout << "a-add element to the list\n";
		cout << "d-delete element in the list\n";
		cout << "p-print this list\n";
		cout << "q-quit\n";
		cin >> ch;

		switch (ch) {
		case 'a':
			cout << "1-add element at beginning, 2-add at end, 3-add after an item\n";
			cout << "choice: ";
			cin >> ch;
			cout << "Enter the value to be added: ";
			cin >> val;

			newNode = new ListNode;
			newNode->value = val;
			newNode->next = nullptr;
			newNode->prev = nullptr;
			
			if (ch == '1') //add element at beginning
			{
				if (head == nullptr)
				{
					head = newNode;
					tail = newNode;
				}
				else if (head != nullptr)
				{
					nodePtr = head;
					temp = head;
					newNode->prev = head;
					head = newNode;
					nodePtr->next = temp;

					while (nodePtr->next != nullptr)
					{
						nodePtr->prev = nodePtr;
						nodePtr = nodePtr->next;
					}


				}

			}
			else if (ch == '2') //add at end
			{
				if (head == nullptr)
				{
					head = newNode;
					tail = newNode;
				}
				else
				{
					nodePtr = head;
					
					
					while (nodePtr->next != nullptr)
					{
						nodePtr->prev = nodePtr;
						nodePtr = nodePtr->next;
					}

					nodePtr->next = newNode;
					tail->next = newNode;
					tail = newNode;
				}

				cout << "\nhead = " << head->value;
				cout << "\ntail = " << tail->value << endl;
			}
			else if (ch == '3') //add after an item
			{


			}
			


			break;
		case 'd':
			cout << "deleting...\n";
			cout << "enter a value to be deleted ";
			cin >> val;

			if (head != nullptr)
			{
				if (head->value == val)
				{
					nodePtr = head->next;
					delete head;
					head = nodePtr;
				}
				else
				{
					nodePtr = head;

					while (nodePtr != nullptr && nodePtr->value != val)
					{
						prevPtr = nodePtr;
						nodePtr = nodePtr->next;
					}
					if (nodePtr != nullptr)
					{
						prevPtr->next = nodePtr->next;
						delete nodePtr;
					}
				}

			}
			else
				cout << "list is empty" << endl;
			break;
		case 'p':
			cout << "1-print forward, 2-print in reverse\n";
			cout << "choice: ";
			cin >> ch;

			if (ch == '1')
			{
				nodePtr = head;
				while (nodePtr != nullptr)
				{
					cout << "elem: " << nodePtr->value << endl;
					nodePtr = nodePtr->next;
				}
			}
			
			else if (ch == '2')
			{
				nodePtr = tail;
				while (nodePtr != nullptr)
				{
					cout << "elem: " << nodePtr->value << endl;
					nodePtr = nodePtr->prev;
				}
			}
			break;
		case 'q':
			cout << "quitting...\n";
			nodePtr = head;

			while (nodePtr != nullptr)
			{
				nextNode = nodePtr->next;
				cout << " deleting ... " << nodePtr->value << endl;
				delete nodePtr;
				nodePtr = nextNode;

			}
			break;
		default:
			cout << "invalid character -- please try again\n";
			break;
		}

	} while (ch != 'q');

	cout << endl;
	system("pause");
	return 0;
}
When dealing with linked lists, it is always a good idea to get out a piece of paper and a pencil and start drawing.

A singly linked list:

              ┌───┐   ┌───┐   ┌───┐   ┌───┐
      head ──►│ 0 ├──►│ 1 ├──►│ 2 ├──►│ 3 ├──► nullptr
              └───┘   └───┘   └───┘   └───┘

A doubly linked list also has pointers going back the the previous node:

              ┌───┐   ┌───┐   ┌───┐   ┌───┐
      head ──►│ 0 ├──►│ 1 ├──►│ 2 ├──►│ 3 ├──► nullptr
   nullptr ◄──┤   │◄──┤   │◄──┤   │◄──┤   │◄── tail
              └───┘   └───┘   └───┘   └───┘

To add or remove nodes, work out how to adjust the arrows (pointers).

Hope this helps.
Topic archived. No new replies allowed.