2

I'm trying to make a non stl list and be able to find the most common number in it. Decided to first write the functions needed to be able to input elements and also to be able to print the list so that I can check if everything is going ok. So far so good. Then when I tried to write a function to find the most common element the issues started. I've thought to search for the elements one by one and have a variable that would increase by 1 every time I encounter the number. I've managed so far to only have a function that searches for only one element that I have chosen. However it keeps giving me an error. Not sure how to proceed. I wanted to be able to modify the function to search for the most common element but first wanted to make sure it works as it is. Do you have any idea where my mistake is?

#include <iostream>
using namespace std;

void add_е(int n);
void list();

 struct elem 
 {
    int key; 

    elem *next; 
 } *start=NULL; 


int main()
    {  
        int ch, num, amount, i, element;
    do
    {   cout<<"\n\t Menu";
        cout<<"\n 1.Add elements";
        cout<<"\n 2.Find the most common element";
        cout<<"\n 3.Print the stack";
        cout<<"\n 4.Exit";
    do
    {   cout<<"\n Your choice is:";
        cin>>ch;
    }
    while (ch<1||ch>4);
    switch(ch)
{

    case 1: 
        cout<<"\n How many elements do you want to add?";
        cin>>amount;
        for(i=0; i<amount; i++)
        {
            cout<<"Input stack elements:\n";
            cin>>num;
            add_е(num);
        }
    break;


    case 2:
        cout<<"\n Which element do you want to find? \n ";
        cin>>element;
        search_elem(element);   // here it gives me an error
        break;


    case 3: list();
    break;
}

}while(ch!=4); 
}

void add_е(int n) {  elem *p=start, *q=p;
    q=new elem;      
    q->key=n;    
    q->next=NULL;   
if (start)      
    {  
        while (p->next)
        p=p->next;  
        p->next=q;  
    }   
else    
    start=q;    
}       



void list()  
{   
    if (start) 
    { 
        elem *p=start;
        cout<<"\n List is: \n";
        while (p) 

        { 
            cout<<p->key<<"\n"; 
            p=p->next;   
        }   
    }  
    else 
        cout<<"\n Empty list";
} 
//izvejdane na spisuka 

int search_elem(int n) 
{  
    elem *p=start; 
    if (start) 
    {
        while ((p->key!=n)&&(p->next))   
            p=p->next;  

        if (p->key==n)
    {
        cout<<"p->key"<<"\n";
        return 1; 
    }
    else 
        cout<<"No element found";
        return 0; 
    } 
    return 0; 
} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

You need to give (at least) a function prototype for the search_elem function before you use it (as you have done with the add_е and list functions).

So, add the prototype in the same place as for those other functions:

void add_е(int n);
void list();
int search_elem(int n); // You need this BEFORE any call to the function!

Also, if your compiler conforms to the C++11 standard (or later), you should be using nullptr rather than the NULL macro. And please read this: Why is "using namespace std;" considered bad practice?.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

The e in add_е looks like a Unicode character. Make it an ASCII e.

And, forward declare search_elem() before the place where you're calling it.

Here's the updated code: https://godbolt.org/z/h7P8R3

Another thing is that you're leaking memory. You're allocating memory with new but not releasing it using delete. You need to take care of that too.

Azeem
  • 11,148
  • 4
  • 27
  • 40