-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic_linkedlist.cpp
55 lines (54 loc) · 994 Bytes
/
Dynamic_linkedlist.cpp
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
#include <iostream>
using namespace std;
template <class type>
struct node
{
type data;
node* next;
};
template <class type>
class linkedlist
{
private:
node <type> * first;
public:
linkedlist()
{
first=NULL;
}
void additem(type var);
void show();
};
template <class type>
void linkedlist<type>::additem(type var)
{
node <type>* a= new node <type>;
a->data=var;
a->next=first;
first=a;
}
template <class type>
void linkedlist <type>:: show()
{
node <type>* current = first;
while(current!=NULL)
{
cout<<current->data<<endl;
current=current->next;
}
}
int main()
{
linkedlist<double> ld; //ld is object of class linklist<double>
ld.additem(151.5); //add three doubles to list ld
ld.additem(262.6);
ld.additem(373.7);
ld.show(); //show entire list ld
linkedlist<char> lch; //lch is object of class linklist<char>
lch.additem('a'); //add three chars to list lch
lch.additem('b');
lch.additem('c');
lch.show(); //show entire list lch
cout << endl;
return 0;
}