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
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
XOR Linked List â A Memory Efficient Doubly Linked List | Set 2 In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert
10 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1 In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists.We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR link
15+ min read
Introduction to Linked List - Data Structure and Algorithm Tutorials Linked List is basically chains of nodes where each node contains information such as data and a pointer to the next node in the chain. It is a popular data structure with a wide range of real-world applications. Unlike Arrays, Linked List elements are not stored at a contiguous location. In the lin
9 min read
Sorted insert in a doubly linked list with head and tail pointers A Doubly linked list is a linked list that consists of a set of sequentially linked records called nodes. Each node contains two fields that are references to the previous and to the next node in the sequence of nodes. The task is to create a doubly linked list by inserting nodes such that list rema
10 min read