数据结构:顺序表

顺序表

#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>

#define MAXSIZE 100 //顺序表的最大长度

typedef struct {
	char key[15];
	char name[20];
	int age;
}DATA;
typedef struct {
	DATA ListData[MAXSIZE + 1];
	int ListLen;//顺序表已保存节点的数量
}SeqListType;

int SeqListAll(SeqListType *SL);//遍历顺序表中的内容
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 main() {
	int i;
	SeqListType SL;
	DATA data, *data1;
	char key[15];

	SeqListInit(&SL);//初始化

	do {
		printf("输入添加的节点(学好   姓名   年龄):\n");
		fflush(stdin);//清空输入缓冲区

		scanf("%s%s%d", &data.key, &data.name, &data.age);

		if (data.age) {
			if (!SeqListAdd(&SL, data)) {
				break;
			}
		}
		else {
			break;
		}
	} while (1);

	printf("\n顺序表中的节点顺序为:\n");
	SeqListAll(&SL);//显示所有节点数据

	//fflush(stdin);

	printf("\n要取出节点的序号:");
	scanf("%d", &i);
	data1 = SeqListFindByNum(&SL, i);//按序号查找节点
	if (data1) {
		printf("第%d个节点为:(%s,%s,%d)\n", i, data1->key, data1->name, data1->age);
	}

	//fflush(stdin);
	printf("\n要查找节点的关键字:");
	scanf("%s", key);

	i = SeqListFindByCont(&SL, key);//按关键字查找
	data1 = SeqListFindByNum(&SL, i);//按序号查找

	if (data1) {
		printf("第%d个节点为:(%s,%s,%d)\n", i, data1->key,
			data1->name, data1->age);
	}

	getch();
	return 0;
}

int SeqListAll(SeqListType *SL) {
	//显示所有节点
	int i;
	for (i = 1; i <= SL->ListLen; i++) {
		printf("(%s, %s, %s)\n", SL->ListData[i].key,
			SL->ListData[i].name,
			SL->ListData[i].age);
	}
}

void SeqListInit(SeqListType *SL) {
	//初始化顺序表
	SL->ListLen = 0;//初始化时,设置顺序表长度为0
}
int SeqListLength(SeqListType *SL) {
	//长度
	return (SL->ListLen);//返回顺序表的元素数量
}
int SeqListAdd(SeqListType *SL, DATA data) {
	//添加在最后一个位置,添加一个节点数据
	if (SL->ListLen >= MAXSIZE) {
		printf("顺序表已经满了,不能再次添加节点了!\n");
		return 0;
	}
	SL->ListData[++SL->ListLen] = data;
	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 - 1) {
		printf("输入节点序号错误,不能插入元素!\n");
		return 0;
	}

	for (i = SL->ListLen; i >= n; i--) {
		SL->ListData[i + 1] = SL->ListData[i];
	}

	SL->ListData[n] = data;//插入节点
	SL->ListLen++;//顺序表节点数量增加1
	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--;//顺序表元素数量减少1

	return 1;

}
DATA *SeqListFindByNum(SeqListType *SL, int n) {
	//根据序号返回元素
	if (n<1 || n>SL->ListLen + 1) {
		printf("节点序号错误,不能返回节点!\n");
		return NULL;//返回NULL,表示不成功
	}
	return &(SL->ListData[n]);

}
int SeqListFindByCont(SeqListType *SL, char *key) 
{
	//按关键字查找
	int i;
	for (i = 1; i <= SL->ListLen; i++) 
	{
		if (strcmp(SL->ListData[i].key, key) == 0) 
		{
			return i;
		}
	}
	return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值