Operator Overloading '<<' and '>>' operator in a linked list class
Last Updated :
22 Mar, 2023
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)
Similar Reads
Overloading New and Delete operator in c++ The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes. If these operators are overloaded using member function for a class, it means that these operators are overloaded only
5 min read
Increment (++) and Decrement (--) Operator Overloading in C++ Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types. Why Operator Overloading? Let's say we have defined a class Integer for handling operations on integers. We can have functions add(), subtr
4 min read
Overloading stream insertion (<>) operators in C++ In C++, stream insertion operator "<<" is used for output and extraction operator ">>" is used for input. We must know the following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object of istream class 2) These operators must be
2 min read
Input/Output Operators Overloading in C++ Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We c
2 min read
Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the
8 min read
How to create a pointer to another pointer in a linked list? In this article we cover how to create a pointer to another pointer in a linked list, it can be singly, doubly, or circularly. What is Linked List: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked
7 min read
C++ Assignment Operator Overloading Prerequisite: Operator OverloadingThe assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.Assignment operator overloading is binary operator overloading.Overloading ass
4 min read
deque::operator= and deque::operator[] in C++ STL Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may
4 min read
Different Ways of Operator Overloading in C++ In C++, operator overloading is the concept that allows us to redefine the behavior of the already existing operator for our class. C++ provides a special function called operator function that can be used to achieve operator overloading. In this article, we will learn the different ways in which we
6 min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read