Sunday, 18 November 2012

Insertion in Singly Link List


#include<iostream>
using namespace std;
template<class T>
class Chain;
template<class T>
class Node
{
private:
T info;
Node *link;
friend class Chain<T>;

};

template<class T>
class Chain
{
private:
Node<T> *first;
public:
Chain()
{
first = NULL;
}
void head_insert(T val)
{
Node<T> *temp;
temp = new Node<T>;
temp -> info = val ;
temp -> link = first;
first = temp;
}

void display()
{
Node<T> *temp;
temp = first;
while(temp != NULL )
{
cout<<temp -> info<<endl;
temp = temp -> link;
}
}
};

void main()
{
Chain<int> ch;
ch.head_insert(5);
ch.head_insert(10);
ch.head_insert(12);
cout<<"the info part of the nodes are : "<<endl;
ch.display();
}

No comments:

Post a Comment