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

(c++) I\'m try to to check the equality of two lists using the operator== , is m

ID: 3770382 • Letter: #

Question

(c++) I'm try to  to check the equality of two lists using the operator== , is my code correct? can anyone check my code ? thanks

==================================================================

template <class T>

class LinkedList
{
public:
T m_data;
LinkedList<T>* m_next;
=======================================================================
template <typename T>
const LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& rhs)
{
if(this!=&rhs)
{
    LinkedList<T> *tmp_this=this;
    const LinkedList<T> *tmp_rhs=&rhs;
    while(tmp_rhs->m_next!=NULL)
    {
        tmp_this->m_data=tmp_rhs->m_data;
        tmp_this->m_next=tmp_rhs->m_next;
        delete tmp_this;
        delete tmp_rhs;
        tmp_this=tmp_this->m_next;
        tmp_rhs=tmp_rhs->m_next;
    }
    return *this;
}
}

Explanation / Answer

import java.util.Queue;
import java.util.LinkedList;
public class TreeTraverse
{
private static class Node<T>
{
public Node<T> left;
public Node<T>right;
public T data;
public Node (T data)
{
this.data=data;
}
public Node <T>getleft()
{
return this.left;
}
public void setleft(Node <T>left)
{
this.left=left;
}
public Node<T>getright()
{
return this.right;
}
public void setright(Node<T>right)
{
this.right=right;
}
}
public static void preorder(Node<?>n)
{
if(n!=null)
{
System.out.println(n.data+" ");
preorder(n.getleft());
preorder(n.getright());
}
}
public static void inorder(Node<?>n)
{
if(n!=null)
{
System.out.println(n.data+" ");
preorder(n.getleft());
preorder(n.getright());
}
}
public static void postorder(Node<?>n)
{
if(n!=null)
{
postorder(n.getleft());
postorder(n.getright());
System.out.println(n.data+" ");
}
}
public static void main(final String[]args)
{
Node<Integer>one=new Node<Integer>(1);
Node<Integer>two=new Node<Integer>(2);
Node<Integer>three=new Node<Integer>(3);
Node<Integer>four=new Node<Integer>(4);
Node<Integer>five=new Node<Integer>(5);
Node<Integer>six=new Node<Integer>(6);
Node<Integer>seven=new Node<Integer>(7);
Node<Integer>eight=new Node<Integer>(8);
Node<Integer>nine=new Node<Integer>(9);
one.setleft(two);
one.setright(three);
two.setleft(four);
two.setright(five);
three.setleft(six);
four.setleft(seven);
six.setleft(eight);
six.setright(nine);
System.out.println("PREORDER IS:");
preorder(one);
System.out.println();
System.out.println("INORDER IS:");
inorder(one);
System.out.println();
System.out.println("POSTORDER IS:");
postorder(one);
System.out.println();
}
}