工程由三个文件组成。
list.h
#ifndef __LIST_H__
#define __LIST_H__
struct list;
/*
构造一个空的线性表,成功返回其地址;失败返回NULL
*/
struct list *list_init();
/*
释放线性表list
*/
void list_free(struct list *list);
/*
返回线性表的数据元素个数
*/
int list_count(const struct list *list);
/*
判断线性表是否是空表,如果是返回1,否则返回0
*/
int list_isempty(const struct list *list);
/*
把线性表list清空
*/
void list_clear(struct list *list);
/*
在线性表中查找元素value,找到了返回1,否则返回0
*/
int list_lookup(struct list *list, int value);
/*
返回线性表list的index位置的数据元素
index >= 0 && index < list->count
*/
int list_get(struct list *list, int index);
/*
设置线性表list的index位置的数据元素为value
index >= 0 && index < list->count
*/
void list_set(struct list *list, int index, int value);
/*
在线性表list的index位置插入数据元素value
index >= 0 && index <= list->count
*/
void list_add(struct list *list, int index, int value);
/*
在线性表list的index位置删除数据元素,并把该元素返回
index >= 0 && index < list->count
*/
int list_remove(struct list *list, int index);
#endif /* __LIST_H__ */
main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void list_union(struct list *lista, struct list *listb);
int main(int argc, char *argv[])
{
struct list *lista = NULL;
struct list *listb = NULL;
int i;
int elem;
lista = list_init();
if (lista == NULL) {
}
listb = list_init();
list_add(lista, 0, 7);
list_add(lista, 1, 4);
list_add(lista, 2, 1);
list_add(lista, 3, 6);
list_add(lista, 4, 3); /* 7, 4, 1, 6, 3*/
for (i = 0; i < list_count(lista); i++) {
printf("%d ", list_get(lista, i));
}
printf("\n");
list_add(listb, 0, 8);
list_add(listb, 1, 4);
list_add(listb, 2, 1);
list_add(listb, 3, 12);
list_add(listb, 4, 3);
for (i = 0; i < list_count(listb); i++) {
printf("%d ", list_get(listb, i));
}
printf("\n");
list_union(lista, listb);
/* 打印 */
for (i = 0; i < list_count(lista); i++) {
printf("%d ", list_get(lista, i));
}
printf("\n");
list_free(lista);
list_free(listb);
system("PAUSE");
return 0;
}
void list_union(struct list *lista, struct list *listb)
{
int i;
int elem;
for (i = 0; i < list_count(listb); i++) {
elem = list_get(listb, i);
if (list_lookup(lista, elem) == 0) {
list_add(lista, list_count(lista), elem);
}
}
return;
}
int max(int a[], int n)
{
}
list.c
#include "list.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#define LIST_INIT_SIZE 1
#define LIST_INCR_SIZE 1
struct list {
int *elements;
int size;
int count;
};
/*
构造一个空的线性表,成功返回其地址;失败返回NULL
*/
struct list *list_init()
{
struct list *list = NULL;
list = (struct list *)malloc(sizeof(struct list));
if (list == NULL) return NULL;
assert(list != NULL);
list->elements = NULL;
list->size = 0;
list->count = 0;
list->elements = (int *)malloc(sizeof(int) * LIST_INIT_SIZE);
if (list->elements == NULL) {
free(list);
return NULL;
}
assert(list->elements != NULL);
list->size = LIST_INIT_SIZE;
memset(list->elements, 0, sizeof(int) * list->size);
return list;
}
/*
释放线性表list
*/
void list_free(struct list *list)
{
assert(list != NULL);
assert(list->elements != NULL);
free(list->elements);
free(list);
return;
}
/*
返回线性表的数据元素个数
*/
int list_count(const struct list *list)
{
assert(list != NULL);
assert(list->elements != NULL);
return list->count;
}
/*
判断线性表是否是空表,如果是返回1,否则返回0
*/
int list_isempty(const struct list *list)
{
assert(list != NULL);
assert(list->elements != NULL);
return (list->count == 0);
}
/*
把线性表list清空
*/
void list_clear(struct list *list)
{
// int i;
assert(list != NULL);
assert(list->elements != NULL);
// for (i = 0; i < list->cout; i++) {
// list->elements[i] = 0;
// }
// memset(list->elements, 0, sizeof(int) * list->count);
list->count = 0;
return;
}
/*
在线性表中查找元素value,找到了返回1,否则返回0
*/
int list_lookup(struct list *list, int value)
{
int i;
assert(list != NULL);
assert(list->elements != NULL);
for (i = 0; i < list->count; i++) {
if (list->elements[i] == value) return 1;
}
return 0;
}
/*
返回线性表list的index位置的数据元素
index >= 0 && index < list->count
*/
int list_get(struct list *list, int index)
{
assert(list != NULL);
assert(list->elements != NULL);
assert(index >= 0 && index < list->count);
return list->elements[index];
}
/*
设置线性表list的index位置的数据元素为value
index >= 0 && index < list->count
*/
void list_set(struct list *list, int index, int value)
{
assert(list != NULL);
assert(list->elements != NULL);
assert(index >= 0 && index < list->count);
list->elements[index] = value;
return;
}
/*
在线性表list的index位置插入数据元素value
index >= 0 && index <= list->count
*/
void list_add(struct list *list, int index, int value)
{
int i;
assert(list != NULL);
assert(list->elements != NULL);
assert(index >= 0 && index <= list->count);
if (list->count == list->size) {
int *tmp_p = NULL;
tmp_p = (int *)realloc(list->elements, sizeof(int) *(list->size + LIST_INCR_SIZE));
if (tmp_p == NULL) {
perror("list.c add realloc error!");
exit(1);
}
list->elements = tmp_p;
list->size += LIST_INCR_SIZE;
}
for (i = list->count -1; i >= index; i--) {
list->elements[i+1] = list->elements[i];
}
list->elements[index] = value;
list->count++;
return;
}
/*
在线性表list的index位置删除数据元素,并把该元素返回
index >= 0 && index < list->count
*/
int list_remove(struct list *list, int index)
{
int i;
int ret;
assert(list != NULL);
assert(list->elements != NULL);
assert(index >= 0 && index < list->count);
ret = list->elements[index];
// for (i = index; i < list->count -1; i++) {
// list->elements[i] = list->elements[i+1];
// }
for (i = index + 1; i < list->count; i++) {
list->elements[i-1] = list->elements[i];
}
list->count--;
return ret;
}