📌  相关文章
📜  链表类中的运算符重载'<<‘和’>>’运算符

📅  最后修改于: 2021-05-04 09:08:41             🧑  作者: Mango

先决条件: C++中的运算符重载,C++中的链表

C++附带的库提供了执行输入和输出的方法。在C++中,输入和输出以字节序列(也称为流)的形式执行。输入和输出流由iostream库管理。 cincout是输入流和输出流的标准对象。
我们可以重载‘>>’‘<<‘运算符以在链表中获取输入,并在C++中打印链表中的元素。它具有为运算符提供数据类型特殊含义的能力,这种能力称为“操作员重载”。
重载运算符的语法是:

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

重载istream运算符“ >>”:

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

解释:
上面函数的返回类型是对istream对象的引用。在声明中“ cin >> head; “,CIN是所述第一参数和所述头部的引用是该函数的第二个参数。当执行任何这样的语句时,将调用上述函数。

重载ostream运算符'<<‘:

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

解释:
上面函数的返回类型是对ostream对象的引用。在陈述中“ cout << head; “,COUT是第一参数和所述头部的引用是该函数的第二个参数。当执行任何这样的语句时,将调用上述函数。

重载'<>’运算符:

下面是用于重载‘>>’‘<<‘运算符,它们连续地将数字N作为输入并将数字N插入链表中,直到l N = -1。

// C++ program to demonstrate the
// overloading of '<<' and '>>'
// operators
#include 
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;
}