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

Linked Lists Problem I have a class node, which contains data and a value. I wou

ID: 3841163 • Letter: L

Question

Linked Lists Problem

I have a class node, which contains data and a value.

I would like to create objects of my Node class and put them into a Linked List.

I have trouble making my: Insert, Remove, Print and get value functions

template
class Node
{
public:
   Node(const T& data = T(), int value = 0, Node* next = nullptr)
       : data(data), value(value), next(next) {}
   T data;
   int value;
   Node* next;
};

Here is my LinkedList Class:

template
class Queue
{
public:

   // PRECONDITION: None
   // POSTCONDITIONCONDITION: The queue is empty
   Queue()

   {
       head = NULL; // Empty list with no objects.
   }

   void headInsert(T data, T value)
   {
       head = new Node(data, value, head); // head pointer assignment.
   }

   // PRECONDITION: value > 0
   // POSTCONDITION: Object of Node, which contains (data,value) has been inserted
   void insert(const T& data, int value)

   {
       if (value > 0)
       {
           Node* next = tail->next;
           tail->next = new Node(data, value, next); // Insertion of new Node
       }
   }

   // PRECONDITION: None
   // POSTCONDITION: I search for data, and delete the object which contains that value of data.
   void remove(const T& data)
   {

       if (tail == NULL) return;
       if (tail->next == NULL) return;

       //...
   }


   // PRECONDITION: None
   // POSTCONDITION: Prints out all objects in the Linked List
   void print()
   {
      
   }

   // PRECONDITION: -
   // POSTCONDITION: - Returns the "value" that correspons with the data.
   int getvalue(const T& data) const
   {
      
return 0;
   }

   // PRECONDITION: -
   // POSTCONDITION: Returns 'trueø if the queue is empty, 'false' otherwise
   bool isEmpty() const

   {
       if (head == NULL) return true;
       return false;
   }

   ~Queue()
   {
       while (!isEmpty()) remove(head->data);
   }

private:
   Node* head;
};

Explanation / Answer

PROGRAM CODE:

#include <iostream>
using namespace std;

template<class T>
class Node
{
public:
Node(const T& data = T(), int value = 0, Node* next = NULL)
: data(data), value(value), next(next) {}
T data;
int value;
Node* next;
};
template<class T>
class Queue
{
public:
// PRECONDITION: None
// POSTCONDITIONCONDITION: The queue is empty
Queue()
{
head = NULL; // Empty list with no objects.
}
/* void headInsert(T data, T value)
{
head = new Node(data, value, head); // head pointer assignment.
}*/
// PRECONDITION: value > 0
// POSTCONDITION: Object of Node, which contains (data,value) has been inserted
void insert(const T& data, int value)
{
Node<T> *node = new Node<T>(data, value);
if(head==NULL)
{
    head = node;
}else
{
    Node<T> *temp = head;
    while(temp->next != NULL)
    temp = temp->next;
    temp->next = node;
}
}
// PRECONDITION: None
// POSTCONDITION: I search for data, and delete the object which contains that value of data.
void remove(const T& data)
{
if(head == NULL)
    return;
else
{
   
    if(head->data == data)
        head = head->next;
else
{
        Node<T> *temp = head;
      while(temp->next != NULL)
      {
      if(temp->next->data == data)
      {
         temp->next = temp->next->next;
             break;
      }
      }
}
}
}

// PRECONDITION: None
// POSTCONDITION: Prints out all objects in the Linked List
void print()
{
Node<T> *temp = head;
while(temp != NULL)
{
    cout<<temp->data<<" "<<temp->value<<endl;
    temp = temp->next;
}
}
// PRECONDITION: -
// POSTCONDITION: - Returns the "value" that correspons with the data.
int getvalue(const T& data) const
{
Node<T> *temp = head;
while(temp != NULL)
{
    if(temp->data == data)
       return temp->value;
    temp = temp->next;
}
return 0;
}
// PRECONDITION: -
// POSTCONDITION: Returns 'trueø if the queue is empty, 'false' otherwise
bool isEmpty() const
{
return (head==NULL);
}
~Queue()
{
while (!isEmpty()) remove(head->data);
}
private:
Node<T>* head;
};
int main() {
   Queue<string> queue;
   queue.insert("Me", 1);
   queue.insert("You", 2);
   queue.print();
   queue.remove("Me");
   cout<<"After removing 'Me': "<<endl;
   queue.print();
   return 0;
}

OUTPUT:

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