请教一下线性表的问题?
程序代码:
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 struct date { char data[MAXSIZE]; int length; }; void create_list(struct date *l, char str[], int n); void init_list(struct date *l); void destory_list(struct date *l); int list_empty(struct date *l); int list_length(struct date *l); void disp_list(struct date *l); int get_elem(struct date *l, int i, char *c); int locate_elem(struct date *l, char c); int list_insert(struct date *l, int i, char c); int list_delete(struct date *l, int i, char *c); void create_list(struct date *l, char str[], int n) { int i; l = (struct date *) malloc(sizeof(struct date)); for (i = 0; i < n ;i++) l -> data[i] = str[i]; l -> length = n; } void init_list(struct date *l) { l = (struct date *) malloc(sizeof(struct date)); l -> length = 0; } void destroy_list(struct date *l) { free(l); } int list_empty(struct date *l) { return (l -> length == 0); } int list_length(struct date *l) { return l -> length; } void disp_list(struct date *l) { int i; if (!list_empty(l)) return; for (i = 0; i < l -> length; i++) printf(" %c", l -> data[i]); printf("\n"); } int get_elem(struct date *l, int i, char *c) { if (i < 1 || i > l -> length) return 0; c = l -> data[i-1]; return 1; } int locate_elem(struct date *l, char c) { int i = 0; while (i < l -> length && l -> data[i] != c) i++; if (i >= l -> length) return 0; else return i + 1; } int list_insert(struct date *l, int i, char c) { int j; if (i < 1 || i > l -> length + 1) return 0; i--; for (j = l -> length; j > i; j--) l -> data[j] = l -> data[j-1]; l -> data[i] = c; l -> length++; return 1; } int list_delete(struct date *l, int i, char *c) { int j; if (i < 1 || i > l -> length) return 0; i--; c = l -> data[i]; for (j = i; j < l -> length - 1; j++) l -> data[j] = l -> data[j+1]; l -> length--; return 1; } int main(void) { struct date info; char str[MAXSIZE]; fgets(str, MAXSIZE, stdin); create_list(&info, str, MAXSIZE); disp_list(&info); return 0; }
测试了一下程序。。。就出问题了,create_list没有改变结构体内容,为什么呢?