数据结构线性表 - 单链表练习Demo(稍作修改即为各种管理系统)
话不多说,直接上代码,自己写完测试完,所有功能正常。
#include <iostream>
using namespace std;
typedef struct{
char a;
}Elem;
typedef struct LNode{
Elem data;
LNode *next;
} *LinkList;
int InitList(LinkList &L)
{
L = new LNode;
L->next = NULL;
return 1;
}
void CreateList_H(LinkList &L)
{
char a = NULL;
cout << "请输入数据,按#号键结束!" << "\n";
while (a != '#')
{
LinkList s;
s = new LNode;
cin >> a;
if (a == '#')
{
break;
}
s->data.a = a;
s->next = L->next;
L->next = s;
}
cout << "数据插入完毕!";
}
void CreateList_L(LinkList &L)
{
LinkList p;
p = L;
char a = NULL;
cout << "请输入数据,按#号键结束!" << "\n";
while (a != '#')
{
LinkList s;
s = new LNode;
cin >> a;
if (a == '#')
{
break;
}
s->data.a = a;
s->next = p->next;
p->next = s;
p = s;
}
cout << "数据插入完成!";
}
void Printlist(LinkList &L)
{
LinkList p;
p = L;
while (p->next != NULL )
{
p = p->next;
cout << p->data.a;
}
}
void SearchList(LinkList &L,char k)
{
int j = 0;
LinkList p;
p = L;
while (p->next != NULL)
{
if (p->data.a == k)
{
cout << "查询" << k << "数据是链表中第" << j << "个数据!"<<"\n";
return;
}
else
{
p = p->next;
j++;
}
}
}
void DeleteList(LinkList &L , int k)
{
LinkList p,q;
int j;
p = L;
j = 0;
while (p->next != NULL && j < k - 1)
{
p = p->next;
++j;
}
if (!(p->next) || j > k - 1)
{
cout << "删除结点不存在!";
return;
}
q = p->next;
p->next = q->next;
delete q;
cout << "删除成功!";
}
void AlterList(LinkList &L,int k)
{
LinkList p;
int j = 0;
char b;
p = L;
while (p->next && j < k)
{
p = p->next;
++j;
}
if (!(p->next) || j > k)
{
cout << "修改失败,无此结点!";
return;
}
cout << "请输入要修改的值:";
cin >> b;
p->data.a = b;
cout << "修改成功!";
}
void InsertList(LinkList &L, int k)
{
LinkList p, q;
int j;
char d;
p = L;
j = 0;
while (p->next != NULL && j < k - 1)
{
p = p->next;
++j;
}
if (!(p->next) || j > k - 1)
{
cout << "删除结点不存在!";
return;
}
q = new LNode;
cout << "请输入插入数据的值:" << "\n";
cin >> d;
q->next = p->next;
p->next = q;
q->data.a = d;
cout << "插入成功!";
}
void JudgeList(LinkList &L)
{
if (L->next != NULL)
{
cout << "链表非空!";
}
else
{
cout << "链表为空!";
}
}
void LengthList(LinkList &L)
{
int j = 0;
LinkList p;
p = L;
while (p->next != NULL)
{
p = p->next;
++j;
}
cout << "链表长度为" << j;
}
void GetDataList(LinkList &L, int k)
{
LinkList p;
int j;
p = L;
j = 0;
while (p->next != NULL && j < k)
{
p = p->next;
++j;
}
if (!(p->next) || j > k)
{
cout << "取值结点不存在!";
return;
}
cout << p->data.a;
}
void ClearList(LinkList &L)
{
LinkList p,q;
p = L->next;
while (p->next != NULL)
{
q = p;
p = p->next;
delete q;
}
L->next = NULL;
cout << "链表清空!";
}
void DestoryList(LinkList &L)
{
LinkList p;
p = L;
while (L)
{
p = L;
L = L->next;
delete p;
}
cout << "链表销毁成功!";
}
void Contents()
{
cout << "*****************************************\n";
cout << "* 单 链 表 练 习 *\n";
cout << "* *\n";
cout << "* 1.建立单链表 *\n";
cout << "* 2.单链表头插数据 *\n";
cout << "* 3.显示单链表数据 *\n";
cout << "* 4.单链表尾插数据 *\n";
cout << "* 5.查询单链表的数据位置 *\n";
cout << "* 6.删除指定位置的结点数据 *\n";
cout << "* 7.修改单链表指定位置上的数据 *\n";
cout << "* 8.插入指定位置上的数据 *\n";
cout << "* 9.判断链表是否为空 *\n";
cout << "* 10.单链表长度 *\n";
cout << "* 11.清空单链表 *\n";
cout << "* 12.销毁链表 *\n";
cout << "* 13.取得单链表指定位置上的数据 *\n";
cout << "* 14.退出程序 *\n";
cout << "* *\n";
cout << "*****************************************\n";
}
void main()
{
LinkList L;
int a,flag,b;
char k;
flag = 0;
Contents();
while (1)
{
system("cls");
Contents();
cout << "请选择功能:";
cin >> a;
switch (a)
{
case 1: if (InitList(L)){ cout << "单链表创建成功"; flag = 1; }else { cout << "单链表创建成功"; }system("PAUSE"); break;
case 2: if (flag){ CreateList_H(L); }else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 3: if (flag){ Printlist(L); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 4: if (flag){ CreateList_L(L); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 5: if (flag){ cout << "请输入要查找的数据:"; cin >> k; SearchList(L, k); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 6: if (flag){ cout << "请输入要删除第几个数据:"; cin >> b; DeleteList(L, b); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 7: if (flag){ cout << "请输入要修改第几个数据:"; cin >> b; AlterList(L, b); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 8: if (flag){ cout << "请输在第几个位置上插入数据:"; cin >> b; InsertList(L, b); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 9: if (flag){ JudgeList(L); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 10: if (flag){ LengthList(L); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 11: if (flag){ ClearList(L); } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 12: if (flag){ DestoryList(L); flag = 0; } else {cout<< "请先创建单链表!";} system("PAUSE"); break;
case 13: if (flag){ cout << "请输入要取第几个位置上的数据:"; cin >> b; GetDataList(L, b); } system("PAUSE"); break;
case 14: exit(-1);
default: printf("输入错误!请重新输入"); system("PAUSE"); break;
}
}
}
链式存储:用一组任意的存储单元存储线性表中的元素。 存储单元可以是连续的也可以是不连续的。 数据的逻辑顺序和物理顺序不一定相同 单链表中有一个指针域(*next),和一个数据域(data) 插入,查找,删除等,时间复杂度都是O(n)
有问题可以留言私信,大家一同探讨!
|