Question

Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

Please show me how to overload the operators << and >>

#ifndef LINK_LIST
#define LINK_LIST

#include <iostream>
using namespace std;

template <typename T>
struct Int_Node
{
   T value;
   Int_Node<T> *pre, *next;
};

template <typename T>
class Link_List
{
   template <typename U>
   friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list
   template <typename U>
   friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);

public:
   Link_List();// default constructor
   Link_List(const Link_List &);// copy constructor
   ~Link_List();
   int getSize() const;
  
   const Link_List &operator=(const Link_List &);// assignment operator
   bool operator==(const Link_List &right) const// equality operator
   {
       return (*this == right);
   }
   bool operator!=(const Link_List &right) const// inequality operator
   {
       return !(*this == right);
   }

   T &operator[](int index);// subscript operator for non-const objects
   T operator[](int index) const;// subscript operator for const objects

   bool insert_node(T value);// insert an integer at the back of link list
   bool delete_node();// delete the last node
   bool insert_node(int index, T value);// insert an integer after the i_th position
   bool delete_node(int index);// delete the i_th node

private:
   int size;
   Int_Node<T> *head, *tail;// pointer to the first and the last element of Link_List
};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

template <typename U>
friend ostream &operator<<(ostream &os, const Link_List<U> &Obj)// print all integers in the list
{
   Int_Node<U>*Temp = Obj.head;
   os << "Current List : ";
   while (Temp != NULL)
   {
       os << Temp->value << " ";
       Temp = Temp->next;
   }
   os << endl;
   return os;
}

template <typename U>
friend istream &operator>>(istream &is, Link_List<U> &Obj)// input a value at the back of the list, like insert_node(val)
{
   U val;
   cout << "Enter Value: ";
   is >> val;
   Obj.insert_node(val);
   return is;
}

#pragma once
#include"Node.h"
#include<iostream>
using namespace std;
template <typename T>
class LinkedList // LinkedList Using Head And Tail.
{
private:
   // Top / Front / Head.
   Node<T>*Head;
   // Tail / Back / Current.
   Node<T>*Current;
   // Set Head As It Is A Private Member.
   void SetHead(Node<T> *Val) { this->Head = Val; }
   // Set Tail As It Is A Private Member.
   void SetCurrent(Node<T> *Val) { this->Current = Val; }
   // Get Head As It Is A Private Member.
   Node<T>* GetHead() { return this->Head; }
   // Get Tail As It Is A Private Member.
   Node<T>* GetCurrent() { return this->Current; }
public:
   // Constructor Sets Head And Tail To Null.
   LinkedList() { SetHead(NULL); SetCurrent(NULL); }
   // Copy Constructor.
   LinkedList(LinkedList &obj) // Yes , I Googled This! , Used To Make A New Copy Of Linked List When Passing It To A Function Without Reference So The Orignal List Is Does Not Get Deleted!.
   {
       Node<T>dummy;
       for (Node<T>* o = obj.GetHead(), *n = &dummy; o; o = o->GetNext())
       {
           n->SetNext(new Node<T>(o->GetData()));
           n = n->GetNext();
       }
       this->Head = dummy.GetNext();
   }
   // Returns If List Is Empty Or Not.
   bool IsEmptyList() { if (GetHead() == NULL) return true; return false; }
   // Returns The Data Of Current List From Head.
   T GetHeadData() { return GetHead()->GetData(); }
   // Returns The Data Of Current List From Tail.
   T GetTailData() { return GetCurrent()->GetData(); }
   // Prints The Current List From Head To Tail.
   void Print()
   {
       Node<T>*Temp = GetHead();
       cout << "Current List : ";
       while (Temp != NULL)
       {
           cout << Temp->GetData() << " ";
           Temp = Temp->GetNext();
       }
       cout << endl;
   }
   // Inserts The New Value At Head.
   void InsertAtHead(int Val)
   {
       Node<T>*Temp = new Node<T>;
       Temp->SetData(Val);
       Temp->SetNext(this->Head);
       if (GetHead() == NULL)
       {
           SetHead(Temp);
           SetCurrent(Head);
       }
       else
       {
           Temp->SetNext(GetHead());
           SetHead(Temp);
       }
   }
   // Inserts The New Value At Tail.
   void InsertAtTail(T Val)
   {
       Node<T>*Temp = new Node<T>;
       Temp->SetData(Val);
       Temp->SetNext(NULL);
       if (Head == NULL)
       {
           SetHead(Temp);
           SetCurrent(Head);
       }
       else
       {
           GetCurrent()->SetNext(Temp);
           SetCurrent(Temp);
       }
   }
   // Deletes The Value At Head.
   void DeleteFromHead()
   {
       if (!IsEmptyList())
       {
           if (GetHead()->GetNext() != NULL)
           {
               Node<T>*Temp = GetHead();
               SetHead(GetHead()->GetNext());
               delete Temp;
           }
           else
           {
               Node<T>*Temp = GetHead();
               delete Temp;
               SetHead(NULL);
           }
       }  
   }
   // Deletes The Value At Tail.
   void DeleteFromTail()
   {
       if (!IsEmptyList())
       {
           SetCurrent(GetHead());
           if (GetCurrent() == GetHead() && GetCurrent()->GetNext() == NULL)
           {
               delete GetCurrent();
               SetHead(NULL);
           }
           else
           {
               while (GetCurrent()->GetNext()->GetNext() != NULL)
               {
                   SetCurrent(GetCurrent()->GetNext());
               }
               delete GetCurrent()->GetNext();
               GetCurrent()->SetNext(NULL);
           }
       }
   }
   // Searches The List For A Value, And Returns The Address Of Node Which Contains The Value.
   Node<T>* Search(T Val)
   {
       SetCurrent(GetHead());
       if (IsEmptyList()) return NULL;
       else
       {
           while (GetCurrent() != NULL)
           {
               if (GetCurrent()->GetData() == Val) return GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
           }
           return NULL;
       }
   }
   // Insert A Value, After A Specific Value.
   void InsertAfter(T Check, T Val)
   {
       SetCurrent(GetHead());
       Node<T>* Ptr = Search(Check);
       if (!Ptr) cout << "Value Not Found!" << endl;
       else
       {
           Node<T>* Temp = new Node<T>;
           Temp->SetData(Val);
           Temp->SetNext(Ptr->GetNext());
           Ptr->SetNext(Temp);
       }
   }
   // Insert A Value, Before A Specific Value.
   void InsertBefore(T Check, T Val)
   {
       SetCurrent(GetHead());
       Node<T>* Ptr = Search(Check);
       if (!Ptr) cout << "Value Not Found!" << endl;
       else
       {
           Node<T>* Temp = new Node<T>;
           Temp->SetData(Ptr->GetData());
           Ptr->SetData(Val);
           Temp->SetNext(Ptr->GetNext());
           Ptr->SetNext(Temp);
       }
   }  
   // Sorts The List Value By Value In Descending, Not Node By Node, Until The List Is Fully In Order.
   void SortDescending()
   {
       Node<T>* Temp;
       bool Swap = true;
       while(Swap)
       {
           Swap = false;
           SetCurrent(Head);
           while (GetCurrent())
           {
               Temp = GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
               if (GetCurrent() && Current->GetData() > Temp->GetData())
               {
                   Swap = true;
                   T Val;
                   Val = Temp->GetData();
                   Temp->SetData(GetCurrent()->GetData());
                   GetCurrent()->SetData(Val);
               }
           }
       }
   }
   // Sorts The List Value By Value In Ascending, Not Node By Node, Until The List Is Fully In Order.
   void SortAscending()
   {
       Node<T>* Temp;
       bool Swap = true;
       while (Swap)
       {
           Swap = false;
           SetCurrent(Head);
           while (GetCurrent())
           {
               Temp = GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
               if (GetCurrent() && Current->GetData() < Temp->GetData())
               {
                   Swap = true;
                   T Val;
                   Val = Temp->GetData();
                   Temp->SetData(GetCurrent()->GetData());
                   GetCurrent()->SetData(Val);
               }
           }
       }
   }
   // Default Destructor.
   ~LinkedList() = default;
};
#pragma once
template <typename T>
class Node
{
private:
   T Data;
   Node* Next;
public:
   T GetData() { return this->Data; }
   void SetData(T Val) { this->Data = Val; }
   Node* GetNext() { return this->Next; }
   void SetNext(Node* Val) { this->Next = Val; }
   // SetData Cannot Be NULL For Some Structs, So Remove This Statement.
   Node() { this->SetData(NULL); this->SetNext(NULL); }
   Node(T Val) { this->SetData(Val); }
   ~Node() = default;
};

IF THERE IS ANY PROBLEM OR YOU HAVE ANY QUERY REGARDING THE SOLUTION KINDLY FEEL FREE TO ASK, AND LEAVE A THUMBS UP WHEN YOU ARE SATISFIED. THANKS!.

Add a comment
Know the answer?
Add Answer to:
Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT