直接上代码了,说明看注释就可以:
利用静态成员构建链表
?
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
|
#include <IOSTREAM.H>
class Node
{
public :
Node( int val, Node* next):val(val),next(next){}
//~Node(){cout<<"del "<<val<<endl;}
static void showAll(); //打印全部节点的值
static void insertHead( int ); //头插
static void insertTail( int ); //尾插
static void delHead(); //删头
static void delTail(); //删尾
static void clear(); //清空
protected :
int val;
Node *next;
static Node *head;
private :
};
Node* Node::head = 0;
void Node::showAll(){ //打印全部节点的值
Node *p = head;
while (p)
{
cout<<p->val<< " " ;
p = p->next;
}
cout<<endl;
}
void Node::insertHead( int val){ //头插
Node *p = new Node(val, head);
head = p;
}
void Node::insertTail( int val){ //尾插
Node *p = new Node(val, 0);
if (!head)
{
head = p;
return ;
}
Node *q = head;
while (q->next)
{
q = q->next;
}
q->next = p;
}
void Node::delHead(){ //删头
Node *p = head;
if (head)
{
head = head->next;
delete p;
}
}
void Node::delTail(){ //删尾
if (!head)
{
return ;
}
if (!(head->next))
{
delete (head);
head = NULL;
return ;
}
Node *p = head;
while (p->next->next)
{
p = p->next;
}
delete (p->next);
p->next = NULL;
}
void Node::clear(){ //清空
Node *p = head;
Node *q = 0;
head = 0;
while (p)
{
q = p;
p = p->next;
delete q;
}
}
void main(){
Node::delHead();
Node::delTail();
Node::insertTail(2);
Node::delTail();
for ( int i = 0; i < 10; i++)
{
Node::insertTail(i + 1);
}
Node::delTail();
Node::showAll();
}
|
利用类模板构建链表
这有点类似于list<>:
?
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
|
#include <iostream>
#include <string>
using namespace std;
template < class T> class Node //创建一个类模板,一个可以放入任何类型节点的链表
{
public :
Node(T val, Node* next):val(val),next(next){}
static void showAll(); //打印全部节点的值
static void insertHead(T); //头插
static void insertTail(T); //尾插
static void delHead(); //删头
static void delTail(); //删尾
static void clear(); //清空
protected :
T val;
Node *next;
static Node *head;
private :
};
template < class T> Node<T>* Node<T>::head = 0;
template < class T> void Node<T>::showAll(){ //打印全部节点的值
Node *p = head;
while (p)
{
cout<<p->val<< " " ;
p = p->next;
}
cout<<endl;
}
template < class T> void Node<T>::insertHead(T val){ //头插
Node *p = new Node(val, head);
head = p;
}
template < class T> void Node<T>::insertTail(T val){ //尾插
Node *p = new Node(val, 0);
if (!head)
{
head = p;
return ;
}
Node *q = head;
while (q->next)
{
q = q->next;
}
q->next = p;
}
template < class T> void Node<T>::delHead(){ //删头
Node *p = head;
if (head)
{
head = head->next;
delete p;
}
}
template < class T> void Node<T>::delTail(){ //删尾
if (!head)
{
return ;
}
if (!(head->next))
{
delete (head);
head = NULL;
return ;
}
Node *p = head;
while (p->next->next)
{
p = p->next;
}
delete (p->next);
p->next = NULL;
}
template < class T> void Node<T>::clear(){ //清空
Node *p = head;
Node *q = 0;
head = 0;
while (p)
{
q = p;
p = p->next;
delete q;
}
}
class Student //创建一个自定义的学生类
{
public :
Student(string name, int age, char sex):name(name), age(age), sex(sex){}
void showInfo(){
cout<< "姓名:" <<name<< " 年龄:" <<age<< " 性别:" <<sex<<endl;
}
protected :
string name;
int age;
char sex;
private :
};
void Node<Student>::showAll(){ //学生类节点和其他基本数据类型不同,不能直接用<<输出,所以重载showAll()
Node *p = head;
while (p)
{
p->val.showInfo();
p = p->next;
}
}
void main(){
for ( int i = 1; i < 10; i++)
{
Node< int >::insertTail(i); //这时Node<int>称为一个用类模板生成的模板类
Node< float >::insertTail(i / 10.0f);
Node< double >::insertTail(i / 10.00);
Node<Student>::insertTail(Student( "stu" , i, 'F' ));
}
Node< int >::showAll();
Node< float >::showAll();
Node< double >::showAll();
Node<Student>::showAll();
}
|
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Spring Boot整合logback一个简单的日志集成架构
2025-05-29 61 -
2025-05-29 50
-
2025-05-25 100
-
thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例
2025-05-29 64 -
2025-05-29 59
热门评论