C++ Linked List. Hello! I was wondering if someone would be able to walk me thro
ID: 3694383 • Letter: C
Question
C++ Linked List. Hello! I was wondering if someone would be able to walk me through how to do this example problem for linked list. Below is the LinkedList.cpp and the LinkedList.h. It says that in LinkedList.cpp code needs to be added to print out the linked list ( the LinkedList::printList() ) and code has to be added to add a single node element to the end of the list ( LikedList::appendItem() ) and the code for deleting a node (LinkedList::deleteItem() ). I'd really love to evaluate and learn from this code so any help would be appreciated, thank you!
//LinkedList.cpp
#include
#include "LinkedList.h"
using namespace std;
//---------------------------------------------------
//List Element Members
//---------------------------------------------------
ListElement::ListElement(int d, ListElement * n)
{
datum = d;
next = n;
}
int ListElement::getDatum() const
{
return datum;
}
ListElement const* ListElement::getNext() const
{
return next;
}
//---------------------------------------------------
//LinkedList Members
//---------------------------------------------------
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::insertItem(int item)
{
ListElement *currPtr = head;
ListElement *prevPtr = NULL;
ListElement *newNodePtr; //points to a new node
while (currPtr != NULL && item > currPtr->datum)
{
prevPtr = currPtr;
currPtr = currPtr->next;
}
newNodePtr = new ListElement(item, currPtr);
if (prevPtr == NULL) // insert at the beginning
head = newNodePtr;
else
prevPtr->next = newNodePtr;
}
void LinkedList::makeList()
{
int InValue;
ListElement *currPtr;
ListElement *newNodePtr;
cout << "Enter values for a linked list, one per line." << endl
<< "Enter 999 to end the list." << endl;
cin >> InValue;
//Adding elements to end so that "next" will be NULL
newNodePtr = new ListElement(InValue, NULL);
head = newNodePtr; //assign head to the first Node
currPtr = head;
cin >> InValue;
while (InValue != 999)
{
newNodePtr = new ListElement(InValue, NULL);
currPtr->next = newNodePtr; //link the new node to list
currPtr = newNodePtr; //move the currPtr so it is at end of list
cin >> InValue;
}
}
void LinkedList::appendItem(int item)
{
//REPLACE THE LINE BELOW WITH CODE
cout << "You must implement this function" << endl;
}
void LinkedList::deleteItem(int item)
{
//REPLACE THE LINE BELOW WITH CODE
cout << "You must implement this function" << endl;
}
void LinkedList::printList()
{
//REPLACE THE LINE BELOW WITH CODE
cout << "You must implement this function" << endl;
}
// LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include
#include
using namespace std;
class LinkedList; // needed for friend line below
class ListElement
{
private:
int datum;
ListElement* next;
public:
ListElement(int, ListElement*);
int getDatum() const;
ListElement const* getNext() const;
friend class LinkedList; // gives LinkedList access to datum and next
};
class LinkedList
{
private:
ListElement* head;
public:
LinkedList();
void insertItem(int);
void makeList();
void appendItem(int);
void deleteItem(int);
void printList();
};
#endif
I'm not sure if you would like to have this or not but below is the main.cpp (thank you again :) )
#include<iostream>
#include"LinkedList.h"
#include <string>
using namespace std;
int readInt(string);
int main()
{
char choice;
int item;
LinkedList a;
cout << "This program demonstrates the linked list. " << endl;
cout << "Initially, you will be asked to create the list." << endl;
cout << "You will be later prompted to manipulate the list." << endl << endl;
a.makeList();
choice = 'b';
while (choice != 'q')
{
cout << "*******************************************************" << endl;
cout << "i: Insert (Insert an element and keep the list ordered) ";
cout << "a: Append (Append an element to the end of the list) ";
cout << "d: Delete (Delete a node with the given value) ";
cout << "p: Print (Print the content of the current list) ";
cout << "q: Quit (Quit the program) ";
cout << "*******************************************************" << endl << endl;
cout << " Please enter your choice here:";
cin >> choice;
switch (choice)
{
case 'i':
item = readInt("to insert:");
a.insertItem(item);
break;
case 'a':
item = readInt("to append to the end:");
a.appendItem(item);
break;
case 'd':
item = readInt("to delete:");
a.deleteItem(item);
break;
case 'p':
cout << "The content of the current ordered list is: " << endl;
a.printList();
break;
case 'q':
break;
default:
cout << " Invalid choice. Please try again. ";
break;
}
}
cout << " Bye ";
return 0;
}
int readInt(string descr)
{
int item;
cout << " Please enter an integer value " << descr;
cin >> item;
while (item < 0)
{
cout << " Please try again:";
cin >> item;
}
return item;
}
Explanation / Answer
/* * C++ Program to Implement Singly Linked List */ #include #include #include using namespace std; /* * Node Declaration */ struct node { int info; struct node *next; }*start; /* * Class Declaration */ class single_llist { public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void sort(); void search(); void update(); void reverse(); void display(); single_llist() { start = NULL; } }; /* * Main :contains menu */ main() { int choice, nodes, element, position, i; single_llist sl; start = NULL; while (1) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.