-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.h
More file actions
114 lines (84 loc) · 1.91 KB
/
list.h
File metadata and controls
114 lines (84 loc) · 1.91 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*****************************************************************
* l i s t . h *
* linked list functions *
* *
* started 6/15/98, Karl Kosack (kosack@andrew.cmu.edu) *
*****************************************************************/
#ifndef __LIST_H__
#define __LIST_H__
#include <iostream.h>
template<class Type>
class ListItem{
public:
Type *node;
ListItem *next;
};
template<class Type>
class List {
public:
List(void);
~List(void);
void addItem(Type *);
bool isEmpty(void);
void beginIteration(void);
Type* getItem(void);
int getLength(void);
ListItem<Type> *current;
ListItem<Type> *first;
};
template<class Type>
List<Type>::List(void) {
first = NULL;
current = first;
}
template<class Type>
List<Type>::~List(void){
ListItem<Type> *temp;
while (first != NULL && first->next != NULL){
temp = first->next;
delete first;
first = temp;
}
delete first;
first = NULL;
}
template<class Type>
void
List<Type>::addItem(Type *item){
ListItem<Type> *newitem;
newitem = new ListItem<Type>;
newitem->node = item;
newitem->next = first;
first = newitem;
}
template<class Type>
bool
List<Type>::isEmpty(void){
if (first==NULL) return true;
else return false;
}
template<class Type>
void
List<Type>::beginIteration(void){
current = first;
}
template<class Type>
Type *
List<Type>::getItem(void){
Type *temp;
if(current == NULL) return NULL;
temp = current->node;
current = current->next;
return temp;
}
template<class Type>
int
List<Type>::getLength(void){
ListItem<Type> *temp;
int i=0;
for(temp=first; temp != NULL; temp=temp->next){
i++;
}
return i;
}
#endif //__LIST_H__