数据结构-顺序表

顺序表


#include <stdio.h>
#include <malloc.h>
//存储方式:连续存储
//对于一旦建立就不需要经常插入或者删除的可以使用顺序表,因为其查找,插入,删除复杂度较高
struct SeqList
{
	int MAXNUM;
	int n;
	unsigned char *element;
	
};

typedef struct SeqList *mySeqList;

mySeqList createNull_seqList(int n)
{
	mySeqList seqlist = (mySeqList)malloc(sizeof(struct SeqList));
	
	if (seqlist!= NULL)
	{
		seqlist ->element = (unsigned char*)malloc(sizeof(unsigned char)*n);
		if(seqlist ->element)
		{
			seqlist -> MAXNUM = n;
			seqlist ->n = 55;
			return seqlist;
		}
		else free(seqlist);
	}
	printf("out of space!!");
	return NULL;
	
	
}
int isNullList_seq(mySeqList seqlist)
{
	
	return (seqlist->n ==0);
	
}
//复杂度
int locate_seq(mySeqList list,unsigned char x)
{
	
	int q;
	for (q=0;q<list->n;q++)
	{
		if(list->element[q] ==x) return q;
		
		
	}
	return -1;
}
//复杂度:(n-1)/2
int insertPre_seq(mySeqList list,int p,unsigned char x)
{
	int q;
	if(list->n >=list->MAXNUM)
	{
		printf("overflow!!!");
		return -1;
	}
	if (p <0 || p > list->n)
	{
		printf("n is %d\n",list->n);
		printf("not exist\n");
		return -1;
		
	}
	//移动时从后面开始
	for(q=list->n-1;q>=p;q--) 
		list->element[q+1] = list->element[q];

	list ->element[p] =x;
	list ->n= list->n+1;
	return 1;
	
}

int deleteP_seq(mySeqList list,int p)
{
	int q;
	if (p<0 || p>list->n-1)
	{
		printf("not existed");
		return -1;
	}
	//删除时从前面开始移动
	for(q=p; q<list->n-1; q++)
		list ->element[q] = list ->element[q+1];
	
	list ->n =list->n-1;
	return 1;
	
	
	
}

void main()
{
	int is_success;
	int position;
	mySeqList mySeq_List = createNull_seqList(100);
	is_success = insertPre_seq(mySeq_List,30,40);
	position = locate_seq(mySeq_List,40);
	printf("position is %d",position);
	
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值