Doubly Linked List

Hey Everyone, it's been a while but I am back and needs some assistance. I have a linked list and I want to take it to a doubly linked list so I can print the alphabet backwards using my *prior but can seem to figure it out. Any ideas?

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

#include "stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;

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

node::node( )
{
	data = ' ';
	next = NULL;
	prior = NULL;
}

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

	node *head; // pointer to front
	node *tail; // pointer to back
	node *temp;
	node *temp1;
	node *current;
	node *prior;

	head = new node; // create the head of the linked list
	head->data = s[0];
	head->next = NULL;
	temp = head;

	tail = new node; // create the tail of the linked list
	tail->data = s[0];
	tail->next = NULL;
	temp1 = tail;

	// This creates the alphabet but I am haing a hard time trying to figure out
	// how to implement tail so I can traverse the alphabet backwards.

	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)
	}

	// Prints alphabet 
	node *ptr = head;    // set a ptr to head
	while (ptr != NULL)
	{
		cout << ptr->data; // print out the linked list
		ptr = ptr->next;   // increment the linked list
	}

	// Prints alphabet backwards
	node *ptr1 = head;    // 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;
}


Thanks,
Last edited on
closed account (D80DSL3A)
When you create the 1st node it is both head and tail of the list:
1
2
3
4
5
head = new node;
head->data = s[0];
head->next = NULL;
head->prior = NULL;
tail = head;


Each node that follows in the list becomes the new tail. In your for loop:
1
2
3
4
5
6
current = new node;    // make a new node
current->data = s[i];  // set it's data member
current->next = NULL;
current->prior = tail;
tail->next = current;
tail = current;


Note that the code for all of this can be simplified a lot by adding this constructor to your node structure:
1
2
3
4
5
node(char Data, node* Prior, node* Next ): data(Data), prior(Prior), next(Next)
{
    if(prior)prior->next = this;
    if(next)next->prior = this;
}

The 5 lines for creating the 1st node can become 1 line:
head = tail = new node(s[0], NULL, NULL);
and the 6 lines in the for loop become:
 
tail = new node(s[i], tail, NULL);


EDIT: You never made use of the prior data member in your node structure. You will need to use it to print the list backwards. Presently both of your while loops are printing the list forward.
Last edited on
Thanks fun2code. This is what I got and it works due to your input and advice.
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
#include "stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;

class node
{
  public:
  char data;
  node *next;
  node *prior;
  node( char, node*, node* );
};
// use constructor to initialize members of Class node
node::node(char Data, node* Prior, node* Next ): data(Data), prior(Prior), next(Next) 
{
    if(prior)prior->next = this;
    if(next)next->prior = this;
}

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

	node *head; // ptr to head of list
	node *tail; // ptr to tail of list

	// call constructor and initialize first node 
	head = tail = new node(s[0], NULL, NULL);

	for(size_t i=1; i < strlen(s); i++) // create the rest of the linked list
	{
		tail = new node(s[i], tail, NULL);
	}

	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 tail, then "decrement" the pointer form z to a
	cout << "\n\nThe alphabet reversed using prior ptr:" << endl; // output 2 empty lines and add a comment
	while ( ptr1 != NULL )
	{
		cout << ptr1->data; // print out the linked list
		ptr1 = ptr1->prior; // decrement the linked list
	}

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


Thanks again.
closed account (D80DSL3A)
You're welcome. One more thing...
You ought to delete the nodes since they were dynamically allocated. One more while loop can do this. Just iterate through the list and delete each node as you go (but save a pointer to the next node first).

1) Try writing a function to insert a new node within the list! The constructor I gave makes this easy.

2) Try writing a function to remove a node from within the list. A destructor for the node class which assigns the links across the node to be deleted (and then deletes the node) would be useful.
Alright, I will give it a go. You will be hearing form me again, I'm sure.
Thanks again.
Last edited on
Topic archived. No new replies allowed.