Last change
on this file since 80 was
80,
checked in by Sam Hocevar, 14 years ago
|
- Adding the Golgotha source code. Not sure what's going to be interesting
in there, but since it's all public domain, there's certainly stuff to
pick up.
|
-
Property svn:keywords set to
Id
|
File size:
1.7 KB
|
Line | |
---|
1 |
|
---|
2 | /**********************************************************************
|
---|
3 | *<
|
---|
4 | FILE: linklist.cpp
|
---|
5 |
|
---|
6 | DESCRIPTION: Linked-list template classes
|
---|
7 |
|
---|
8 | CREATED BY: Tom Hudson
|
---|
9 |
|
---|
10 | HISTORY: created 10 December 1995
|
---|
11 |
|
---|
12 | *> Copyright (c) 1995, All Rights Reserved.
|
---|
13 | **********************************************************************/
|
---|
14 |
|
---|
15 | #ifndef __LINKLIST_H__
|
---|
16 |
|
---|
17 | #define __LINKLIST_H__
|
---|
18 |
|
---|
19 | template <class T> class LinkedEntryT {
|
---|
20 | public:
|
---|
21 | T data;
|
---|
22 | void *next;
|
---|
23 | LinkedEntryT(T& d) { data = d; next = NULL; }
|
---|
24 | };
|
---|
25 |
|
---|
26 | template <class T,class TE> class LinkedListT {
|
---|
27 | private:
|
---|
28 | TE* head;
|
---|
29 | TE* tail;
|
---|
30 | int count;
|
---|
31 | public:
|
---|
32 | LinkedListT() { head = tail = NULL; count = 0; }
|
---|
33 | ~LinkedListT() { New(); }
|
---|
34 | void New() {
|
---|
35 | while(head) {
|
---|
36 | TE* next = (TE*)head->next;
|
---|
37 | delete head;
|
---|
38 | head = next;
|
---|
39 | }
|
---|
40 | head = tail = NULL;
|
---|
41 | count = 0;
|
---|
42 | }
|
---|
43 | int Count() { return count; }
|
---|
44 | void Append(T& item) {
|
---|
45 | TE *entry = new TE(item);
|
---|
46 | if(tail)
|
---|
47 | tail->next = entry;
|
---|
48 | tail = entry;
|
---|
49 | if(!head)
|
---|
50 | head = entry;
|
---|
51 | count++;
|
---|
52 | }
|
---|
53 | T &operator[](int index) {
|
---|
54 | TE *e = head;
|
---|
55 | while(index && e) {
|
---|
56 | e = (TE*)e->next;
|
---|
57 | index--;
|
---|
58 | }
|
---|
59 | if(!e) {
|
---|
60 | assert(0);
|
---|
61 | return T();
|
---|
62 | }
|
---|
63 | return e->data;
|
---|
64 | }
|
---|
65 | LinkedListT &operator=(LinkedListT &from) {
|
---|
66 | New();
|
---|
67 | for(int i = 0; i < from.Count(); ++i)
|
---|
68 | Append(from[i]);
|
---|
69 | return *this;
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | // Handy macro for defining linked-lists
|
---|
74 |
|
---|
75 | #define MakeLinkedList(TYPE) typedef LinkedEntryT<TYPE> TYPE##Entry; typedef LinkedListT<TYPE,TYPE##Entry> TYPE##List;
|
---|
76 |
|
---|
77 | #endif // __LINKLIST_H__
|
---|
Note: See
TracBrowser
for help on using the repository browser.