/
clinklist.h
123 lines (111 loc) · 3.38 KB
/
clinklist.h
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
115
116
117
118
119
120
121
122
123
//=============================================================================
//
// Author: safesky - safesky@163.com
//
// Copyright: safesky
//
// Last modified: 2015-05-12 13:33
//
// Filename: clinklist.h
//
// Description:
//
//=============================================================================
#ifndef _CLINKLIST_H
#define _CLINKLIST_H
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
template<class Ty>
struct LNode {
Ty data;
struct LNode *next;
};
template<class Ty>
class CLinkList {
public:
CLinkList() {
}
~CLinkList() {
LNode<Ty>* q;
while( list ) {
q = list;
list = list->next;
delete q;
}
list = NULL;
}
bool Insert(int i, Ty e) { // insert e at i
LNode<Ty>* q = list;
int j = 0;
while( q && j < i-1 ) {
q = q->next;
j++;
}
if( j>i-1 || !q ) return false;
LNode<Ty>* elem = new LNode<Ty>;
elem->next = q->next;
q->next = elem;
elem->data = e;
length++;
return true;
}
bool Delete(int i, Ty* e) { // get position i element and save into e and delete it
LNode<Ty>* q = list;
int j = 0;
while( q->next && j<i-1 ) {
q = q->next;
j++;
}
if( j>i-1 || !q->next ) return false;
LNode<Ty>* elem = q->next;
q->next = elem->next;
*e = elem->data;
delete elem;
length--;
return true;
}
int Locate(Ty e) { // locate element e position of list
LNode<Ty>* elem = list->next;
int j = 0;
while( elem && elem->data!=e ) {
elem = elem->next;
j++;
}
if( NULL==elem ) return 0; // can find element e in the list
else return j;
}
void Show() {
int iPutCnt = 0;
int iPutTimes = 10;
LNode<Ty>* q = list->next;
while( q ) {
cout<<q->data<<"\t";
iPutCnt++;
if( iPutTimes == iPutCnt ) cout<<endl;
q = q->next;
}
cout<<endl;
}
int GetLength() { // get list curren length
return length;
}
void Init(Ty* v, int n) {
p = v;
length = n;
list = new LNode<Ty>; // reverse create a link list which has a head node
list->next = NULL;
for(int i=length-1; i>=0; i--) {
LNode<Ty>* q = new LNode<Ty>; // create new node
q->data = *(p+i); // insert new node value
q->next = list->next; // insert node into list head
list->next = q;
}
}
private:
Ty* p;
int length;
LNode<Ty>* list;
};
#endif /* _CLINKLIST_H */