
1c:\documents๎and๎settings\usman.younis\my๎...\TemplateList\TemplateList\TemplateList.cpp
1๎#๎include๎๎<๎iostream๎>
2๎#๎include๎๎<๎string๎>
3
4๎using๎namespace๎std๎;
5
6๎class๎Number
7๎{
8๎public๎:
9๎๎ ๎int๎intData๎;
10 ๎๎ ๎Number๎():๎๎intData๎(๎0๎)๎๎{}
11 ๎๎ ๎friend๎ostream๎&๎๎operator๎<<๎(๎ostream๎&๎๎out๎,๎๎Number๎&๎๎a๎)๎;
12 ๎};
13
14 ๎ostream๎&๎๎operator๎<<๎(๎ostream๎&๎๎out๎,๎๎Number๎&๎๎a๎)
15 ๎{
16 ๎๎ ๎out๎<<๎a๎.๎intData๎;
17 ๎๎ ๎return๎out๎;
18 ๎}
19
20 ๎template๎๎<๎class๎Type๎1๎,๎๎class๎Type๎2๎>
21 ๎class๎List
22 ๎{
23 ๎private๎:
24 ๎๎ ๎Type๎1๎๎Data๎1๎;
25 ๎๎ ๎Type๎2๎๎Data๎2๎;
26 ๎๎ ๎List๎*๎๎next๎;
27 ๎public๎:
28 ๎๎ ๎List๎():๎๎next๎(๎NULL๎)๎๎{}๎
29 ๎๎
30 ๎๎ ๎void๎add๎
๎item๎(๎Type๎1๎๎a๎,๎๎Type๎2๎๎b๎)๎;
31 ๎๎ ๎void๎show๎
๎List๎()๎;๎
32 ๎};
33
34 ๎template๎๎<๎class๎Type๎1๎,๎๎class๎Type๎2๎>
35 ๎void๎List๎<๎Type๎1๎,๎๎Type๎2๎>๎::๎add๎
๎item๎(๎Type๎1๎๎a๎,๎๎Type๎2๎๎b๎)
36 ๎{
37 ๎๎ ๎List๎*๎๎newLink๎๎=๎๎new๎List๎<๎Type๎1๎,๎๎Type๎2๎>;
38 ๎๎ ๎newLink๎โ๎>๎Data๎1๎ ๎=๎๎a๎;
39 ๎๎ ๎newLink๎โ๎>๎Data๎2๎ ๎=๎๎b๎;
40 ๎๎ ๎newLink๎โ๎>๎next๎ ๎=๎๎next๎;
41 ๎๎
42 ๎๎ ๎next๎๎=๎๎newLink๎;
43 ๎}
44
45 ๎template๎๎<๎class๎Type๎1๎,๎๎class๎Type๎2๎>
46 ๎void๎List๎<๎Type๎1๎,๎๎Type๎2๎>๎::๎show๎
๎List๎()
47 ๎{
48 ๎๎ ๎List๎*๎๎Temp๎๎=๎๎next๎;
49
50 ๎๎ ๎while๎(๎Temp๎๎!=๎๎NULL๎)
51 ๎๎ ๎{
52 ๎๎๎๎cout๎<<๎Temp๎โ๎>๎Data๎1๎<<๎"๎:๎"๎;
53 ๎๎๎๎cout๎<<๎Temp๎โ๎>๎Data๎2๎<<๎endl๎;
54 ๎๎๎๎Temp๎๎=๎๎Temp๎โ๎>๎next๎;
55 ๎๎ ๎}
56 ๎}
57
58 ๎void๎main๎(๎void๎)
59 ๎{
60 ๎๎ ๎List๎<๎int๎,๎๎float๎>๎๎intfloatList๎;
61
62 ๎๎ ๎intfloatList๎.๎add๎
๎item๎(๎3๎,๎๎4๎.๎5๎F๎)๎;
63 ๎๎ ๎intfloatList๎.๎add๎
๎item๎(๎5๎,๎๎2๎.๎3๎F๎)๎;
64 ๎๎ ๎intfloatList๎.๎add๎
๎item๎(๎7๎,๎๎5๎.๎7๎F๎)๎;
65 ๎๎ ๎intfloatList๎.๎add๎
๎item๎(๎8๎,๎๎8๎.๎9๎F๎)๎;
66 ๎๎ ๎intfloatList๎.๎add๎
๎item๎(๎2๎,๎๎5๎.๎3๎F๎)๎;