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

Modify the LinkedList.h file and the linkedList file to make accessing the previ

ID: 3808321 • Letter: M

Question

Modify the LinkedList.h file and the linkedList file to make accessing the previous node possible. You do NOT need to implement any of the methods required to move backward through the list - that will be implemented later.

========================================= LinkedList.h

#ifndef LinkedList_h
#define LinkedList_h

#include <iostream>
#include <string>
using namespace std;

//***********************************************************************************
// Node structs contain data and a pointer to the next node.
// In this project, it will represent a song/artist combination.
//***********************************************************************************
struct node
{
    string song;
    string artist;
    node * next;
};
//***********************************************************************************
// LinkedList is a list of singly-linked nodes.
// In this project, it will represent a song playlist.
//***********************************************************************************
class LinkedList
{
private:
    // Head of the list contains no song data, but points to the song playlist.
    node * head;
    int listLength;
public:
    // Default Constructor creates the head node.
    LinkedList();

     // Setter adds a node to the list at a given position.
    // Takes a node and list position as parameters.
    // Position must be between 1 and the number of data nodes.
    // Returns true if the operation is successful.
    bool insertNode( node * newNode, int position );

       // Setter removes a node by its given position.
    // Returns true if the operation is successful.
    bool removeNode( int position );
    // Prints each node in the list in consecutive order,
    // starting at the head and ending at the tail.
    // Prints list data to the console.
    void printList();

    // Destructor de-allocates memory used by the list.
    ~LinkedList();
};
#endif

================================================ LinkedList.cpp

#include "LinkedList.h"

// Default Constructor creates the head node.
LinkedList::LinkedList()
{
    head = new node;
    head -> song = "head (contains no song data)";
    head -> artist = "head (contains no artist data)";
    head -> next = NULL;
    listLength = 0;
}

// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool LinkedList::insertNode( node * newNode, int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL)
    {
        head -> next = newNode;
        listLength++;
        return true;
    }
    int count = 0;
    node * p = head;
    node * q = head;
    while (q != NULL)
    {
        if (count == position)
        {
            p -> next = newNode;
            newNode -> next = q;
            listLength++;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    if (count == position)
    {
        p -> next = newNode;
        newNode -> next = q;
        listLength++;
        return true;
    }
    cout << "nError: node was not added to list.n";
    return false;
}

// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL)
    {
       cout << "nError: there is nothing to remove.n";
       return false;
    }
    int count = 0;
    node * p = head;
    node * q = head;
    while (q)
    {
        if (count == position)
        {
            p -> next = q -> next;
            delete q;
            listLength--;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    cout << "nError: nothing was removed from the list.n";
    return false;
}

// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints the data to the console.
void LinkedList::printList()
{
    int count = 0;
    node * p = head;
    node * q = head;
    cout << "n---------------------------n";
    cout << "Song Playlist n";
    while (q)
    {
        p = q;
        cout << "n-----------------------------n";
        cout << "t position: " << count << endl;
        cout << "t song: " << p -> song << endl;
        cout << "t artist: " << p -> artist << endl;
        q = p -> next;
        count++;
    }
}

// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList()
{
    node * p = head;
    node * q = head;
    while (q)
    {
        p = q;
        q = p -> next;
        if (q) delete p;
    }
}

Explanation / Answer

#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
#include <string>
using namespace std;
//***********************************************************************************
// Node structs contain data and a pointer to the next node.
// In this project, it will represent a song/artist combination.
//***********************************************************************************
struct node
{
string song;
string artist;
node * next;
   node * prev;
};
//***********************************************************************************
// LinkedList is a list of singly-linked nodes.
// In this project, it will represent a song playlist.
//***********************************************************************************
class LinkedList
{
private:
// Head of the list contains no song data, but points to the song playlist.
node * head;
int listLength;
public:
// Default Constructor creates the head node.
LinkedList();
// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool insertNode( node * newNode, int position );
// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool removeNode( int position );
// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints list data to the console.
void printList();
// Destructor de-allocates memory used by the list.
~LinkedList();
};
#endif
================================================ LinkedList.cpp
#include "LinkedList.h"
// Default Constructor creates the head node.
LinkedList::LinkedList()
{
head = new node;
head -> song = "head (contains no song data)";
head -> artist = "head (contains no artist data)";
head -> next = NULL;
   head -> prev = NULL;
listLength = 0;
}
// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool LinkedList::insertNode( node * newNode, int position )
{
if ((position <= 0) || (position > listLength + 1))
{
cout << "nError: the given position is out of range.n";
return false;
}
if (head -> next == NULL)
{
head -> next = newNode;
       newNode -> prev = head;
listLength++;
return true;
}
int count = 0;
node * p = head;
node * q = head;
while (q != NULL)
{
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
           newNode -> prev = p;
           q -> prev = newNode;
listLength++;
return true;
}
p = q;
q = p -> next;
count++;
}
if (count == position)
{
p -> next = newNode;
newNode -> next = q;
       newNode -> prev = p;
       q -> prev = newNode;      
listLength++;
return true;
}
cout << "nError: node was not added to list.n";
return false;
}
// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
if ((position <= 0) || (position > listLength + 1))
{
cout << "nError: the given position is out of range.n";
return false;
}
if (head -> next == NULL)
{
cout << "nError: there is nothing to remove.n";
return false;
}
int count = 0;
node * p = head;
node * q = head;
while (q)
{
if (count == position)
{
p -> next = q -> next;
           q -> next -> prev =p;
delete q;
listLength--;
return true;
}
p = q;
q = p -> next;
count++;
}
cout << "nError: nothing was removed from the list.n";
return false;
}
// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints the data to the console.
void LinkedList::printList()
{
int count = 0;
node * p = head;
node * q = head;
cout << "n---------------------------n";
cout << "Song Playlist n";
while (q)
{
p = q;
cout << "n-----------------------------n";
cout << "t position: " << count << endl;
cout << "t song: " << p -> song << endl;
cout << "t artist: " << p -> artist << endl;
q = p -> next;
count++;
}
}
// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList()
{
node * p = head;
node * q = head;
while (q)
{
p = q;
q = p -> next;
if (q) delete p;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote