Operator Overloading ‘<<‘ and ‘>>’ operator in a linked list class

Prerequisite: Operator Overloading in C++, Linked List in C++

C++ comes with libraries that provide ways for performing Input and Output. In C++, Input and Output are performed as a sequence of bytes, also known as streams. Input and Output stream are managed by the iostream library. cin and cout are the standard objects for the input stream and output stream.
We can overload the ‘>>’ and ‘<<‘ operators to take input in a linked list and print the element in the linked list in C++. It has the ability to provide the operators with a special meaning for a data type, this ability is known as Operator Overloading.
The syntax for overloading an operator are:

returnType operator symbol (arguments)
{
   Operations...
} 

Overloading the istream operator ‘>>’:

filter_none

edit
close

play_arrow

link
brightness_4
code

istream& operator>>(istream& is, node*& head)
{
    // Function call to overload the ">>"
    // operator
    takeInput(head);
}
chevron_right

Explanation:
The return type for the above function is a reference to the istream object. In the statement “ cin >> head; “, cin is the first parameter and a reference to the head is the second parameter for the function. The above function is called when any such statement is executed.

Overloading the ostream operator ‘<<‘:



filter_none

edit
close

play_arrow

link
brightness_4
code

ostream& operator<<(ostream& os, node* head)
{
    // Function call to overload the "<<"
    // operator
    print(head);
}
chevron_right

Explanation:
The return type for the above function is a reference to the ostream object. In the statement “ cout << head;“, cout is the first parameter and a reference to the head is the second parameter for the function. The above function is called when any such statement is executed.

Code for Overloading of ‘<>’ operator:

Below is the code for overloading of ‘>>’ and ‘<<‘ operators, which takes a number N as an input continuosly and insert the number N in the linked list untill N = -1.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the
// overloading of '<<' and '>>'
// operators
#include <iostream>
using namespace std;
  
// Class for each node object
// of the linked list
class node {
public:
    // Node of the linked list
    int data;
    node* next;
  
    // Constructor of node class
    node(int d)
    {
        data = d;
        next = NULL;
    }
};
  
// Insert a node at head of linked
// list
void insertAtHead(node*& head, int d)
{
    node* n = new node(d);
    n->next = head;
    head = n;
}
  
// Insert a node at tail of linked
// list
void insertAtTail(node* head, int data)
{
    // Make new node using
    // constructor
    node* n = new node(data);
    node* temp = head;
  
    // Traverse till we get to end of
    // the linked list
    while (temp->next != NULL)
        temp = temp->next;
  
    // Append the new node n at the end
    // of the linked list
    temp->next = n;
}
  
// Print the node at the linked list
void print(node* head)
{
    // Print the first Node
    if (head != NULL) {
        cout << head->data;
        head = head->next;
    }
  
    // Traverse till head traverse
    // till end
    while (head != NULL) {
        cout << "->" << head->data;
        head = head->next;
    }
}
  
// Function that takes continuous input
// until user enter -1 while initializing
// the linked list.
void takeInput(node*& head)
{
    int n;
    cin >> n;
  
    // If n is not equals to -1 insert
    // the node in the linked list
    while (n != -1) {
  
        // If head is NULL, insert at
        // the beginning of list
        if (head == NULL)
            insertAtHead(head, n);
        else
            insertAtTail(head, n);
        cin >> n;
    }
}
  
// Overloading the ostream operator '<<'
// to print the complete linked list from
// beginning
ostream& operator<<(ostream& os, node* head)
{
    print(head);
}
  
// Overloading the istream operator '>>'
// to take continuous input into the linked
// list until user inputs -1
istream& operator>>(istream& is, node*& head)
{
    takeInput(head);
}
  
// Driver Code
int main()
{
    // initialise head to NULL
    node* head = NULL;
  
    // Overloading of '>>' for inserting
    // element in the linked list
    cin >> head;
  
    // Overloading of '<<' for printing
    // element in the linked list
    cout << head;
    return 0;
}
chevron_right

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :