【数据结构】顺序表seqlist

前端之家收集整理的这篇文章主要介绍了【数据结构】顺序表seqlist前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct SeqlistNode
{
	 int date;
}seqlistNode;

typedef struct Seqlist
{
	unsigned int lengh;   //已存数据长度
	unsigned int count;    //最大长度
	seqlistNode * date;   //数据域
	
}seqlist;

seqlist * Creatlist(int count)
{
	seqlist * s=NULL;
	if(count==0)
		return NULL;
	s=(seqlist *)malloc(sizeof(seqlist)+count*sizeof(seqlistNode));  //动态开辟内存
	memset(s,sizeof(seqlist)+count*sizeof(seqlistNode));       //自己管理
	s->date=(seqlistNode *)(s+1);
	s->lengh=0;
	s->count=count;
	return s;
}

int insertList(seqlist *s,seqlistNode n,unsigned pos)
{
	unsigned i;
	if(s==NULL) return -1;
	if(pos > (s->count-1) )return -2;
	if(s->lengh==s->count) return -3;
	if(pos > s->lengh) pos=s->lengh;
	for(i=s->lengh;i!=pos;i--)
		s->date[i].date=s->date[i-1].date;
	s->date[pos].date=n.date;
	s->lengh+=1;
	return 0;
}

int dellist(seqlist *s,unsigned pos)
{	
	unsigned int i;
	if(s==NULL) return -1;
	if(pos>s->lengh) return -2;
	for(i=pos;i<s->lengh-1;i++)
		s->date[i]=s->date[i+1];
	s->lengh--;
	return 0;
}

void destroylist(seqlist* list)
{
	if(list!=NULL)
		free(list);
}


void display(seqlist *s)
{
	unsigned i;
	for(i=0;i<s->lengh;i++)
	{
		printf("%d ",s->date[i].date);
	}
	printf("\n");
}

int main()
{
	seqlist *list=NULL;
	unsigned i=0;
	list=Creatlist(10);
	for(i;i<list->count;i++)
	{
		seqlistNode n;
		n.date=i;
		insertList(list,n,0);
	}
	display(list);

	for(i=0;i<list->count;i++)
	{
		dellist(list,0);
		display(list);
	}
	destroylist(list);
	return 0;
}
 

运行结果:

原文链接:https://www.f2er.com/datastructure/382727.html

猜你在找的数据结构相关文章