/**@Author:suifengtec*@Date:2017-09-0216:06:33*@LastModifiedby:suifengtec*@LastModifiedtime:2017-09-0220:47:13**//*字符单向链表gcc-oa.exemain.c&&a*/#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//声明一个自包含结构体listNode,并把它定义为类型ListNodetypedefstructlistNode{chardata;structlistNode*nextPtr;}ListNode;//为ListNode的指针定义一个类型ListNodePtrtypedefListNode*ListNodePtr;//引导说明voidbootstrap(void);//插入到链表,这里按照字符的ASCII码插入//参数分别为当前节点的指针和元素的值voidinsert(ListNodePtr*sPtr,charvalue);//链表是否为空boolisEmpty(ListNodePtrsPtr);//从链表中删除特定的元素值,返回是否删除成功的布尔值//每次只删除1个,即使链表中有两个AA,在value为A时,也只删除第一个booldelete(ListNodePtr*sPtr,charvalue);//打印链表voidprintList(ListNodePtrcurrentPtr);intmain(void){//起始指针/当前指针ListNodePtrstartPtr=NULL;//在引导说明中已经说明了,只能取1,2,3中的一个值,其它的整数将会被认为是字符unsignedintchoice;//字符charitem;//引导说明bootstrap();//获取用户输入的1或2或3或者字符,或者无空格的不含1和2和3的字符串printf("%s","?");scanf("%u",&choice);//不为3时while(choice!=3){switch(choice){//1表示向链表中插入字符case1:/*每次输入一个字符还支持输入一个不含空格的字符串如HelloChina!链表为!-->C-->H-->a-->h-->i-->i-->NULL*/printf("%s","Enteracharacter:");scanf("\n%c",&item);insert(&startPtr,item);printList(startPtr);break;//2表示从链表中删除字符,链表为空或者没有要删除的字符时,给出错误提示。//如果这个字符串中的字符全都存在于链表的话//也可以一次性删除输入的不含1和2和3的不含空格的字符串,case2:if(!isEmpty(startPtr)){printf("%s","Enteracharactertobedeleted:");scanf("\n%c",&item);if(delete(&startPtr,item)){printf("\'%c\'hasbedeleted.\n",item);printList(startPtr);}else{printf("notfound\'%c\'\n",item);}}else{puts("thelistisempty?");}break;//后备,因为不可能到这里的case3:puts("invalidoption!");bootstrap();break;}/*bootstrap();*/printf("%s","?");scanf("%u",&choice);}return0;}voidbootstrap(void){printf("Enteryourchoice:\n""1=insertanelementtothelist\n""2=deleteanelementfromthelist\n""3=GameOver.\n");}voidinsert(ListNodePtr*sPtr,charvalue){ListNodePtrnewPtr=malloc(sizeof(ListNode));if(newPtr!=NULL){newPtr->data=value;newPtr->nextPtr=NULL;ListNodePtrprePtr=NULL;ListNodePtrcurrentPtr=*sPtr;while(currentPtr!=NULL&&value>currentPtr->data){prePtr=currentPtr;currentPtr=currentPtr->nextPtr;}if(prePtr==NULL){newPtr->nextPtr=*sPtr;*sPtr=newPtr;}else{prePtr->nextPtr=newPtr;newPtr->nextPtr=currentPtr;}}else{printf("%cnotinserted.Nomemoryavailabe.\n",value);}}booldelete(ListNodePtr*sPtr,charvalue){if(value==(*sPtr)->data){ListNodePtrtmpPtr=*sPtr;*sPtr=(*sPtr)->nextPtr;free(tmpPtr);returntrue;}else{ListNodePtrprePtr=*sPtr;ListNodePtrcurrentPtr=(*sPtr)->nextPtr;while(currentPtr!=NULL&&currentPtr->data!=value){prePtr=currentPtr;currentPtr=currentPtr->nextPtr;}if(currentPtr!=NULL){ListNodePtrtmpPtr=currentPtr;prePtr->nextPtr=currentPtr->nextPtr;free(tmpPtr);returntrue;}}returnfalse;}boolisEmpty(ListNodePtrsPtr){returnsPtr==NULL?true:false;}voidprintList(ListNodePtrcurrentPtr){if(isEmpty(currentPtr)){puts("Listisempty?");}else{puts("Thelistis:\n");while(currentPtr->nextPtr!=NULL){printf("%c-->",currentPtr->data);currentPtr=currentPtr->nextPtr;}puts("NULL\n");}}/*EOF*/

单向字符链表的C语言实现。