顺序链表的实现

本文运行环境 ubantu 12.04 gcc

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


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

你可能感兴趣的:(c,链表)