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

Design and implement your own linked list template class and methods. You will t

ID: 3764933 • Letter: D

Question

Design and implement your own linked list template class and methods. You will test your new template class using a driver program. The driver program should do the following:


• Create a list of integers,
• Add five integers to the list,
• Display the list,
• Remove a selected integer,
• Display the list,
• Empty the list.


• Create a list of strings,
• Add five strings to the list,
• Display the list,
• Remove a selected string,
• Display the list,
• Empty the list and exit.

The list should keep its elements ordered (ascending).

Your implementation should not use std:list, but rather build one using pointers. Use the example in the textbook and slides.

Starting out with C++ 8th edition

Explanation / Answer

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

class LinkedList
{
private:
struct Node
{
string name;
int number;
struct Node *next;
};


Node *head;

public:
LinkedList()
{head = NULL; }
void addLast(string, int);
void Insert(string, int);
void Delete(string, int);
void Display() const; //ABHI: What is the need to put const??
};

void LinkedList::addLast(string data1, int data2)
{
Node *newNode;
Node *nodePtr;

newNode = new Node;
newNode->name = data1;
newNode->number = data2;
newNode->next = NULL;

if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void LinkedList::Insert(string data1, int data2)
{
int comp;
Node *newNode;
Node *nodePtr;
Node *prevNode = NULL;

newNode = new Node;
newNode->name = data1;
newNode->number = data2;

if(!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
prevNode = NULL;

while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))
//ABHI: Should not it be nodePtr->next??
{
prevNode = nodePtr;
nodePtr = nodePtr->next;
}

if(prevNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void LinkedList::Delete(string data1, int data2)
{
int comp;
Node *nodePtr;
Node *prevNode;

if(!head)
return;


if(nodePtr->name.compare(data1) == 0)
{
nodePtr = head-> next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
{
prevNode = nodePtr;
nodePtr = nodePtr-> next;
}

if(nodePtr)
{
prevNode->next = nodePtr->next;
delete nodePtr;
}
}
}

void LinkedList::Display() const
{
Node *nodePtr;
nodePtr = head;

while(nodePtr)
{
cout << nodePtr->name << nodePtr->number << endl;
nodePtr = nodePtr->next;
}
}
int main()
{
ifstream inFile;
const int SIZE = 51;
string nm, info[SIZE],num;// input, num;
string input;
char ch;
int i = 0;

inFile.open("/home/abhishek/view/sample_abhi/cpluplus/cppluvr8.txt");
if(!inFile)
{
cout << "Error! File cannot open." << endl;
return 0;
}
else
cout << "File has been opened." << endl;

getline(inFile,input);
while(!inFile.eof())
{
if (input == "%INSERT")
{
while(
(input != "%DELETE") && (input != "%SEARCH") && (input != "%PRINT") && (input != "%END")
)
{
getline(inFile,input);
cout << input << endl;
}
continue;
}
else if (input == "%DELETE")
{
cout << "delete" << endl;
}
else if (input == "%SEARCH")
{
cout << "search" << endl;
}
else if (input == "%PRINT")
cout << "print" << endl;
else if (input == "%END")
return 0;
//cout << "File has been closed." << endl;
// return 0;
getline(inFile,input);
}

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