PROGRAM FOR SEARCHING IN LINK LIST.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
class Chain;
template <class Type>
class Node
{
private:
Type Info;
Node<int> *Link;
friend class Chain;
};
class Chain
{
private:
Node<int> *First;
public:
Chain()
{
First = NULL;
}
void Head_Insert(int No)
{
Node<int> *Temp = new Node<int>;
Temp->Info = No;
Temp->Link = First;
First = Temp;
}
void Print()
{
if (ListMsg())
{
cout << "\nLIST OVERFLOWED...!!!\n";
exit(1);
}
for (Node<int> *Temp = First; Temp != NULL; Temp = Temp->Link)
cout << "Value of Node is: " << Temp->Info << endl;
}
Node<int>* Search(int Value)
{
bool Find = false;
Node<int> *Temp;
for (Temp = First; Temp != NULL && !Find; Temp = Temp->Link)
{
if (Temp->Info == Value)
Find = true;
}
return Temp;
}
inline bool ListMsg()
{
return First == NULL;
}
};
int main()
{
Chain Obj;
Obj.Head_Insert(1);
Obj.Head_Insert(2);
Obj.Head_Insert(3);
Obj.Head_Insert(4);
Obj.Head_Insert(5);
Node<int> *Obj1 = Obj.Search(2);
if (Obj1 == NULL)
cout << "\nValue Not Found In LIST....!!!!\n" << endl;
else
cout << "\nValue Found...!!! \n" << endl;
getch();
return 0;
}
0 comments:
Post a Comment