我也来一段无聊的代码:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define SIZE (sizeof(NODE))
typedef struct node {
char ch;
struct node *next;
} NODE, *PN;
void prtList(PN head) {
PN p = NULL;
if(head != NULL) {
for(p = head; p != NULL; p = p->next)
putchar(p->ch);
puts("");
} else puts("链表为空");
}
PN addToListHead(PN head, char ch) {
PN pnew = (PN)malloc(SIZE);
if(pnew == NULL) {
puts("错误: 头部插入节点时开辟内存失败...");
exit(EXIT_FAILURE);
}
pnew->ch = ch;
pnew->next = head;
return pnew;
}
PN createList(void) {
PN head = NULL;
char ch;
while((ch = getchar()) != '\n')
head = addToListHead(head, ch);
return head;
}
void freeList(PN head) {
PN p;
while(head != NULL) {
p = head->next;
free(head);
head = p;
}
}
int main(void) {
PN head = createList();
prtList(head);
freeList(head);
return 0;
}