//創建一個鏈表,該鏈表可以存放從鍵盤輸入的任意長度的字符串,以按下回車鍵作為輸入的結束。統計輸入的字符個數並將其字符串輸出。 #include<stdio.h>
#include<stdlib.h>
struct symbol{
char sym;
struct symbol *nextPtr;
};
struct symbol *createsym(struct symbol *p);
void listsym(struct symbol *p);
int main(){
struct symbol *head;
head=NULL;
head=createsym(head);
listsym(head);
return 0;
}
struct symbol *createsym(struct symbol *p){
//p=(struct symbol *)malloc(sizeof(struct symbol));
struct symbol *p1,*p2;
p1=p2=(struct symbol*)malloc(sizeof(struct symbol));
printf("請輸入字符串以enter結束:\n");
scanf("%c",&p1->sym);
p1->nextPtr=NULL;
p=p1;
while(p1->sym!='\n'){
p2=p1;
p1=(struct symbol *)malloc(sizeof(struct symbol));
scanf("%c",&p1->sym);
p1->nextPtr=NULL;
p2->nextPtr=p1;
}
printf("輸入完成!\n");
return p;
}
void listsym(struct symbol *p){
if(!p){
printf("字母鏈表為空!\n");
}else{
printf("字母序列為:\n");
while(p){
printf("%c",p->sym);
p=p->nextPtr;
}
}
}