简体   繁体   English

如何在队列中查找特定元素?

[英]How to find an especific element on a queue?

I'm really newbie in c++, and now I'm trying to find a specific number in a queue, but it doesn't work, I'm getting this error: 我真的是C ++的新手,现在我正在尝试在队列中查找特定的数字,但是它不起作用,我收到此错误:

  • Line 34 cannot convert 'node' to 'node*' in initialization. 第34行无法在初始化时将“节点”转换为“节点*”。
  • Line 71 could not convert 'q' from 'node*' to 'node'. 第71行无法将“ q”从“ node *”转换为“ node”。

what I'm doing is this: 我在做什么是这样的:

#include <iostream>
using namespace std;

typedef struct node {
        int data;
        node * next;
        }queue;
        queue *Q, *first, *final=NULL;


void push()
{
     int number;    
     cout<<"Enter the number: ";
     cin>>number;
     Q=new node; 
     Q->data=number;
     Q->next=NULL;
     if(first==NULL)
     {
        final=first=Q;
     }
     else
     {
         final->next = Q;
         final = Q;    

     }
}

void findElement(node q ,int number)
{

     node * Q=q;
     int i=0,flag=0;

     while(Q!=NULL)
     {
        if(Q->data==number)
        {
           cout<<"Found in position: "<<i<<endl;
           flag=1;
        }
        Q=Q->next;
        i++;
     }
     if(flag==0)
     {
        cout<<"\n\nNot found..."<<endl;
     }
}

int main()
{
    int number,x;
    node *q=NULL;

    cout<<"1 Push"<<endl;
    cout<<"2 Find"<<endl;
    cin>>x;
    do
    {  
        switch (x)
        {
        case 1:
            push();
            break;
        case 2:
            cout<<"\n\nEnter the number you want to find: ";
            cin>>number;
            findElement(q,number);
            break;
        default:
            cout<<"Invalid option..."<<endl;
            break;

        }
    }while (x!=0); 
}

I know that my program might be really bad, but I'm trying really hard to learn, so please if you can tell me what I'm doing wrong it'd mean so much to me. 我知道我的程序可能真的很糟糕,但是我正在努力学习,所以请您告诉我我做错了什么对我来说意义重大。 Thanks in advance. 提前致谢。

You may have other issues involved, but for those specific errors, you are trying to copy a node to a node*. 您可能还涉及其他问题,但是对于那些特定的错误,您正在尝试将节点复制到节点*。 The latter is a pointer. 后者是一个指针。

//This won't work.
node * Q=q;

// This will.  (However...)
node* Q = &q;

Similarly, on line 71 同样,在第71行

findElement( q, number);

q is of type node* (pointer to node), but findElement takes type node. q的类型为node *(指向节点的指针),但是findElement的类型为node。 Perhaps you should change findElement( node q, int number ) to findElement( node* q, int number ) 也许您应该将findElement(node q,int number)更改为findElement(node * q,int number)

Pointers can take a while to wrap your head around, and the syntax is subtle. 指针可能需要花费一些时间才能使您的头脑转转,而且语法很微妙。 They are a key point to the language though. 它们是该语言的关键点。
http://www.cplusplus.com/doc/tutorial/pointers/ http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm http://www.cplusplus.com/doc/tutorial/pointers/ http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM