c语言线性表的代码,C语言实现线性表之具体实现

/*

* list.c

*

*  Created on: Oct 31, 2010

*      Author: jenson

*/

#include "list.h"

#include

#include

#include

sq_list sq_create() {

sq_list list = NULL;

list = (sq_list) malloc(sizeof(struct _sq_list) * LIST_INIT_SIZE);

if (list != NULL) {

list->data = (elem_type *) malloc(sizeof(elem_type) * LIST_INIT_SIZE);

list->length = 0;

list->list_size = sizeof(elem_type) * LIST_INIT_SIZE;

}

return list;

}

int sq_insert(sq_list list, elem_type e) {

if (list == NULL) {

perror("list is null\n");

return -1;

}

int cur_size = list->list_size;

cur_size -= sizeof(elem_type *);//在插入前判断元素的容量是否足够

if (cur_size < 0) {

list->data = (elem_type *) realloc(list->data, sizeof(elem_type)

*LIST_INCREMENT_SIZE);//重新分配元素的内存

if (list->data == NULL) {

perror("realloc list->data\n");

return -1;

}

list->length++;

list->list_size = cur_size + sizeof(elem_type) * LIST_INCREMENT_SIZE;//重新计算内存大小

list->data[list->length - 1] = e;

return 1;

} else {

list->length++;

list->list_size = cur_size;

list->data[list->length - 1] = e;

return 1;

}

}

int sq_index_of(sq_list list, elem_type e) {

if (list != NULL) {

int local = -1;

int i = 0;

for (i = 0; i < list->length; i++) {

if (e == list->data[i]) {

local = i;

break;

}

}

return local;

}

perror("list is null\n");

return -1;

}

int sq_pre_elem(sq_list list, elem_type e, elem_type * value) {

int local = sq_index_of(list, e);

if (local != -1) {

if (local == 0) {

*value = list->data[list->length - 1];//如果是第一个元素,那么它的最后面一个元素是最后一个元素

}

*value = list->data[local - 1];

return 0;

}

return -1;

}

int sq_next_elm(sq_list list, elem_type e, elem_type *value) {

int local = sq_index_of(list, e);

if (local != -1) {

if (local == list->length - 1) {

*value = list->data[0];

}

*value = list->data[local + 1];

}

return -1;

}

int sq_delete(sq_list list, int pos, elem_type *del) {

if (list != NULL && list->length > 0 && pos >= 0 && pos <= list->length - 1) {

if (pos == list->length - 1) {

*del = list->data[list->length - 1];

//       free(d);

} else {

int i = 0;

*del = list->data[pos];

for (i = pos; i <= list->length-1 ; i++) {

list->data[i] = list->data[i+1];

}

}

list->length = list->length-1;

return 1;

}

perror("error args\n");

return -1;

}

int sq_length(sq_list list) {

if (list != NULL) {

return list->length;

}

perror("list is null\n");

return -1;

}

int sq_empty(sq_list list) {

if (list != NULL) {

return (list->length > 0);

}

perror("null list \n");

return -1;

}

void sq_clear(sq_list list) {

// free(list->data);

//list->data = (elem_type *)malloc(sizeof(elem_type )* LIST_INIT_SIZE);

int i = 0;

for (i = 0; i < list->length; i++) {

(list->data[i]) = 0;

}

list->length = 0;

list->list_size = 0;

}

void sq_display(sq_list list) {

if (list != NULL) {

int i = 0;

for (i = 0; i < list->length; i++) {

if (i % 10 == 0)

printf("\n");

printf("%d\t", list->data[i]);

}

}

}

void sq_destroy(sq_list list) {

if (list != NULL) {

list->length = 0;

list->list_size = 0;

//free(list->data);

free(list);

list = NULL;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值