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.
#include "stdafx.h"
#include <string.h>
#include <iostream>
usingnamespace 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;
}
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.
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;
}