Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 ‘>>’:  

C++




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


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 ‘<<‘: 

C++




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


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 continuously and insert the number N in the linked list until N = -1.

C++




// 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;
}


Input and Output:

Input: 5 4 3 2 -1
Output: 5->4->3->2

Time Complexity: O(n)

Space Complexity: O(n)



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads