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

3. [40 points in all) Here is an excerpt from the definition of singly linked Li

ID: 3786478 • Letter: 3

Question

3. [40 points in all) Here is an excerpt from the definition of singly linked List object represents a singly-linked list of integers. implementation uses no dummy The node, and the end is indicated by the last node having nullptr as its m next data member. The empty list is represented by m head and m tail being nullptr. class LinkedList public: creates an empty list LinkedList void push back (int v); void remove copiesofSecond private: struct Node Node (int v Node* n. m value (v), m next (n) int m value Node m next Node m head points to first Node in the list Node m tail points to last Node in the list For this problem, we will ask you to write s function implementations. Be sure code is syntactically correct as well as functionally correct. Notice that the Node type has no default constructor.

Explanation / Answer

/*
* LinkedList.h
*
* Created on: Feb 3, 2017
* Author: swetaprakash
*/

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include <iostream>
#include <cstddef>

using std::cout;
using std::endl;

class LinkedList {
public:
   LinkedList();
   virtual ~LinkedList();

   void push_back(int v);
   void removeCopiesOfSecond();
private:
   struct Node
   {
       Node(int v,Node *n): m_value(v),m_next(n){}
       int m_value;
       Node* m_next;
   };
   Node* m_head;
   Node* m_tail;

   };
   void LinkedList::push_back(int v)
   {
       Node *p;
       if(m_head == NULL)
           m_head = new Node (v, NULL);
       else
       {
           p=m_head;
           while(p->m_next !=NULL)
               p=p->m_next;
           p->m_next = new Node (v, NULL);
       }
   }
   void LinkedList::removeCopiesOfSecond()
   {
       //list is empty
       if(m_head == NULL)
       {
           return;
       }
       //doesnot hav 2nd node
       if(m_head->m_next==NULL)
       {
           return;
       }

  int target = m_head->m_next->m_value;
       if(m_head->m_value==target)
       {
           Node* temp = m_head;
           m_head = temp->m_next;
           delete temp;
           m_tail = m_head->m_next;
       }
       else
       {
           m_tail = m_head->m_next;
       }

       /* traverse the list and check the value of each node */
       while (m_tail != NULL) {
           Node* p = m_head;
       if (m_tail->m_value == target) {
       /* Update the list */
       p->m_next = m_tail->m_next;
       delete m_tail; /* Here only remove the first node with the given value */
       }
       else
       {
           m_tail= m_tail->m_next;
       }
       }

   }

#endif /* LINKEDLIST_H_ */

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