/*****************************************
* This example shows "search" for linked lists.
*****************************************/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
struct Node
{
double data;
Node *next;
};
Node* add2front(double val, Node* L);
void printlist(Node* L);
Node* search(double x, Node* L);
Node* deletefront(Node* L);
void deletelist(Node* L);
/*****************************************
** MAIN
*****************************************/
int main()
{
Node* L = NULL;
// read and store numbers
double x;
char c;
while( cin >> x >> c) // comma-separated
{
L = add2front(x,L);
// ';' means the end of the numbers
if( c == ';' )
break;
}
// process commands
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;
}
// delete the list
deletelist(L);
return 0;
}
/*****************************************
** Function definitions
*****************************************/
Node* add2front(double val, Node* L)
{
Node* T = new Node{val, L};
return T;
}
void printlist(Node* L)
{
for(Node* T = L; T != NULL; T = T->next)
cout << T->data << ' ';
}
Node* search(double x, Node* L)
{
for(Node* p = L; p != NULL; p = p->next)
{
if( x - p->data > -1 && x - p->data < 1 )
return p;
}
return NULL;
}
Node* deletefront(Node* L)
{
if (L == NULL)
return NULL;
Node* second = L->next; // store the 2nd node to return
delete L; // delete the front node
return second;
}
void deletelist(Node* L)
{
while (L != NULL)
L = deletefront(L);
}