65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class linked_list_element
{
public:
int data;
private:
linked_list_element *next_ptr;
friend class linked_list;
};
class linked_list
{
public:
linked_list_element *first_ptr;
linked_list(){first_ptr = 0;}
inline void add_list (int item);
bool find (const int& a);
};
void linked_list::add_list (int item)
{
linked_list_element *new_ptr;
new_ptr = new linked_list_element;
new_ptr -> data = item;
new_ptr -> next_ptr = first_ptr;
first_ptr = new_ptr;
}
bool linked_list::find (const int& a)
{
linked_list_element *current_ptr;
current_ptr = first_ptr;
while ((current_ptr -> data != a) && (current_ptr != 0))
current_ptr = current_ptr->next_ptr;
return (current_ptr != 0);
}
int main()
{
linked_list lis;
lis.add_list(0);
lis.add_list(1);
lis.add_list(2);
lis.add_list(3);
lis.add_list(4);
bool b;
int x = 6; //把"6”改成0到4之间的数就能正常输出。
b = lis.find (x) ;
if(b)
cout<<"1"<<endl;
else
cout<<"2"<<endl;
return 0;
}
bool linked_list::find (const int& a)
{
linked_list_element *current_ptr;
current_ptr = first_ptr;
while ((current_ptr != 0) && (current_ptr -> data != a))//<-----------这一行有错
current_ptr = current_ptr->next_ptr;
return (current_ptr != 0);
}