Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165906
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 806
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-05 10:31
个人简介

share your ideas

文章分类

全部博文(29)

文章存档

2015年(1)

2013年(28)

分类: C/C++

2013-08-19 20:34:35

本文运行环境 ubantu 12.004 gcc

#include
#include


#define DEBUG 0
#define N 5


typedef struct _sequence_list_{
int *data;
int t_size;
int c_size;
}SeqList;


SeqList *creat_seqlist(int size);
int free_seqlist(SeqList *list);
int insert_seqlist(SeqList *list, int value);
int search_seqlist(SeqList *list, int value);
int modify_seqlist(SeqList *list, int obj, int value);
int delet_seqlist(SeqList *list, int value);
int show_seqlist(SeqList *list);
void _debug_(SeqList *list);


int main()
{
SeqList *list = NULL;
int locate;
int i;


list = creat_seqlist(N);
for(i = 10 ; i < 20; i ++)
insert_seqlist(list, i);
#if DEBUG
_debug_(list);
#endif
locate = search_seqlist(list, 10);
printf("locate: %d\n", locate);
modify_seqlist(list, 12, 20);
#if DEBUG
_debug_(list);
#endif
delet_seqlist(list, 20);
show_seqlist(list);
#if DEBUG
_debug_(list);
#endif
free_seqlist(list);
return 0;
}


SeqList *creat_seqlist(int size)
{
SeqList *list = NULL;


list = (SeqList *)malloc(sizeof(SeqList));
list->data = (int *)malloc(sizeof(int) * size);
// memset(list->data, 0, sizeof(int) * size);
list->t_size = size;
list->c_size = 0;


return list;
}


int insert_seqlist(SeqList *list, int value)
{
int i;


if(NULL == list)
return -1;
if(list->c_size >= list->t_size)
return -1;

for(i = list->c_size; i > 0; i --)
list->data[i] = list->data[i - 1];
list->data[0] = value;
// *list->data = value;
list->c_size ++;
return 0;
}


void _debug_(SeqList *list)
{
int i;

for(i = 0; i < 10; i ++)
printf("%3d   ", list->data[i]);
putchar(10);
}


int free_seqlist(SeqList *list)
{
if(NULL == list)
return -1;


free(list->data);
free(list);
return 0;
}


int search_seqlist(SeqList *list, int value)
{
int i;


for(i = 0; i < list->c_size; i ++)
{
if(list->data[i] == value)
return i;
}
return -1;
}


int modify_seqlist(SeqList *list, int obj, int value)
{
int locate;


locate = search_seqlist(list, obj);
if(locate < 0)
return -1;
list->data[locate] = value;
return 0;
}


int delet_seqlist(SeqList *list, int value)
{
int locate;
int i;

if(list->c_size <= 0)
return -1;


locate = search_seqlist(list, value);
if(locate < 0)
return -1;

for(i = locate; i < list->c_size - 1; i ++)
list->data[i] = list->data[i + 1];
list->c_size --;


return 0;
}


int show_seqlist(SeqList *list)
{
int i ;


for(i = 0; i < list->c_size; i ++)
printf("%3d ", list->data[i]);
putchar(10);


return 0;
}

阅读(1688) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~