编写一个程序,读入一行字符,且每个字符存入一个结点,按输入顺序建立一个链表的结点序列,然后再按相反顺序输出并释放全部结点。
#include <stdio.h>
#include <stdlib.h>
void main(){
struct node {
char info;
struct node *link;
}*top,*p;
char c;
printf("input a string:");
top=NULL;
while ((c=getchar())!='\n'){
p=(struct node *)malloc(sizeof(struct node));
p->info=c;
p->link=top;
top=p;
}
printf("input:");
while (top!=NULL){
p=top;
top=p->link;
printf("%c",p->info);
free(p);
}
printf("\n");
}
编写一个函数,删除 data 成员等于x的所有结点
答:从头开始扫描单链表,q 始终指向当前结点*p的前一个结点,当P所指结点满足条件时,使用语句
q->next-p->next删除P所指结点。函数如下: