Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

Students also viewed these Databases questions

Question

2. Record these without judgments, criticisms, or comments.

Answered: 1 week ago

Question

Tell the merits and demerits of Mendeleev's periodic table.

Answered: 1 week ago

Question

Be familiar with the basic ways to manage capacity.

Answered: 1 week ago

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago