/*
* list.c
*
* Created on: Oct 31, 2010
* Author: jenson
*/
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
}
* list.c
*
* Created on: Oct 31, 2010
* Author: jenson
*/
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
}
转载于:https://blog.51cto.com/wujuxiang/413648