c++链表演示
2024-09-07 03:02:42
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
#define MAXLEN 100
typedef struct
{
char key[];
char name[];
int age;
}DATA;
typedef struct
{
DATA ListData[MAXLEN + ];
int ListLen;
}SLType;
void SLInit(SLType*SL)//初始化顺序表
{
SL->ListLen = ;
}
int SLLength(SLType*SL)//返回顺序表的元素数量
{
return(SL->ListLen);
}
int SLInsert(SLType*SL, int n, DATA data)//只在表间插入
{
int i;
if (SL->ListLen >= MAXLEN)
{
cout << "顺序表已满,不能插入结点!\n";
return ;
}
if (n< || n>SL->ListLen - )//插入结点序号不正确
{
cout<<"插入结点的序号错误";
return ;
}
for (i = SL->ListLen; i >= n; i--)
{
SL->ListData[i + ] = SL->ListData[i];
}
SL->ListData[n] = data;
SL->ListLen++;
return ;//成功插入返回1
}
int SLAdd(SLType*SL, DATA data)
{
if (SL->ListLen >= MAXLEN)
{
cout << "顺序已满,不能再添加结点了" << endl;
return ;
}
SL->ListData[++SL->ListLen] = data;
return ;
}
int SLDelete(SLType* SL, int n)
{
int i;
if (n< || n>SL->ListLen + )
{
cout << "删除结点序号错误,不能删除";
return ;
}
for (i = n; i < SL->ListLen; i++)
{
SL->ListData[i] = SL->ListData[i + ];
}
SL->ListLen--;
return ;
}
DATA*SLFindByNum(SLType*SL, int n)
{
if (n< || n>SL->ListLen + )
{
cout << "结点序号错误,不能返回结点";
return NULL;
}
return &(SL->ListData[n]);
}
int SLFindByCont(SLType*SL, char *key)
{
int i;
for (i = ; i < SL->ListLen; i++)
{
if (strcmp(SL->ListData[i].key, key) == )
{
return i;//返回结点序号
}
}
return ;
}
int SLALL(SLType*SL)
{
int i;
for (i = ; i <=SL->ListLen; i++)
{
cout << SL->ListData[i].key << SL->ListData[i].name << SL->ListData[i].age << endl;
}
return ;
}
int main()
{
int i;
SLType SL;
DATA data;
DATA *pdata;
char key[];
cout << "顺序操作表演示" << endl;
SLInit(&SL);
cout << "链表初始化完成" << endl;
do
{
cout << "输入添加的结点(学号 姓名 年龄):" << endl;
fflush(stdin);//清空输入缓冲区
cin>>data.key>>data.name>>data.age;
if (data.age)//若年龄不为0
{
if (!SLAdd(&SL, data))
{
break;
}
}
else
{
break;
}
} while ();
cout << "顺序表中的结点顺序为:" << endl;
SLALL(&SL);//显示所有的结点数据
fflush(stdin);
cout << "要取出结点的序号:" << endl;
cin >> i;
pdata = SLFindByNum(&SL, i);
if (pdata)
{
cout << i << pdata->key << pdata->name << pdata->age << endl;
}
fflush(stdin);
cout << "要查找结点的关键字" << endl;
cin >> key;
i = SLFindByCont(&SL, key);
pdata = SLFindByNum(&SL, i);
if (pdata)
{
cout << i << pdata->key << pdata->name << pdata->age << endl;
}
getch();
return ;
}
最新文章
- 【Delphi】最小化事件捕捉
- html 5新特性 --用SVG绘制的微信logo
- Eclipse 中隐藏的 5 个非常有用的功能
- SQL Server Data Tool 嘹解(了解)一下 SSDT -摘自网络
- Bash For Loop Examples
- Java cookie的使用
- UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)
- 从0.5开始学Java 零
- 基于RBAC的权限设计模型
- 【转】FLEX中SharedObject介绍及应用
- Ext 常用组件解析
- Codeforces 343E Pumping Stations
- 【python】正则替换
- javascript 值类型和引用类型
- Java编程思想 学习笔记10
- maven上传jar包到nexus私服后的存放路径 以及 使用IDEA上传jar包的步骤
- Effective Java (6) - 消除过期的对象引用
- 【python-opencv】17-形态学操作-腐蚀与膨胀
- new operator
- linux上设置mysql编码