代码主体使用结构体+类+模板进行实现。
1.LinkList.h
#pragma once
#include<iostream>
using namespace std
;
template<class T>
struct Node
{
T data
;
Node
*link
;
Node() { link
= NULL; }
Node(T e
, Node
*next
= NULL)
{
data
= e
;
link
= next
;
}
};
template<class T>
class LinkList
{
private:
Node
<T
> *head
;
public:
LinkList();
LinkList(T data
[], int mSize
);
~LinkList() { Clear(); }
bool Insert(int pos
, const T
&x
);
bool Remove(int pos
, T
&x
);
bool Replace(int pos
, const T
&x
);
int Length()const;
bool IsEmpty() const;
void Clear();
void Output() const;
bool search(const T
&x
) const;
};
3单链表.cpp
#include"LinkList.h"
#include<iostream>
using namespace std
;
template<class T>
LinkList
<T
>::LinkList() {
head
= new Node
<T
>;
head
->link
= NULL;
}
template<class T>
LinkList
<T
>::LinkList(T data
[], int mSize
) {
head
= new Node
<T
>;
head
-> link
= NULL;
head
->data
= data
[0];
Node
< T
> *end
= head
;
for (int i
= 0; i
< mSize
; i
++)
{
Node
<T
> *p
= new Node
<T
>;
p
->data
= data
[i
];
p
->link
= NULL;
end
->link
= p
;
end
= p
;
}
}
template<class T>
bool LinkList
<T
>::Insert(int pos
, T
&x
) {
int cont
= 1;
Node
<T
>*p
= head
;
bool flag
= false;
while (p
!= NULL && cont
<= pos
- 1)
{
if (cont
== pos
- 1)
{
Node
<T
> *add
= new Node
<T
>;
add
->data
= x
;
add
->link
= p
->link
;
p
->link
= add
;
flag
= true;
break;
}
else {
cont
++;
p
= p
->link
;
}
}
return flag
;
}
template<class T>
bool LinkList
<T
>::Remove(int pos
, T
&x
)
{
int cont
= 1;
Node
<T
> *p
= head
;
bool flag
= false;
while (p
!=NULL&&cont
<=pos
-1)
{
if (cont
== pos
- 1)
{
Node
<T
> *del
= p
->link
;
p
->link
=p
->link
->link
;
x
= del
->data
;
delete del
;
flag
= true;
break;
}
else
{
cont
++;
p
= p
->link
;
}
}
return flag
;
}
template<class T>
bool LinkList
<T
>::Replace(int pos
, T
&x
)
{
int cont
= 1;
Node
<T
>* p
= head
;
bool flag
= false;
while (p
!= NULL && cont
<= pos
)
{
if (cont
== pos
)
{
p
->data
= x
;
flag
= true;
}
else
{
cont
++;
p
= p
->link
;
}
}
return flag
;
}
template<class T>
int LinkList
<T
>::Length()const
{
int cont
= 0;
Node
<T
>*p
= head
;
while (p
!= NULL)
{
cont
++;
p
= p
->link
;
}
return cont
;
}
template<class T>
void LinkList
<T
>::Clear() {
if (head
!= NULL) {
Node
<T
> *p
;
p
= head
->link
;
while(p
!= NULL) {
Node
<T
> *del
= p
;
p
= p
->link
;
delete del
;
}
head
= NULL;
}
}
template<class T>
bool LinkList
<T
>::IsEmpty()const
{
return head
== NULL;
}
template<class T>
bool LinkList
<T
>::Search(const T
&x
)const
{
bool flag
= false;
Node
<T
>*p
= head
;
while (p
!=NULL)
{
if (p
->data
== x
)
{
flag
= true;
break;
}
else
{
p
= p
->link
;
}
}
return flag
;
}
template<class T>
void LinkList
<T
>::OutPut()const
{
Node
<T
> *p
= head
;
int cont
= 0;
while (p
!= NULL)\
{
cout
<< p
->data
<< ",";
p
= p
->link
;
cont
++;
if (cont
% 10 == 0)
{
cout
<< endl
;
}
}
cout
<< endl
;
}
3.测试.cpp
#include"单链表.cpp"
#include<ctime>
void menu()
{
cout
<< "********************************************************" << endl
;
cout
<< "* 1 ------------输出链表 *" << endl
;
cout
<< "* 2 ------------插入元素 *" << endl
;
cout
<< "* 3 ------------删除元素 *" << endl
;
cout
<< "* 4 ------------销毁链表 *" << endl
;
cout
<< "* 5 ------------查找 *" << endl
;
cout
<< "* 0 ------------退出系统 *" << endl
;
cout
<< "********************************************************" << endl
;
}
const int N
= 20;
int main()
{
srand((unsigned)time(NULL));
int data
[N
];
for (int i
= 0; i
< N
; i
++)
data
[i
] = rand() % 100;
LinkList
<int> L(data
, N
);
menu();
while (1)
{
int select
;
cout
<< "请输入您的选择:";
cin
>> select
;
switch (select
)
{
case 1:
if (L
.IsEmpty())
{
cout
<< "链表为空!" << endl
;
}
else
L
.OutPut();
break;
case 2:
int pos
, elem
;
cout
<< "请输入插入位置:";
cin
>> pos
;
cout
<< "插入元素:";
cin
>> elem
;
if (L
.Insert(pos
, elem
))
cout
<< "在第" << pos
<< "个元素前成功插入" << elem
<< endl
;
else
cout
<< "插入位置不合法!" << endl
;
break;
case 3:
cout
<< "请输入删除位置:"; cin
>> pos
;
int x
;
if (L
.Remove(pos
, x
))
cout
<< "单链表的第" << pos
<< "个元素" << x
<< "已被删除。" << endl
;
else
cout
<< "删除位置不合法!" << endl
;
break;
case 4:
char OK
;
cout
<< "确定要销毁链表吗?(y/n)" << endl
;
cin
>> OK
;
if (OK
== 'y' || OK
== 'Y') L
.Clear();
break;
case 5:
cout
<< "请输入查找元素:";
cin
>> elem
;
if (L
.Search(elem
))
cout
<< "查找成功!" << endl
;
else
cout
<< "查找失败!" << endl
;
break;
case 0:
exit(0);
default:
cout
<< "您输入的选项有误,请重新输入:";
}
}
return 0;
}
4.运行截图
转载请注明原文地址:https://ipadbbs.8miu.com/read-61980.html