1 //list参考
2
3 #ifndef MYLIST_INC
4 #define MYLIST_INC
5
6 #include <qmutex.h>
7
8 template <typename T>
9 class MyList
10 {
11 public:
12 MyList(void)
13 {
14 mHead= 0;
15 mCur= 0;
16 mTotal = 0;
17 }
18
19 private:
20 struct Node
21 {
22 T cp;
23 Node *next;
24 };
25
26 public:
27
28 /*
29 *功能: 添加到队列
30 *参数: null
31 *返回: void
32 */
33 void push_back(T cp)
34 {
35 if(cp == 0)
36 {return;}
37
38 mMutex.lock();
39 Node *node = new Node();
40 node->cp = cp;
41 node->next = 0;
42
43 if(mHead == 0)
44 {
45 mHead = node;
46 mCur = node;
47 }
48 else
49 {
50 mCur->next = node;
51 mCur = node;
52 }
53
54 ++mTotal;
55 mMutex.unlock();
56
57 }
58
59
60 /*
61 *功能: 获取队列头
62 *参数: null
63 *返回: void
64 */
65 T front()
66 {
67 if(mTotal == 0)
68 {
69 return 0;
70 }
71 return mHead->cp;
72 }
73
74
75 /*
76 *功能: 删除队列头并释放该内存
77 *参数: null
78 *返回: void
79 */
80 void pop_front()
81 {
82 mMutex.lock();
83
84 if(mTotal != 0)
85 {
86 T tmp = mHead->cp;
87 mHead = mHead->next;
88 --mTotal;
89
90 delete[] tmp;
91 }
92 mMutex.unlock();
93 }
94
95 //返回队列大小
96 int size()
97 {
98 return mTotal;
99 }
100
101 private:
102 Node *mHead;
103 Node *mCur;
104
105 QMutex mMutex;
106 int mTotal;
107 };
108
109 #endif /* ----- #ifndef MYLIST_INC ----- */