TEMPLATE linked list , HELP !!!!!!!!!!!
Oct 21, 2011 at 2:37pm UTC
I'm new to generic programming and I just cant figure out what is wrong in this . The program is supposed to make a linked list using class.
I have used 2 classes so that a single object of class LL can be used to manipulate a linked list . I want the program to work for both integers and characters . When I try without the generic part , the code is working perfectly !
Somebody please help me ! My exam is tomorrow!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class T>
class node
{
T data;
node * link;
friend class ll;
public :
};
template <class T>
class ll
{
node * <T>start;
public :
template <class T>
ll()
{
<T>start=NULL;
}
//void search();
void add();
void disp();
//void remove();
};
template <class T>
void ll::add()
{
T x;
node * <T>temp=new node;
cout<<"\nData : " ;
cin>>x;
temp->data=x;
temp->link=start;
start =temp;
}
template <class T>
void ll::disp()
{
node *<T>ptr;
T x;
ptr =start;
while (ptr!=NULL)
{
x=ptr->data;
cout<<x<<"\n" ;
ptr=ptr->link;
}
}
int main()
{
ll <int >list1;
int ch;
while (1)
{
cout<<"\n1.Add\n2.Display\n3.Exit\nEnter choice : " ;
cin>>ch;
switch (ch)
{
case 1 : list1.add(); break ;
case 2 : list1.disp(); break ;
case 3 : exit(0); break ;
default : cout<<"Invalid Entry " ; break ;
}
}
return 0;
}
Oct 21, 2011 at 5:00pm UTC
I've corrected only compile errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class T>
class ll; // Empty declaration of the ll template class to use it in node
template <class T>
class node
{
T data;
node * link;
friend class ll<T>;
public :
};
template <class T>
class ll
{
node<T> *start;
public :
ll()
{
start=NULL;
}
//void search();
void add();
void disp();
//void remove();
};
template <class T>
void ll<T>::add()
{
T x;
node<T> *temp=new node<T>;
cout<<"\nData : " ;
cin>>x;
temp->data=x;
temp->link=start;
start =temp;
}
template <class T>
void ll<T>::disp()
{
node<T> *ptr;
T x;
ptr =start;
while (ptr!=NULL)
{
x=ptr->data;
cout<<x<<"\n" ;
ptr=ptr->link;
}
}
int main()
{
ll <int >list1;
int ch;
while (1)
{
cout<<"\n1.Add\n2.Display\n3.Exit\nEnter choice : " ;
cin>>ch;
switch (ch)
{
case 1 : list1.add(); break ;
case 2 : list1.disp(); break ;
case 3 : exit(0); break ;
default : cout<<"Invalid Entry " ; break ;
}
}
return 0;
}
Topic archived. No new replies allowed.