Question: Create a doubly linked list using the C++ code below to create a program that prints the alphabet forwards and backwards The pointer that is
Create a doubly linked list using the C++ code below to create a program that prints the alphabet forwards and backwards
The pointer that is already there is called "next" so call this new pointer "previous" since it will point to the node preceding it, whereas the "next" pointer points to the node following it. Build the linked list of the alphabet but set the the prior pointer to point to the preceding node.
#include "stdafx.h"
#include
#include
using namespace std;
class node
{
public:
char data;
node *next;
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;
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");
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
