/*****************************************
** This example shows "search" for linked lists.
*****************************************/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
struct Node
{
double data;
Node *next;
};
void add2front(double val, Node* &L);
void printlist(Node* L);
Node* search(double x, Node* L);
/*****************************************
** MAIN
*****************************************/
int main()
{
// Get original list of values from user
// comma-separated, semi-colon terminated.
Node *L = NULL;
double x;
char c = ',';
while(c == ',' && cin >> x >> c)
{
add2front(x,L);
}
string comm;
while(cin >> comm && comm == "search" && cin >> x)
{
Node* p = search(x,L);
if (p != NULL)
cout << "Found!" << endl;
else
cout << "Not Found!" << endl;
}
return 0;
}
/*****************************************
** Function definitions
*****************************************/
void add2front(double val, Node* &L)
{
Node *T = new Node;
T->data = val;
T->next = L;
L = T;
}
void printlist(Node* L)
{
if (L != 0)
{
cout << L->data << ' ';
printlist(L->next);
}
}
bool match(double a, double b)
{
return fabs(a - b) < 0.0001;
}
Node* search(double x, Node* L)
{
Node* p = L;
while(p != NULL && !match(x,p->data))
p = p->next;
return p;
}