Question
This program builds a doubly linked list from entered numbers I have three files, but i can only modify LinkedList.cpp. Can you help me fill
This program builds a doubly linked list from entered numbers
I have three files, but i can only modify LinkedList.cpp. Can you help me fill it out to make the program build a doubly linked list from entered numbers.
===============Main.cpp==================
#include "LinkedList.h"
int main()
{
//initialize our pointer to NULL
LinkedList LL;
int input;
cout << "Please input some numbers." << endl;
do
{
cout << "#";
cin >> input;
while (!cin)
{
cin.clear();
cin.ignore(2000, ' ');
cout << "Not a legal integer" << endl;
cout << "#: ";
cin >> input;
}
//if they're positive, add them to the linked list
if (input > 0)
LL.Add(input);
} while (input > 0);
cout << "The numbers you entered are:" << endl;
//this just prints them in order
LL.PrintList();
//the list is deleted in the class
return 0;
}
===========LinkedList.h=============
#include
//a basic node for a doubly linked list with data struct Node { int data; Node *next; Node *prev; };
//a linked list class we will use class LinkedList { public: LinkedList(); ~LinkedList(); void Add(int); void PrintList(); private: void DeleteList(); Node* Head; };
========LinkedList.cpp=========
#include "LinkedList.h" //constructor LinkedList::LinkedList() { //should set head pointer } //destructor LinkedList::~LinkedList() { //should call delete list } /* This function adds a new node to the end of the doubly linked list. If it is the first node, then head will still be NULL. Here we are using head directly, so that when we create a new node it stores it in the variable being used in main. head <=> [2|] <=> [8|] <=> [6|X] */ void LinkedList::Add(int newdata) { //create a new node to hold the data with a terminal (NULL) next pointer //check whether head has been initialized (is NULL) //if not, make the new node head and set prev //if head has been initialized //find the end of the chain with a pointer //add the new node on to the last node in the list //set pointers both forward and backwards } //This function walks through the list and prints the integer for each of the nodes //We are using a copy of head, so changing it's value does not alter the //variable in main void LinkedList::PrintList() { //walk through the list starting at head //print the data for the current node } //This function walks through the list and deletes each node void LinkedList::DeleteList() { //make sure the list has data if(Head != NULL) { //delete first node Node *next = Head->next; delete Head; //loop through list deleting each node while (next != NULL) { Head = next; next = Head->next; delete Head; } //set pointer to null Head = NULL; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started