# 【数据结构】单链表基本操作(C++实现)

//LinkList.h
#pragma once
#include<iostream>
using namespace std;
template<class T>
struct Node   //结点结构
{
T data;   //结点数据域
Node() { link = NULL; }
Node(T e, Node *next = NULL)
{
data = e;
}
};
template<class T>
{
private:
public:
LinkList(T data[], int  mSize);//构造有mSize个元素的单链表
~LinkList() { Clear(); }            //析构函数
bool Insert(int pos, const T&x);   //在单链表第pos个元素前插入元素x
bool Remove(int pos, T&x);    //删除单链表第pos个元素
bool Replace(int pos, const T&x);  //将修改单链表第pos个元素为x
int Length()const;      //求表长等不改变数据值，只是读取建议使用常函数const末尾标识
bool IsEmpty() const;  //判空操作
void Clear();        //清空操作
void Output() const;    //输出链表
bool search(const T&x) const;//查找元素x在表中是否存在
};


#### 3单链表.cpp

//单链表.cpp
#include<iostream>
using namespace std;
template<class T>
head = new Node<T>;
}
template<class T>
head = new Node<T>;
Node < T> *end = head;
for (int  i = 0; i < mSize; i++)
{
Node<T> *p = new Node<T>;
p->data = data[i];
end = p;
}
}
template<class T>
bool LinkList<T>::Insert(int pos,  T&x) {//插入元素
int cont = 1;
bool flag = false;//初始flag为false
while (p != NULL && cont <= pos - 1)
{
if (cont == pos - 1)//如果p在目标节点处
{
flag = true;
break;
}
else {//如果没到目标节点，指针一直移动并且计数器++
cont++;
}
}
return flag;
}
template<class T>
bool LinkList<T>::Remove(int pos,  T&x)//删除元素
{
int cont = 1;
Node<T> *p = head;//p指向头节点
bool flag = false;
while (p!=NULL&&cont<=pos-1)
{
if (cont == pos - 1)
{
Node<T> *del = p->link;
x = del->data;
delete del;
flag = true;
break;
}
else
{
cont++;
}
}
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++;

}
}
return flag;
}
template<class T>
{
int cont = 0;
while (p != NULL)
{
cont++;
}
return cont;
}
template<class T>
if (head != NULL) {
Node<T> *p;
while(p != NULL) {
Node<T> *del = p;
delete del;
}
}
}

template<class T>
{
}

template<class T>
{
bool flag = false;
while (p!=NULL)
{
if (p->data == x)
{
flag = true;
break;

}
else
{
}
}
return flag;
}

template<class T>
{
Node<T> *p = head;
int cont = 0;
while (p != NULL)\
{
cout << p->data << ",";
cont++;
if (cont % 10 == 0)
{
cout << endl;
}
}
cout << endl;
}


#### 3.测试.cpp

//测试.cpp
#include"单链表.cpp"
#include<ctime>
{
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;//用随机数发生器产生100以内的整数
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))//删除单链表的第pos个元素
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.运行截图

02-09 1620

09-14 2万+
10-22 705
02-13 2万+
03-14 5025
07-23 1万+
06-21 2141
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客