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, It never show the position.
Please, can you tell me what I'm doing wrong?
Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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;
	do
	{  	
		cout<<"1 Push"<<endl;
		cout<<"2 Find"<<endl;
		cin>>x;
		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); 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

struct node
{
    int data ;
    node* next ;
};

node* first = nullptr ;
node* last = nullptr ;

void push( int number )
{
    node* n = new node { number, nullptr } ;

    if( first == nullptr ) first = last = n ;

    else
    {
        last->next = n ;
        last = n ;
    }
}

void find( int number )
{
    int cnt = 0 ;

    for( node* n = first ; n != nullptr ; n = n->next )
    {
        if( n->data == number )
        {
            std::cout << "found " << number << " at position " << cnt << '\n' ;
            return ;
        }

        else ++cnt ;
    }

    std::cout << number << " was not found\n" ;
}

int main()
{
    for( int v : { 23, 56, 78, 93, 41, 52, 12, 78, 65, 84 } ) push(v) ;
    
    find(23) ; // found 23 at position 0
    find(52) ; // found 52 at position 5
    find(84) ; // found 84 at position 9
    find(200) ; // 200 was not found
}

http://coliru.stacked-crooked.com/a/787b6813906198ca
Thank you so much!!!
It works. I was doing my fuction really wrong I realize that my while loop wasn't right.

Wow I'm very happy, finally my code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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)
{
     
    int cnt = 0 ;

    for( node* Q = first ; Q != NULL ; Q = Q->next )
    {
        if( Q->data == number )
        {
            std::cout << "found " << number << " at position " << cnt << '\n' ;
            return ;
        }

        else ++cnt ;
    }

    std::cout << number << " was not found\n" ;
}
 
int main()
{
	int number,x;
	node *q=NULL;
	do
	{  	
		cout<<"1 Push"<<endl;
		cout<<"2 Find"<<endl;
		cin>>x;
		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); 
}
Topic archived. No new replies allowed.