码迷,mamicode.com
首页 > 其他好文 > 详细

【数据结构】顺序表seqlist

时间:2015-01-18 14:24:40      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:c语言   数据结构   c   struct   

#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,0,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;
}

运行结果:

技术分享

【数据结构】顺序表seqlist

标签:c语言   数据结构   c   struct   

原文地址:http://blog.csdn.net/a781558066/article/details/42835321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!