#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
struct Node;
typedef struct Node *PNode;
typedef struct Node *LinkList;
struct Node {
DataType info;
PNode link;
};
LinkList createNullList_link(void) {
LinkList llist;
/*申请表头结点空间*/
llist = malloc(sizeof(*llist));
if(llist != NULL) llist->link = NULL;
else printf("Out of space!\n"); /*创建失败*/
return llist;
}
int insertPost_link(LinkList llist, PNode p, DataType x) {
PNode q = malloc(sizeof(*q)); /*申请新结点*/
if(q == NULL) {
printf("Out of Space!\n");
return 0;
} else {
q->info = x;
//q->link = p->link; // 谜之操作
q->link = NULL;
p->link = q;
return 1;
}
}
#if 0
int deleteV_link(LinkList llist, DataType x) {
PNode p, q;
p = llist;
if(p == NULL)
return 0;
while(p->link != NULL && p->link->info != x)
p = p->link; /*找值为x的结点的前驱结点的存储位置*/
if(p->link == NULL) { /* 没找到值为x的结点*/
printf("Not exist!\n ");
return 0;
} else {
q = p->link; /* 找到值为x的结点*/
p->link = q->link; /* 删除该结点*/
free(q);
return 1;
}
}
#endif
// 你是不是忘记写这个函数了?
void free_link(LinkList list) {
if(!list) return;
free_link(list->link);
free(list);
}
int main(void) {
LinkList m = createNullList_link();
//PNode a = malloc(sizeof(*a));
//m = a; // 谜之操作
PNode a = m;
insertPost_link(m, a, 1);
PNode temp = m->link;
while(temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
free_link(m);
puts("");
return 0;
}
|