数据结构c语言-顺序表项目

数据结构->存储结构->顺序存储:内存开辟空间连续

顺序表项目操作要求:
        1.创建空的顺序表
        2.指定位置插入数据
        3.判断表是否满
        4.指定位置删除数据
        5.判断表是否为空
        6.遍历顺序表
        7.修改指定位置的值
        8.查询指定位置的值
        9.将指定的值修改为新的值
        10.查询指定值出现的位置
        11.清空顺序表
        12.销毁顺序表

分析:共需4个文件:seqlist.h  seqlilst.c  main.c  Makefile

seqlist.h(顺序表头文件)

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define N 6

typedef int datatype;
typedef struct
{
	datatype data{N};
	int last;
}seqlist_t;

//1.创建空顺序表
seqlist_t *createSeqlist(void);
//2.指定位置插入数据
int insertPostSeqlist(seqlist_t *p,int post,datatype data);
//3.判断顺序表是否已满
int ifFullSeqlist(seqlist_t *p);
//4.指定位置删除数据
int deletePostSeqlist(seqlist_t *p,int post);
//5.判断顺序表是否为空
int ifEmptySeqlist(seqlist_t *p);
//6.遍历顺序表
void showSeqlist(seqlist_t *p);
//7.修改指定位置的值
int changePostSeqlist(seqlist_t *p,int post,datatype data);
//8.查询指定位置的值
datatype searchPostSeqlist(seqlist_t *p,int post);
//9.将指定的值修改为新的值
int changeDataSeqlist(seqlist_t *p,datatype old,datatype new);
//10.查询指定值出现的位置
int searchDataSeqlist(seqlist_t *p,datatype data);
//11.清空顺序表
void cleanSeqlist(seqlist_t *p);
//12.销毁顺序表
void destorySeqlist(seqlist_t **q);

#endif

seqlist.c(顺序表操作函数) 

#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"

//1.创建空顺序表
seqlist_t *createSeqlist(void)
{
	seqlist_t *p;
	p=(seqlist_t *)malloc(sizeof(seqlist_t));
	if(NULL == p)
	{
		printf("createSeqlist err.\n");
		return NULL;
	}
	p->last=-1;
	return p;
}
//2.指定位置插入数据
int insertPostSeqlist(seqlist_t *p,int post,datatype data)
{
	//1.容错处理(判断插入位置是否合理)
	if(post<0 || post>p->last+1 || ifFullSeqlist(p))
	{
		printf("insertPostSeqlist err.\n");
		return -1;
	}
	//2.将要插入位置及之后的数据向后移一个
	int i;
	for(i=p->last;i>=post;i--)
	{
		p->data[i=1]=p->data[i];
	}
	//3.插入数据
	p->data[post]=data;
	p->last++;
	return 0;
}
//3.判断顺序表是否已满
int ifFullSeqlist(seqlist_t *p)
{
	return p->last == N;
}
//4.指定位置删除数据
int deletePostSeqlist(seqlist_t *p,int post)
{
	//1.容错处理(判断插入位置是否合理)
	if(post<0 || post>p->last || ifEmptySeqlist(p))
	{
		printf("deletePostSeqlist err.\n");
		return -1;
	}
	//2.将要删除位置之后的数据往前移动一个
	int i;
	for(i=post;i<p->last;i++)
	{
		p->data[i]=p->data[i+1];
	}
	p->last--;
	return 0;
}
//5.判断顺序表是否为空
int ifEmptySeqlist(seqlist_t *p)
{
	return p->last == -1;
}
//6.遍历顺序表
void showSeqlist(seqlist_t *p)
{
	int i;
	for(i=0;i<=p->last;i++)]
	{
		printf("%d",p->data[i]);
	}
	putchar(10);
}
//7.修改指定位置的值
int changePostSeqlist(seqlist_t *p,int post,datatype data)
{
	if(post<0 || post>p->last || ifEmptySeqlist(p))
	{
		printf("changePostSeqlist err.\n");
		return -1;
	}
	p->data[post]=data;
	return 0;
}
//8.查询指定位置的值
datatype searchPostSeqlist(seqlist_t *p,int post)
{
	if(post<0 || post>p->last || ifEmptySeqlist(p))
	{
		printf("searchPostSeqlist err.\n");
		return -1;
	}
	return p->data[post];
}
//9.将指定的值修改为新的值
int changeDataSeqlist(seqlist_t *p,datatype old,datatype new)
{
	int i;
	for(i=0;i<=p->last;i++)
	{
		while(p->data[i]==old)
			p->data[i]=new;
	}
	return 0;
}
//10.查询指定值出现的位置
int searchDataSeqlist(seqlist_t *p,datatype data)
{
	int i;
	for(i=0;i<=p->last;i++)
	{
		while(p->data[i]==data)
			return i;
	}
	return 0;
}
//11.清空顺序表
void cleanSeqlist(seqlist_t *p)
{
	p->last=-1;
}
//12.销毁顺序表
void destorySeqlist(seqlist_t **q)
{
	if(*q->last != -1)
		cleanSeqlist(*q);
	free(*q);
	*q=NULL;
}







main.c(主函数-顺序表操作)

#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"

int main(int argc,const char *argv[])
{
	seqlist_t *p=NULL;
	//创建空顺序表
	p=createSeqlist();
	//向顺序表中插入数据
	insertPostSeqlist(p,0,1);  //顺序表为1 2 3 4 5 6且last=5
	insertPostSeqlist(p,1,2);  
	insertPostSeqlist(p,2,3);
	insertPostSeqlist(p,3,4);
	insertPostSeqlist(p,4,5);
	insertPostSeqlist(p,5,6);
	showSeqlist(p);
	//删除顺序表中第6个数据(即post=5)
	deletePostSeqlist(p,5);
	showSeqlist(p);
	//将post=2的数据改为30
	changePostSeqlist(p,2,30);
	//查询post=2的数据
	printf("data:%d\n",searchPostSeqlist(p,2));
	//将值为30的数据改为3
	changeDataSeqlist(p,30,3);
	//查询值为3的数据的位置(下标)
	printf("post:%d\n",searchDataSeqlist(p,3));
	//清空顺序表
	cleanSeqlist(p);
	//销毁顺序表
	destorySeqlist(&p);
    
    return 0;
}

Makefile文件:

OBJS=main.o seqlist.o
CC=gcc
CFLAGS= -c -g -O
test:$(OBJS)
	$(CC) $(OBJS) -o $@
$(OBJS):%.o:%.c
	$(CC) $(CFLAGS) $^ -o $@
.PHONY.clean:
clean:
	rm -f *.o test

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷看月亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值