顺序表的初始化,增加,插入,删除等功能的实例
工程一共包含4个文件
1. Entity.h :声明线性表的元素的类型。可以是基本数据类型也可以是结构体
2. SeqList.h :定义线性表结构体,声明全局的宏定义,函数的声明
3. SeqList.c :具体的函数实现
4. main.c : 测试文件参考博客文章: http://www.cnblogs.com/laojie4321/archive/2012/03/30/2425015.html
参考博客文章: http://blog.163.com/jiaoruijun07@126/blog/static/68943278201042064246409/
Entity.h
typedef struct{
char key[15]; //结点的关键字
char name[20];
int age;
} DATA; //定义结点类型 可定义为简单类型或者结构体
SeqList.h
/*
头文件:数据结构的定义和操作原型
*/
#include <stdio.h>
#include <string.h>
#include "Entity.h"
#define MAXSIZE 100 //定义线性表最大长度
typedef struct {
DATA ListData[MAXSIZE + 1]; //保存顺序表的数组(真正的数据从下标为1的位置开始)
int ListLen; //顺序表结点个数(已存结点);默认为0,表示没有数据
} SeqListType;
void SeqListInit(SeqListType *SL); //初始化顺序表
int SeqListLength(SeqListType *SL); // 返回顺序表的元素数量
int SeqListAdd(SeqListType *SL, DATA data); // 向顺序表中添加元素
int SeqListInsert(SeqListType *SL, int n, DATA data); // 向顺序表中插入元素
int SeqListDelete(SeqListType *SL, int n); // 删除顺序表中的数据
DATA *SeqListFindByNum(SeqListType *SL, int n); // 根据序号返回元素
int SeqListFindByCont(SeqListType *SL, char *key); // 按关键字查找
int SeqListAll(SeqListType *SL); // 遍历顺序表
SeqList.c
/*函数文件:具体的函数实现代码*/
#include "SeqList.h"
/* 初始化顺序表 */
void SeqListInit(SeqListType *SL){
SL->ListLen = 0;
}
/* 返回顺序表元素数量 */
int SeqListLength(SeqListType *SL){
return (SL->ListLen);
}
/* 添加元素到顺序表尾 */
int SeqListAdd(SeqListType *SL, DATA data){
if(SL->ListLen >= MAXSIZE){ // 顺序表已满
printf("顺序表已经满了 不能再添加");
return 0; //返回失败
}
SL->ListData[++SL->ListLen] = data; // 把数据插入到下标为(ListLen+1)的位置
return 1;//返回成功
}
/* 插入元素到顺序表指定位置 */
int SeqListInsert(SeqListType *SL, int n, DATA data){
int i;
if(SL->ListLen >= MAXSIZE){ // 顺序表已满
printf("顺序表已经满了 不能再插入\n");
return 0;
}
if(n < 1 || n > SL->ListLen){
printf("要插入的位置错误\n");
return 0;
}
for(i = SL->ListLen; i>=n; i--){ //移动要插入数据的后面的数据
SL->ListData[i+1] = SL->ListData[i];
}
SL->ListData[n] = data; //插入数据
SL->ListLen++; //数据个数加一
return 1;
}
int SeqListDelete(SeqListType *SL, int n){
int i;
if(n < 1 || n > SL->ListLen+1){
printf("结点错误 不能删除\n");
return 0;
}
for(i=n; i<SL->ListLen; i++){ //移动要删除数据的后面的数据
SL->ListData[i] = SL->ListData[i + 1];
}
SL->ListLen--;
return 1;
}
DATA *SeqListFindByNum(SeqListType *SL, int n){
if(n < 1 || n > SL->ListLen+1){
printf("序号错误 获取失败");
return NULL;
}
return &(SL->ListData[n]); // 返回指针增加通用性
}
int SeqListFindByCont(SeqListType *SL, char *key){
int i;
for(i = 0; i <= SL->ListLen; i++){
if(strcmp(SL->ListData[i].key, key) == 0){
return i;
}
}
return 0; //遍历没有找到
}
main.c
/* 测试文件:调用测试函数*/
#include <stdio.h>
#include "SeqList.h"
/*遍历顺序表中结点*/
int SeqListAll(SeqListType *SL){
int i;
for(i = 0; i <= SL->ListLen; i++){
//输出中第一个是0, 即下标为0的位置的存储的数据
printf("(%s %s %d) \n", SL->ListData[i].key, SL->ListData[i].name, SL->ListData[i].age);
}
return 0;
}
int main(void){
int i, k;
SeqListType SL; //定义顺序表变量
DATA data, *data1; //定义结点保存数据类型变量和指针变量
char key[15]; //保存关键字
SeqListInit(&SL); //初始化数据表
do{
printf("请输入学号 姓名 年龄: ");
fflush(stdin); //清空输入缓冲区
scanf("%s %s %d", &data.key, &data.name, &data.age);
if(data.age){ //年龄不是0 退出循环
if(!SeqListAdd(&SL, data)){//添加元素到顺序表
break; //当添加失败 退出循环
}
}else{ //当年龄为0 退出循环
break;
}
}while(1);
printf("顺序表为: \n");
SeqListAll(&SL);
while(1){
fflush(stdin);
printf("\n\n输入操作\n1.获取结点位置元素\t2.内容查询\t3.添加\t4.插入\t5.删除\t6.求长度\t7.遍历\t8.退出\n:");
scanf("%d", &k);
if(k == 8){
break;
}
switch(k){
case 1:
printf("输入元素位置:");
scanf("%d", &i);
data1 = SeqListFindByNum(&SL, i);
printf("元素为:(%s %s %d) \n", data1->key, data1->name, data1->age);
break;
case 2:
printf("输入元素key(学号):");
scanf("%s", &key);
i = SeqListFindByCont(&SL, key);
if(i == 0){
printf("没有找到对应元素!");
break;
}
data1 = SeqListFindByNum(&SL, i);
printf("位置为: %d ,元素为:(%s %s %d) \n", i, data1->key, data1->name, data1->age);
break;
case 3:
printf("输入元素内容:");
scanf("%s %s %d", &data.key, &data.name, &data.age);
SeqListAdd(&SL, data);
break;
case 4:
printf("输入位置和元素内容:");
scanf("%d %s %s %d", &i, &data.key, &data.name, &data.age);
SeqListInsert(&SL, i, data);
break;
case 5:
printf("输入要删除的位置:");
scanf("%d", &i);
SeqListDelete(&SL, i);
break;
case 6:
printf("-----%d------\n", SeqListLength(&SL));
break;
case 7:
SeqListAll(&SL);
break;
}
}
return 0;
}
个人博客 欢迎来访: http://ay2626.me