使用C语言实现顺序表

提示:本人使用VC2019进行编程,该软件C种可以使用C++语法,顾可能不严谨


前言

在校大学生,学习差,写写帖子长长写代码的记性。不好的地方大家请指正,谢谢。

一、顺序表

顺序表是一种最简单的数据结构。在我理解来顺序表就是一个数组,如果顺序表仅存单个数据那就和数组功能一样了,但是顺序表中可以套结构体呀,当顺序表套上结构体能处理的就比数组高端一点点了。

1.逻辑结构

顺序的逻辑结构就是线性结构嘛,简简单单的一对一,除了第一个数据和最后一个数据其他数据都有唯一前驱唯一后继,至于第一个数据嘛就只有后继,最后一个数据嘛就只有前驱。

2.在内存中的形式

顺序表在内存中一定是以连续的空间作为存放数据的地方,下面是图示:
在这里插入图片描述

3.对应操作

既然是我们自己定义的一种数据类型,那么在使用的时候是不是就该有初始化的步骤,就比如你
int a,一般都会写成int a = 0;吧?当然就int a也可以,但是在我们自己定义的数据这不要这样!!!本人亲身经历!特别是在使用到指针的地方!!不初始化很容易出错,这地方后面我在叙述。另外顺序表作为一个存放数据的东西,我们一般对数据都会进行增删查改操作,所以这也不可少。

二、代码实现

1.结构体定义

代码如下(示例):

typedef struct student {            
	int id;
	char name[20];
}stu;   //以学生作为数据元素吧

typedef struct list {
	stu data[max];
	int length;
}list;  //静态顺序表

/*
typedef是c语言中的改名卡
在此结构体定义中以上写法也等价于:typedef strucu List list
意思是将struct List改为list,那么我们在使用时就方便许多
*/

2.所有代码

代码如下(示例):

#include<stdio.h>
#include<string.h>
#include <malloc.h>
#define max 10

typedef struct student {
	int id;
	char name[20];
}stu;


typedef struct List {
	stu data[max];
	int length;
}list;  //静态顺序表

/*
typedef是c语言中的改名卡
在此结构体定义中以上写法也等价于:typedef strucu List list
意思是将struct List改为list,那么我们在使用时就方便许多
*/

void createlist(list &l) {
	l.length = 0;
	printf("create finish.\n");
}

void insertstu(list& l, int loc) {
	if (l.length < max) {
		stu s = {};
		scanf("%d", &s.id);
		scanf("%s", &s.name);
		if (loc>=l.length)
		{
			l.data[l.length] = s;
			l.length++;
			printf("insert finish.\n");
		}
		else
		{
			for (int i = l.length-1; i >= loc-1; i--)
			{
				l.data[i+1] = l.data[i];
			}
			l.data[loc] = s;
			l.length++;
			printf("insert finish.\n");
		}
	}
	else
	{
		printf("list can not add data.\n");
	}
}

int findstu(list l,int stuid) {
	for (int i = 0; i < l.length; i++)
	{
		if (l.data[i].id == stuid) {
			printf("student imformation is:%d %s\n", l.data[i].id, l.data[i].name);
			return i;
		}
	}
	printf("do not find this student.\n");
	return -1;
}

int findstu(list l,char stuname[]) {
	for (int i = 0; i < l.length; i++)
	{
		if (!strcmp(l.data[i].name,stuname)) {
			printf("student imformation is:%d %s\n", l.data[i].id, l.data[i].name);
			return i;
		}
	}
	printf("do not find this student.\n");
	return -1;
}

void deletestu(list &l,int stuid) {
	int loc = findstu(l, stuid);
	if (loc != -1) {
		for (int i = loc; i < l.length; i++)
		{
			l.data[i] = l.data[i + 1];
		}l.length--;
		printf("delete finish.\n");
	}
	else
	{
		printf("no exist this student.\n");
	}
}

void deletestu(list& l, char stuname[]) {
	int loc = findstu(l, stuname);
	if (loc != -1) {
		for (int i = loc; i < l.length; i++)
		{
			l.data[i] = l.data[i + 1];
		}l.length--;
		printf("delete finish.\n");
	}
	else
	{
		printf("no exist this student.\n");
	}
}

void alterstu(list& l, int stuid) {
	int loc = findstu(l, stuid);
	if (loc!=-1)
	{
		printf("input new imformation:\n");
		stu s = {};
		scanf("%d", &s.id);
		scanf("%s", &s.name);
		l.data[loc] = s;
		printf("alter finish.\n");
	}
	else
	{
		printf("no exsit this student.\n");
	}
}

void alterstu(list& l, char stuname[]) {
	int loc = findstu(l, stuname);
	if (loc != -1)
	{
		printf("input new imformation:\n");
		stu s = {};
		scanf("%d", &s.id);
		scanf("%s", &s.name);
		l.data[loc] = s;
		printf("alter finish.\n");
	}
	else
	{
		printf("no exsit this student.\n");
	}
}

void show(list l) {
	for (int i = 0; i < l.length; i++)
	{
		printf("%d %s\n", l.data[i].id, l.data[i].name);
	}
}

void meum() {
	printf("---1.create       2.insert---------\n");
	printf("---3.find(id)     4.find(name)-----\n");
	printf("---5.delete(id)   6.delete(name)---\n");
	printf("---7.alter(id)    8.alter(name)----\n");
	printf("---9.show         0.exit-----------\n");
	printf("---11.showlength----\n");
}

int main() {
	int a = 10;
	while (a!=0)
	{
		int id=0,loc=0;
		char stuname[20] = {};
		meum();
		int key = 0;
		scanf(" %d", &key);
		switch (key)
		{
		case 1:
			list L ;
			createlist(L);
			break;
		case 2:
			scanf(" %d", &loc);
			insertstu(L, loc);
			break;
		case 3:
			printf("input id:");
			scanf(" %d", &id);
			findstu(L, id);
			break;
		case 4:
			printf("input name:");
			scanf(" %c", stuname);
			findstu(L, stuname);
			break;
		case 5:
			printf("input id:");
			scanf(" %d", &id);
			deletestu(L, id);
			break;
		case 6:
			printf("input name:");
			scanf(" %c", stuname);
			deletestu(L, stuname);
			break;
		case 7:
			printf("input id:");
			scanf(" %d", &id);
			alterstu(L, id);
			break;
		case 8:
			printf("input name:");
			scanf(" %c", stuname);
			alterstu(L, stuname);
			break;
		case 9:
			show(L);
			break;
		case 0:
			a = 0;
			break;
		case 11:
			printf("%d\n", L.length);
			break;
		default:
			printf("input right choose.\n");
			break;
		}
	}
}

总结

第一次写帖子,很潦草。就先这样,其中有的细节在链表的时候再说

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值