线性表:线性表分为顺序存储和链式存储
链表:
优点: 便于插入删除操作
缺点: 只能诸葛访问元素,每个元素都要分配next域所占字节导致储存密度<1
代码:
/**************************************
Copyright 陈四海
Author: created by 陈四海
Date: 2020-12-18
Description: 单链表
Version: 1.0
**************************************/
#include <iostream>
using namespace std;
//单链表节点
class Node
{
public:
string name;
Node* next;
};
//单链表操作
class List
{
public:
List();
~List();
bool setList(const int& n); //单链表内容创建
bool getList(); //遍历单链表
bool findElem(const int& n); //查找位置元素
bool dindValue(const string& id); //查找值
bool insert(const int& n); //位置插入
bool deleted(const int& n); //位置删除
private:
Node* head;
int size;
};
//构造函数
List::List()
{
this->head = new Node;
this->head->next = NULL;
this->size = 0;
}
//析构函数
List::~List()
{
delete head;
}
/**************************************
Fuction: setList
Description: 初始化单链表
Input: const int &n
Output:
Return: bool
Others:
**************************************/
bool List::setList(const int &n)
{
if (n < 0) {
printf("无法创建小于0的单链表!\n");
return false;
}
else {
Node* ptemp = NULL;
Node* pnew = NULL;
this->size = n;
ptemp = this->head;
for (int i = 0; i < n; i++)
{
pnew = new Node;
pnew->next = NULL;
cout << "输入第" << i + 1 << "个学生的姓名:" ;
cin >> pnew->name;
ptemp->next = pnew;
ptemp = pnew;
}
cout << "创建完成\n" << endl;
return true;
}
}
/**************************************
Fuction: getList
Description: 遍历单链表
Input:
Output:
Return: bool
Others:
**************************************/
bool List::getList()
{
Node* temp = this->head->next;
if (this->head == NULL) {
cout << "链表为空!" << endl;
return false;
}
else {
cout <<
"-------------------------\n"
"学号\t\t姓名" << endl;
for (int i = 0; i < this->size; i++)
{
cout << i + 1 << "\t\t" << temp->name << "\t"<<endl;
temp = temp->next;
}
cout <<"-------------------------\n"<< "总共" << this->size << "名学生!\n" << endl;
return true;
}
}
/**************************************
Fuction: findElem
Description: 查找位置元素
Input: const int& n
Output:
Return: bool
Others:
**************************************/
bool List::findElem(const int& n)
{
if (n >= this->size || n < 0)
{
cout<<"该元素不在链表中!\n"<<endl;
return false;
}
else {
Node* ptemp = NULL;
ptemp = this->head;
for (int k = 0; k < n; k++)
{
ptemp = ptemp->next;
}
cout << "第" << n << "名学生,姓名是" << ptemp->name << endl;
return true;
}
}
/**************************************
Fuction: dindValue
Description: 查找位置元素
Input: const string& name
Output:
Return: bool
Others:
**************************************/
bool List::dindValue(const string& name) //查找值
{
Node* ptemp = NULL;
ptemp = this->head;
for (int k = 0; ptemp; k++)
{
if (ptemp->name == name)
{
cout << name << "的学号是" << k << endl;
return true;
}
ptemp = ptemp->next;
}
cout << "该同学不在链表中!" << endl;
return false;
}
/**************************************
Fuction: insert
Description: 位置插入
Input: const int& n
Output:
Return: bool
Others:
**************************************/
bool List::insert(const int& n)
{
if (n >= this->size && n < 0)
{
printf("该位置不在链表范围内\n");
return false;
}
else {
Node* ptemp = NULL;
Node* pnew = NULL;
ptemp = this->head;
for (int k = 0; k < n; k++)
{
ptemp = ptemp->next;
}
pnew = new Node;
pnew->next = ptemp->next;
cout << "请输入新登记学生姓名:";
cin >> pnew->name;
ptemp->next = pnew;
ptemp = pnew;
this->size++;
cout << "添加完成!" << endl;
return true;
}
}
/**************************************
Fuction: deleted
Description: 位置删除
Input: const int& n
Output:
Return: bool
Others:
**************************************/
bool List::deleted(const int& n) //位置删除
{
if (n > this->size && n < 0)
{
printf("该位置不在链表范围内\n");
return false;
}
else {
Node* ptemp;
Node* p;
this->size--;
ptemp = this->head;
for (int k = 0; ptemp->next && k < n - 1; k++)
{
ptemp = ptemp->next;
}
p = ptemp->next;
p->next = ptemp->next;
delete p;
return true;
}
}
int main()
{
List list;
List* plist = &list;
int i;
cout << "请输入登记学生数:";
cin >> i;
cout << "总共登记" << i << "名学生\n" << endl;
plist->setList(i);
plist->getList();
while (true)
{
int key;
cout <<
"学生登记操作:\n"
"1-遍历输出登记学生\n"
"2-按位置查找学生\n"
"3-按姓名查找\n"
"4-位置插入学生\n"
"5-删除位置学生\n"
"0-退出系统\n"
"请选择操作:" << endl;
cin >> key;
if (key <= 5 || key > 0)
{
switch (key)
{
case 0:return 0;
case 1:plist->getList(); break;
case 2:
{
int n;
cout << "请输入学生学号:";cin >> n;
plist->findElem(n);
break;
}
case 3:
{
string name;
cout << "请输入学生姓名:"; cin >> name;
plist->dindValue(name);
break;
}
case 4:
{
int n;
cout << "请输入学生学号:";cin >> n;
plist->insert(n);
break;
}
case 5:
{
int n;
cout << "请输入学生学号:";cin >> n;
plist->deleted(n);
break;
}
}
}
else
{
cout << "请输入0-5中的数字!" << endl;
}
}
}