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

C++ Linked List Program Help Rating all working solution thumbs up! My code is p

ID: 3721567 • Letter: C

Question

C++ Linked List Program Help

Rating all working solution thumbs up! My code is posted below.

Assignment Details:

My code:

#include <iostream>

using namespace std;

/*Linked List requirements

   Finding a node, removing a node, displaying the entire list,

   adding at the head.

*/

template <typename T>

struct node

{

   T data;

   node<T> *next;

};

template <typename T>

class llist

{

public:

   void search(T item);

private:

   node<T> *head;

   node<T> *tail;

};

/*

template <typename T>

void llist::search(T item)

{

   for (int i = 0; i < head; i++)

   {

       if(numNodes[i] == item)

           return true;

   }

   return false;

}

*/

int main()

{

/*

   llist<int> ilist;

   ilist.add(100);

   ilist.add(200);

   ilist.add(50);

   ilist.display();

   int location = ilist.find(200);

   if (location >= 0)

       cout << "Found 200 at location " << location << endl;

   else

       cout << "Could not find 200!" << endl;

   location = ilist.find(1000);

   if (location >= 0)

       cout << "Found 1000 at location " << location << endl;

   else

       count << "Could not find 1000!" << endl;

   ilist.remove(100);

   ilist.remove(800);

   ilist.remove(50);

   ilist.display();

   location = ilist.find(200);

   if (location >= 0)

       cout << "Found 200 at location " << location << endl;

   else

       cout << "Could not find 200!" << endl;

   location = ilist.find(100);

   if (location >= 0)

       cout << "Found 100 at location " << location << endl;

   else

       cout << "Could not find 100!" << endl;

*/

   return 0;

}

This assignment will involve creating a linked list class consisting of nodes that will, in addition to the requisite link, contain a piece of data determined by the use of a template. You must support finding a node, removing a node, displaying the entire list, and adding (at the head). You must also implement a destructor, but you do not need to rewrite the copy constructor or the You may assume that all data stored in the linked list will be unique (i.e. if you find a specific key then you will only find it once). Your main body should be as shown below int main () llist

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


#include <iostream>

using namespace std;

/*Linked List requirements

Finding a node, removing a node, displaying the entire list,

adding at the head.

*/

template <typename T>
struct node
{

T data;
node<T> *next;
};

template <typename T>
class llist
{
public:
llist();
~llist();
void add(T item);
void display();
int find(T item);
void remove(T item);
private:

node<T> *head;

};

template <typename T>
llist<T>::llist()
{
head = nullptr;
}

template <typename T>
llist<T>::~llist()
{
node<T> *next;
while(head != nullptr)
{
next = head->next;
delete head;
head = next;
}
}

template <typename T>
void llist<T>::add(T item)
{
node<T> *n = new node<T>();
n->data = item;
n->next = head;
head = n;

}

template <typename T>
void llist<T>::display()
{
node<T> *n = head;
while(n != nullptr)
{
cout << n->data << endl;
n = n->next;
}
}

template <typename T>
int llist<T>::find(T item)
{
int location = 0;
node<T>* n = head;
while(n != nullptr)
{
if(n->data == item) //found
return location;
location++;
n = n->next;
}

return - 1; //not found
}

template <typename T>
void llist<T>::remove(T item)
{
node<T> *curr = head, *prev = nullptr;
while(curr != nullptr)
{
if(curr->data == item)
break;
prev = curr;
curr = curr->next;
}

if(curr == nullptr) //not found
return;

if(curr == head) //remove head node?
head = curr->next;
else
prev->next = curr->next;

delete curr;

}

int main()

{



llist<int> ilist;

ilist.add(100);

ilist.add(200);

ilist.add(50);

ilist.display();

int location = ilist.find(200);

if (location >= 0)

cout << "Found 200 at location " << location << endl;

else

cout << "Could not find 200!" << endl;

location = ilist.find(1000);

if (location >= 0)

cout << "Found 1000 at location " << location << endl;

else

cout << "Could not find 1000!" << endl;

ilist.remove(100);

ilist.remove(800);

ilist.remove(50);

ilist.display();

location = ilist.find(200);

if (location >= 0)

cout << "Found 200 at location " << location << endl;

else

cout << "Could not find 200!" << endl;

location = ilist.find(100);

if (location >= 0)

cout << "Found 100 at location " << location << endl;

else

cout << "Could not find 100!" << endl;


return 0;

}

output
-----
50
200
100
Found 200 at location 1
Could not find 1000!
200
Found 200 at location 0
Could not find 100!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote