#include<iostream>
#include<process.h>
using namespace std;
class Chain;
class Node
{
private:
int info;
Node *link;
friend class Chain;
};
class Chain
{
private:
Node * first;
public:
Chain()
{
first = NULL;
}
void head_insert(int val)
{
Node *temp;
temp = new Node;
temp -> info = val ;
temp -> link = first;
first = temp;
}
void display()
{
Node *temp;
temp = first;
while(temp != NULL )
{
cout<<temp -> info<<endl;
temp = temp -> link;
}
}
Node *search(int key)
{
bool found = false;
if(first == NULL)
{
cout<<"there is nothing to search, searching is impossible. . "<<endl;
exit(1);;
}
Node * temp;
temp = new Node;
temp = first ;
while( temp != NULL)
{
if ( temp ->info == key )
return temp;
else
temp = temp ->link;
}
if(found == true)
cout<<"Key is found. . ."<<endl;
else
cout<<"Key is not Found. . ."<<endl;
}
};
void main()
{
Chain ch;
ch.head_insert(45);
ch.head_insert(15);
ch.head_insert(75);
ch.head_insert(5);
ch.display();
//ch.search(5);
//ch.display();
Node * t = search(t);
//ch.display();
}
No comments:
Post a Comment