Converting Linked List to Doubly Linked List

Hello everyone,

First post here. The details:
Add a pointer called "prior" to the linked list class so that you now have a doubly linked list. This means you can go forward and backward.
Start at the tail (end) of the linked list and write out the data in reverse order using the "prior" pointer.

I'm aware of this post:http://www.cplusplus.com/forum/beginner/51070/ , but he uses a constructor and I would like to do it without it
Original:
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
  #include "stdafx.h"
#include <string.h>
#include <iostream>

using namespace std;


class node
{
  public:
  char data;
  node *next;
  node *prior;
  node();
};

node::node()
{

}

int _tmain(int argc, _TCHAR* argv[])
{
	char s[]="abcdefghijklmnopqrstuvwxyz";

	node *head;
	node *temp;
	node *current;

	head = new node;          // create the head of the linked list
	head->data = s[0];
	head->next = NULL;
	//head->prior = NULL;
	temp = head;   // get ready for the loop - save the head in temp - you are going to change temp in the loop

	for(size_t i=1; i < strlen(s); i++)      // create the rest of the linked list
	{
	current = new node;    // make a new node
	current->data = s[i];  // set it's data member
	current->next = NULL;
	temp->next = current;  // point to the new node
	temp = current;        // make temp point to current node (for next time through)
	}

	node *ptr = head;    // set a ptr to head, then you are going to "increment" the pointer

	while (ptr != NULL)
	{
		cout << ptr->data; // print out the linked list
		ptr = ptr->next;   // increment the linked list
	}

	cout << endl;
	system("pause");
	return 0;
}


Here's what I've got:
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
int _tmain(int argc, _TCHAR* argv[])
{
	char s[]="abcdefghijklmnopqrstuvwxyz";

	node *head;
	node *tail;
	node *temp;
	node *current;

	head = new node;          // create the head of the linked list
	head->data = s[0];
	head->next = NULL;
	head->prior = NULL;
	temp = head;   // get ready for the loop - save the head in temp - you are going to change temp in the loop
	tail = head;

	for(size_t i=1; i < strlen(s); i++)      // create the rest of the linked list
	{
	current = new node;    // make a new node
	current->data = s[i];  // set it's data member
	current->next = NULL;
	current->prior = tail;
	temp->next = current;  // point to the new node
	temp = current;        // make temp point to current node (for next time through)
	}

	node *ptr = head;    // set a ptr to head, then you are going to "increment" the pointer

	while (ptr != NULL)
	{
		cout << ptr->data; // print out the linked list
		ptr = ptr->next;   // increment the linked list

	
	}

	node *ptr1 = tail;    // set a ptr to head

	while (ptr1 != NULL)
	{
		cout << ptr1->data; // print out the linked list
		ptr1 = ptr1->next;   // increment the linked list
	}


	cout << endl;
	system("pause");
	return 0;
}

I'm horribly wrong right now, but I feel like i'm close. I've looked all over and can't figure it out. If someone could just point me in the the right direction I would appreciate it very much. I'm still very very new to linked lists.

Thanks
You don't need temp. Get rid of it.
Replace lines 23 & 24 with tail = current;
Ok, I replaced lines 23 and 24 with tail = current; and now the program only outputs "az". Im still lost? Am I missing something here?
Ah. The loop starting at line 39 is supposed to walk backwards through the list but your setting ptr1 to ptr1->next.
Ok, so finally got it to work, thanks again for the help. Here was my final solution!
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
int _tmain(int argc, _TCHAR* argv[])
{
	char s[]="abcdefghijklmnopqrstuvwxyz";

	node *head;
	node *tail;
	node *temp;
	node *current;

	head = new node;          // create the head of the linked list
	head->data = s[0];
	head->next = NULL;
	head->prior = NULL;
	temp = head;   // get ready for the loop - save the head in temp - you are going to change temp in the loop
	tail = head;

	for(size_t i=1; i < strlen(s); i++)      // create the rest of the linked list
	{
	current = new node;    // make a new node
	current->data = s[i];  // set it's data member
	current->next = NULL;
	current->prior = tail;
	temp->next = current;  // point to the new node
	temp = current;        // make temp point to current node (for next time through)
	tail = current;
	}

	node *ptr = head;    // set a ptr to head, then you are going to "increment" the pointer
	
	cout << "First Foreward: \n";

	while (ptr != NULL)
	{
		cout << ptr->data; // print out the linked list
		ptr = ptr->next;   // increment the linked list
	}

	cout << "\n\nNow In Reverse:  \n";
	ptr = tail;    // set a ptr to head

	while (ptr != NULL)
	{
		cout << ptr->data; // print out the linked list
		ptr = ptr->prior;   // increment the linked list
	}


	cout << "\n" << endl;
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.