Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have three files, but i can only modify LinkedList.cpp. Can you help me fill i

ID: 3587808 • Letter: I

Question

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<iostream>
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=========

Explanation / Answer

#include "LinkedList.h"

//constructor
LinkedList::LinkedList()
{
//should set head pointer
Head = NULL;
}


//destructor
LinkedList::~LinkedList()
{
//should call delete list
DeleteList();
}

/*
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
Node *newNode;
newNode->data = newdata;
newNode->next = NULL;
newNode->prev = NULL;
  
//check whether head has been initialized (is NULL)
//if not, make the new node head and set prev
if (Head == NULL) {
Head = newNode;
return;
}   
  
//if head has been initialized
//find the end of the chain with a pointer
Node *cur = Head;
while(cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
newNode->prev = cur;
nexNode->next = NULL;
  
//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
Node *cur = Head;
while(cur != NULL) {
cout << cur->data << " ";
cur = cur->next;
}
  
//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;
}
}