数据结构--顺序表

seqlist.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define MAXSIZE 100
typedef int data_t;
typedef struct{
	data_t data[MAXSIZE];
	int last;
}seqlist_t;


seqlist_t *create_seqlist(void);
void clear_seqlist(seqlist_t *L);

int is_empty_seqlist(seqlist_t *L);
int is_full_seqlist(seqlist_t *L);
void set_empty_seqlist(seqlist_t *L);
int get_length_seqlist(seqlist_t *L);
void show_seqlist(seqlist_t *L);

int insert_seqlist(seqlist_t *L,data_t x,int pos);
int delete_seqlist(seqlist_t *L,int pos);
int change_seqlist(seqlist_t *L,data_t x,int pos);
int search_seqlist(seqlist_t *L,data_t x);

#endif

seqlist.c

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



seqlist_t *create_seqlist(void)
{
	seqlist_t *L=NULL;
	L=(seqlist_t *)malloc(sizeof(seqlist_t));
	if(L == NULL)
	{
		puts("malloc no memory!");
		return NULL;
	}
	L->last = -1;
	return L;
}
void clear_seqlist(seqlist_t *L)
{
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return ;
	}
	free(L);
	L = NULL;
	return ;
}

int is_empty_seqlist(seqlist_t *L)
{
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return -1;
	}
	return (L->last == -1);
	
}
int is_full_seqlist(seqlist_t *L)
{
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return -1;
	}
	return (L->last == MAXSIZE-1);
}
void set_empty_seqlist(seqlist_t *L)
{
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return ;
	}
	L->last = -1;
	return ;	
}
int get_length_seqlist(seqlist_t *L)
{
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return -1;
	}
	return (L->last+1);
}
void show_seqlist(seqlist_t *L)
{
	int i=0;
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return ;
	}
	for(i=0;i<=L->last;i++)
	{
		printf("L->data[%d] = %d\n",i,L->data[i]);
	}
	return ;
}

int insert_seqlist(seqlist_t *L,data_t x,int pos)
{
	int i=0;
	if(is_full_seqlist(L) || pos<0 || pos >L->last+1)
	{
		puts("input argv is invalid");
		return -1;
	}
	for(i=L->last;i>=pos;i--)
	{
		L->data[i+1] = L->data[i];
	}
	L->data[pos] = x;
	L->last++;
	return 0;


}
int delete_seqlist(seqlist_t *L,int pos)
{
	int i=0;
	if(pos<0 || pos>L->last)
	{
		puts("input pos is invalid");
		return -1;
	}
	for(i=pos;i<L->last;i++)
	{
		L->data[i] = L->data[i+1];
	}
	L->last--;
	return 0;
}
int change_seqlist(seqlist_t *L,data_t x,int pos)
{
	if(pos<0 || pos>L->last)
	{
		puts("input pos is invalid");
		return -1;
	}
	L->data[pos] = x;
	return 0;
}
int search_seqlist(seqlist_t *L,data_t x)
{
	int i=0;
	if(L == NULL)
	{
		puts("seqlist_t *L is NULL");
		return -1;
	}
	for(i=0;i<get_length_seqlist(L);i++)
	{
		if(L->data[i] == x)
		return i;
	}
	return -1;
}

main.c

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


int main(int argc, const char *argv[])
{
	int i=0;


	seqlist_t *L=NULL;
	L=create_seqlist();
 	
	if(is_empty_seqlist(L))printf("seqlist L is empty\n");
	printf("last=%d\n",L->last);
	for(i=0;i<5;i++)
	{
		insert_seqlist(L,i,i);
	}
	printf("last=%d\n",L->last);
	printf("seqlist L length is %d\n",get_length_seqlist(L));

	show_seqlist(L);
	puts("==========yy================================");

	printf("3 in seqlist L is %d\n",search_seqlist(L,3));
	delete_seqlist(L,search_seqlist(L,3));
	printf("seqlist L length is %d\n",get_length_seqlist(L));
	show_seqlist(L);
	puts("==========yy================================");

	change_seqlist(L,8,search_seqlist(L,4));
	show_seqlist(L);
	puts("==========yy================================");

	set_empty_seqlist(L);

	show_seqlist(L);

	clear_seqlist(L);


	return 0;
}

输出结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值