首页 > 解决方案 > 如何显示更大的数据链表

问题描述

我刚学会使用链表显示数据,我想添加函数来显示大于 10 的数据,但是出现错误 ISO C++ Forbids Comparison Between Pointer and Integer [-fpermissive]。我不知道如何修复我的代码

void DisplayList(void){
            Node* temp = new Node;
            temp = head;
            while(temp != NULL){
                cout<<temp -> data<<" ";
                temp = temp->next;
            }
            cout<<endl;
        }

void GreaterList(void){
            Node* temp = new Node;
            temp =head;
            while(temp != NULL){
                if(temp >= 10){
                cout<<temp->data<<" ";
                temp = temp->next;
            }
            }
        }

标签: c++linked-list

解决方案


您忘记从节点获取数据进行比较:

void GreaterList(void){
            Node* temp = new Node;
            temp = head;
            while(temp != NULL){
                if(temp->data >= 10){ // <== Here
                cout<<temp->data << " ";
                temp = temp->next;
            }
        }
     }

推荐阅读