#include <stdio.h> #include <malloc.h> #define GetNode(type) ((type * )malloc(sizeof(type))) typedef struct node { char date; struct node * next; } NodeType; NodeType * Create() /*根据输入建立带头节点的单链表* / { char c; NodeType * head,* p, * r; head=GetNode(NodeType); /*建立头节点*/ r=head; /*r始终指向尾节点*/ printf("输入一行字符:"); while ((c=getchar())!='\n') { p=GetNode(NodeType); /*建立一个节点*/ p->date=c; r->next=p; r=p; /*r始终指向尾节点*/ } r->next=NULL; return(head); } void Output(NodeType * head) /*输出并释放单链表*/ { NodeType * p=head, * q=p->next; printf("执行结果:"); while (q!=NULL) { printf("%c",q->date); free(p); p=q; q=p->next; } free(p); printf("\n"); } void main() { NodeType * h; h=Create(); Output(h); }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题