链表单词字典顺序c语言,各位高手,HELP!!!!关于字典的编程

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

各位高手,帮帮我看看我这个程序哪里错了?快!先谢谢了!!!!!!!

#include "stdlib.h"

#define NULL 0

#define LEN sizeof(struct word)

#include "stdio.h"

struct word

{char w[20];

struct word *next;

};

int n;

struct word *create(void)

{struct word *head;

struct word *p1,*p2;

char wo[20];

n=0;

FILE *fp;

if((fp=fopen("diction.txt","r"))==NULL)

{printf("cannot open this file.\n");

exit(0);

}

fscanf(fp,"%s",wo);

if(wo==NULL)head=NULL;

else

p1=p2=(struct word*)malloc(LEN);

p1->w=wo;

head=NULL;

while(p1->w!=0)

{ n=n+1;

if(n==1)head=p1;

else p2->next=p1;

p2=p1;

p1=(struct word *)malloc(LEN);

fscanf(fp,"%s",wo);

p1->w=wo;

}

p2->next=NULL;

fclose(fp);

return(head);

}

struct word *insert_a_word(struct word *head,char *wo)

{struct word *p0,*p1,*p2;

p1=head;

p0=(struct word*)wo;

if(head==NULL)

{head=p0;p0->next=NULL;}

else

{while((strcmp(p0->w,p1->w)>0)&&(p1->next!=NULL))

{p2=p1;p1=p1->next;}

if(strcmp(p0->w,p1->w)<=0)

{if(head==p1){head=p0;p0->next=p1;}

else

p2->next=p0;

p0->next=p1;}

else

{p1->next=p0;p0->next=NULL;}}

n=n+1;

return(head);

}

struct word *insert(struct word *head,char *wo)

{insert(head,wo);

printf("you succeed in inserting the word:%s",wo);

system("pause");

return(head);

}

insert_word()

{struct word *head;

char wo[20];

printf("Please input a new word:" );

scanf("%s",wo);

head=insert(head,wo);

return(head);

}

struct word *insert_words(struct word *head,char *filename)

{struct word *p;

char wo[20];

FILE *fp;

if((fp=fopen(filename,"r"))==NULL)

{printf("cannot open this file.\n");

exit(0);}

p=head;

do

{fscanf(fp,"%s",wo);

n=n+1;

printf("%s  ",wo);

p->next=p;

}while(p!=NULL);

}

insert_from_a_text_file()

{char filename[10];

printf("Enter the file name:");

scanf("%s",filename);

insert_words(head,filename);

}

struct word *del(struct word *head, char *wo)

{struct word *p1, *p2;

if (head==NULL){printf("\nlist null! \n");goto end;}

p1=head;

while(wo!=p1->w&&p1->next!==NULL)

{p2=p1;p1=p1->next;}

if(wo==p1->w)

{if(p1==head)head=p1->next;

else p2->next=p1->next;

printf("delete:%s\n",wo);

n=n-1;

}

else printf("%s not been found! \n",wo);

return(head);

system("pause");

}

delete_word()

{struct word *head;

char wo[20];

printf("Please input a deletion word:" );

scanf("%s",wo);

head=del(head,wo);

return(head);

}

struct word *delall(struct word *head)

{struct word *p,*q;

p=head;

q=p->next;

while(q!=NULL)

{free(p);

p=q;

q=q->next;

}

free(q);

}

delete_the_words()

{delall(head);

}

void list(struct word *head)

{ struct word *p;

p=head;

printf("All the word:\n");

if(head!=NULL)

do

{printf("%s\n",p->w);

p=p->next;

}while(p!=NULL);

printf("Press any key to continue.........");

system("pause");

}

list_all_words()

{struct word *head;

if(head==NULL)

printf("Dictionary is null.");

else

list(head);

}

void search(struct word *head, char *wo)

{struct word *p1,*p2;

if (head==NULL){printf("\nNo any words!  \n");

goto end;}

p1=head;

while ((strcmp(wo,p1->w)!=0)&&p1->next!==NULL)

{p2=p1;p1=p1->next;}

if(strcmp(wo,p1->w)==0)

{printf("search the word:%s\n",wo);}

else printf("%s not been found!\n",wo);

}

search_word()

{struct word *head;

char wo[20];

printf("Please input a search word:");

scanf("%s",wo);

search(head,wo);

}

void mainmenu()

{int choice;

struct word *head;

head=create();

do

{ menu_f();

scanf("%d",&choice);

getchar();

switch(choice)

{case 1:insert_word();break;

case 2:insert_from_a_text_file();break;

case 3:delete_word();break;

case 4:search_word();break;

case 5:list_all_words();break;

case 6:delete_all_words();break;

case 0:write_to_file();return;

default:printf("\n\t Input Error!Enter your choice again!\n");

printf("\n\t");

system("pause");

}

}while(choice!=0);

}

void menu_f()

{system("cls");

printf("\n");

printf("\n\t********** Dictionary management system *********");

printf("\n\t  please give menu choice:");

printf("\n\t   1.insert word:");

printf("\n\t   2.insert words from a text file:");

printf("\n\t   3.delete word:");

printf("\n\t   4.serach word:");

printf("\n\t   5.list all words:");

printf("\n\t   6.delete all words:");

printf("\n\t   0.exit");

printf("\n\tchoice:");

}

main()

{

menu_f();

mainmenu();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值