c++实现创建模板(template)结点类 | StriveZs的博客

c++实现创建模板(template)结点类

Node.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef NODE_LIBRARY
#define NODE_LIBRARY
#include <iostream>
#include <cstdlib>

using namespace std;

template <class T>
class Node
{
private:
Node<T> *next;
public:
T data;
Node (const T& item, Node<T>* ptrnext = NULL);
void InsertAfter(Node<T> *p);
Node<T> *DeleteAfter(void);
Node<T> *NextNode(void) const;
};
template <class T>
Node<T>::Node(const T& item, Node<T>* ptrnext) :data(item), next(ptrnext){}

template <class T>
Node<T> *Node<T>::NextNode(void) const
{
return next;
}

template <class T>
void Node<T>::InsertAfter(Node<T> *p)
{
p->next = next;
next = p;
}

template <class T>
Node<T> *Node<T>::DeleteAfter(void)
{
Node<T> *tempPtr = next;
if (next == NULL)
return NULL;
next = tempPtr->next;
return tempPtr;
}

template <class T>
Node<T> \*GetNode(const T& item, Node<T> \*nextPtr = NULL)
{
Node<T> *newNode;
newNode = new Node<T>(item, nextPtr);
if (newNode == NULL)
{
cerr << "Memory allocation failure!" << endl;
exit(1);
}
return newNode;
}

enum AppendNewline {noNewline,addNewline};
template <class T>
void PrintList(Node<T> *head, AppendNewline addnl = noNewline)
{
Node<T> *currPtr = head;
while(currPtr != NULL)
{
if(addnl == addNewline)
cout << currPtr->data << endl;
else
cout << currPtr->data << " ";
currPtr = currPtr->NextNode();
}
}

template <class T>
int Find(Node<T> \*head, T& item, Node<T>\* &prevPtr)
{
Node<T> *currPtr = head;
prevPtr = NULL;
while(currPtr != NULL)
{
if (currPtr->data == item)
return 1;
prevPtr = currPtr;
currPtr = currPtr->NextNode();
}
return 0;
}

template <class T>
void InsertFront(Node<T>* & head, T item)
{
head = GetNode(item,head);
}

template <class T>
void InsertRear(Node<T>* & head, const T& item)
{
Node<T> \*newNode, \*currPtr = head;
if (currPtr == NULL)
InsertFront(head,item);
else
{
while(currPtr->NextNode() != NULL)
currPtr = currPtr->NextNode();
newNode = GetNode(item);
currPtr->InsertAfter(newNode);
}
}

template <class T>
void DeleteFront(Node<T>* & head)
{
Node<T> *p = head;
if (head != NULL)
{
head = head->NextNode();
delete p;
}
}

template <class T>
void Delete (Node<T>* & head, T key)
{
Node<T> \*currPtr = head, \*prevPtr = NULL;
if (currPtr == NULL)
return;
while (currPtr != NULL && currPtr->data != key)
{
prevPtr = currPtr;
currPtr = currPtr->NextNode();
}
if (currPtr != NULL)
{
if(prevPtr == NULL)
head = head->NextNode();
else
prevPtr->DeleteAfter();
delete currPtr;
}
}

template <class T>
void InsertOrder(Node<T>* & head, T item)
{
Node<T> \*currPtr, \*prevPtr, *newNode;
prevPtr = NULL;
currPtr = head;
while (currPtr != NULL)
{
if (item < currPtr->data)
break;
prevPtr = currPtr;
currPtr = currPtr->NextNode();
}
if (prevPtr == NULL)
InsertFront(head,item);
else
{
newNode = GetNode(item);
prevPtr->InsertAfter(newNode);
}
}

template <class T>
void ClearList(Node<T> * &head)
{
Node<T> \*currPtr, \*nextPtr;
currPtr = head;
while(currPtr != NULL)
{
nextPtr = currPtr->NextNode();
delete currPtr;
currPtr = nextPtr;
}
head = NULL;
}
#endif

使用: 代码:

#include <iostream>
#include "Node.h"

using namespace std;
int main()
{
Node<int> \*head = NULL, \*prevPtr, *delPtr;
int i, key, item;
cout<<"请输入十个数:";
for (i=0;i < 10;i++)
{
cin>>item;
InsertRear(head, item);
}
cout << "创建的结点表为: ";
PrintList(head,noNewline);
cout << endl;
cout << "请输入你想要删除的一个整数: ";
cin >> key;
prevPtr = head;
while (Find(head,key,prevPtr) != 0)
{
if(prevPtr == NULL)
head = head->NextNode();
else
delPtr=prevPtr->DeleteAfter();
delete delPtr;
}
cout << "删除后的结果为: ";
PrintList(head,noNewline);
cout << endl;
cout<<"查找一个结点是否存在于结点表中(请输入数值):";
int a,t=0;
cin>>a;
t=Find(head,a,prevPtr);
cout<<"结果为:"<<(t==1?"存在":"不存在")<<endl;
cout<<"在表头插入一个数据(输入数值):";
cin>>a;
InsertFront(head,a);
cout<<"插入之后的表为:";
PrintList(head,noNewline);
cout<<endl<<"开始清除结点表!"<<endl;
ClearList(head);
cout<<"成功清除结点表"<<endl;
}

结果:

StriveZs wechat
Hobby lead  creation, technology change world.
  • Post author: StriveZs
  • Post link: 1337.html
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.