数据结构day2

1、创建顺序表
2、判空
3、判满
4、头插
5、尾插
6、头删
7、尾删
8、清空顺序表
9、释放顺序表 

main.c

#include "seq_list.h"
int main(int argc, const char *argv[])
{
	seq_p S =create_seq();
	printf("判空%d\n",empty_seq(S));
	printf("判满%d\n",full_seq(S));
	start_insert(S,1);
	start_insert(S,2);
	end_insert(S,3);
	end_insert(S,4);
	show(S);
	start_delete(S);
	end_delete(S);
	show(S);
	delete_all(S);
	show(S);
	printf("%p\n",S);
	free_list(S);
	if(S==NULL)
		printf("NULL!\n");
	start_insert(S,2);
	return 0;
}

seq_list.c

#include "seq_list.h"
seq_p create_seq()
{
	seq_p S=(seq_p)malloc(sizeof(seq_list));
	if(S==NULL)
	{
		printf("Space request failed\n");
		return NULL;
	}
	S->len = 0;
	bzero(S->data,sizeof(S->data));
	return S;
}
int empty_seq(seq_p S)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return -1;
	}
	return S->len==0?1:0;
}
int full_seq(seq_p S)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return -1;
	}
	return S->len>MAX?1:0;
}
void start_insert(seq_p S,datatype data)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	if(full_seq(S))
	{
		printf("FULL can't insert\n");
		return;
	}

	for(int i=(S->len)-1;i>=0;i--)
	{
		S->data[i+1]=S->data[i];
	}
	S->data[0]=data;
	S->len++;
	}
void end_insert(seq_p S,datatype data)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	if(full_seq(S))
	{
		printf("FULL can't insert\n");
		return;
	}
	S->data[S->len]=data;
	(S->len)++;
}
void show(seq_p S)
{
	if(S==NULL)
	{
		printf("NULL\n");
		return;
	}
	if(empty_seq(S))
	{
		printf("S = empty\n");
		return;
	}
	for(int i=0;i<S->len;i++)
	{
		printf("%d ",S->data[i]);
	}
	putchar(10);
}
void start_delete(seq_p S)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	if(empty_seq(S))
	{
		printf("empty can't delete\n");
		return;
	}
	for(int i=0;i<S->len-1;i++)
	{
		S->data[i]=S->data[i+1];
	}
	S->len--;
}
void end_delete(seq_p S)
	{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	if(empty_seq(S))
	{
		printf("empty can't delete\n");
		return;
	}
	S->data[S->len-1]=0;
	S->len--;
	}
void delete_all(seq_p S)
	{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	if(empty_seq(S))
	{
		printf("empty can't delete\n");
		return;
	}
	S->len = 0;
	bzero(S->data,sizeof(S->data));
	}
void free_list(seq_p S)
{
	if(S==NULL)
	{
		printf("S==NULL!\n");
		return;
	}
	free(S);
	bzero(S,sizeof(S));
	S=NULL;
	if(S==NULL)
		printf("NULL!\n");
}

seq_list.h

#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 7
typedef int datatype;
typedef struct seq_list
{
	datatype data[MAX];
	int len;
}seq_list,*seq_p;
seq_p create_seq();
int empty_seq(seq_p S);
int full_seq(seq_p S);
void start_insert(seq_p S,datatype data);
void end_insert(seq_p S,datatype data);
void show(seq_p S);
void start_delete(seq_p S);
void end_delete(seq_p S);
void delete_all(seq_p S);
void free_list(seq_p S);
#endif

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值