#include <iostream.h>


struct node 
{
	int key;
	node *next;
	node(){}
	node(int key){this->key=key;}
};

class List
{
	node *head;

public:

	List(){ head=0;}

	~List()
	{
		cout << "Dekonstruktor" << endl;
		node *q; 
		while( head!=0)
		{
			q=head;
			head=head->next;
			delete q;
		}
		delete head;
	}

	bool isempty()
	{
		return (head==0);
	}

	void insertFirst(int x)
	{
		node *tmp = new node(x);
		//tmp->key=x;
		tmp->next = head;
		head = tmp;
	}

	void insertLast(int x)
	{
		node *tmp = new node(x);
		//tmp->key = x;
		tmp->next=0;

		if(isempty()) 
		{
			head = tmp;
			return;
		}

		node *p = head;
		node *q;
		while( p!=0 )
		{
			q=p;
			p=p->next;
		}
		q->next=tmp;	
	}

	bool find(int x)
	{
		if(isempty()) return false;
		else
		{
			node *tmp = head;
			while(tmp!=0)
			{
				if(tmp->key == x) return true;
				tmp=tmp->next;
			}
			return false;
		}
	}

	void remove(int x)
	{
		if(head->key==x) head=head->next;
		else
		{
			node *tmp=head;
			node *tmp2=head->next;
			while(tmp2!=0)
			{
				if(tmp2->key==x)
				{
					tmp->next=tmp2->next;
				}
				tmp=tmp->next;
				tmp2=tmp2->next;
			}
		}
	}
	
	void printList()
	{
		node *tmp = head;
		while(tmp!=0)
		{
			cout << tmp->key << endl;
			tmp = tmp->next;
		}
	}

};


int main()
{
	List myList;
	for(int i=0; i<10; i++){
		myList.insertFirst(i);
	}
	myList.insertLast(10000);
	myList.insertLast(77);
	myList.printList();
	cout << "find 77: ->" << myList.find(77) << endl;
	cout << "**********" << endl;
	
	myList.remove(9);
	myList.remove(77);
	myList.printList();
	return 0;
}