Here is an excerpt from the definition of a singly-linked list object represents
ID: 3826548 • Letter: H
Question
Here is an excerpt from the definition of a singly-linked list object represents a singly-linked list of integers. The implementation uses no dummy node, and the list end is indicated by the last node having NULL as its m next data member. The empty list i represented by m head and m tailbeing NULL. class LinkedIist public: creates an empty list LinkedList void push back int v); void unique bool nates (LinkedList& other const return dom (m head, other m head) private struct Node Node (int v, Node n) m value (v), m next (n) f) int m value Node m next points to first Node in the list Node m head points to last Node in the list Node* m tail bool dom (const Node pl const Node p2) const For this problem, will ask you write some function implementations. Be sure we to your code is syntactically correct as well as functionally correct. Notice that the Node type has no default constructor.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
void LinkedList::unique(){
// base case
if(head m_head == NULL || m_head == m_tail){
return;
}
Node *temp = m_head;
// traverse till end
while(temp != NULL){
// remove consutive same element
while(temp->m_next != NULL && temp->m_value == temp->m_next->m_value){
Node *t = temp->m_next;
temp->m_next = temp->m_next->m_next;
delete t;
// if temp reached at end the update tail
if(temp->m_next == NULL)
m_tail = temp;
}
// forwarding temp
temp = temp->m_next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.