當前位置:編程學習大全網 - 編程語言 - C編程, 在list中,按英文字母順序排列,和書名變大草(急!)

C編程, 在list中,按英文字母順序排列,和書名變大草(急!)

#include?<stdio.h>

#include?<ctype.h>

#include?<stdlib.h>

#include?<string.h>

typedef?struct?record{

char?title[50];

char?author[20];

double?price;

int?quantity;

struct?record?*next;

}Node,*LinkList;

LinkList?InitList()?{

Node?*head?=?(Node?*)malloc(sizeof(Node));

head->title[0]?=?'\0';

head->author[0]?=?'\0';

head->price?=?0.0;

head->quantity?=?0;

head->next?=?NULL;

return?head;

}

void?Add(LinkList?head)?{?//?頭插法

Node?*p?=?(Node?*)malloc(sizeof(Node));

fflush(stdin);

printf("書名?:?");

gets(p->title);

printf("作者?:?");

gets(p->author);

printf("單價?:?");

scanf("%lf",&p->price);

printf("數量?:?");

scanf("%d",&p->quantity);

p->next?=?head->next;

head->next?=?p;

}

void?PrintBookList(LinkList?head)?{

Node?*p?=?head->next;

while(p)?{

printf("書名?:?%s\n",p->title);

printf("作者?:?%s\n",p->author);

printf("單價?:?%.2lf元\n",p->price);

printf("數量?:?%d本\n\n",p->quantity);

p?=?p->next;

}

system("pause");

}

void?Sort(LinkList?head)?{?//?選擇排序

Node?*pt,*qt,*q,*p;

for(p?=?head;?p->next;?p?=?p->next)?{

qt?=?p;

for(q?=?p->next;?q->next;?q?=?q->next)

if(strcmp(qt->next->title,q->next->title)?>?0)

qt?=?q;

if(p?!=?qt)?{?//?調整節點位置

pt?=?p->next;

p->next?=?qt->next;

qt->next?=?qt->next->next;

p->next->next?=?pt;

}

}

}

int?Delete(LinkList?head)?{

Node?*p,*q;

char?title[50];

printf("請輸入欲刪除圖書名稱?:?");

gets(title);

for(p?=?head;?p->next;?p?=?p->next)?{

if(strcmp(p->next->title,title)?==?0)?{

q?=?p->next;

p->next?=?q->next;

free(q);

printf("圖書《%s》已被成功刪除。\n",title);

return?1;

}

}

printf("對不起,沒有找到書名為《%s》的圖書!\n",title);

return?0;

}

void?Modufy(LinkList?head)?{

Node?*p?=?head->next;

char?title[50];

int?an,flag;

do?{

printf("欲修改圖書書名?:?");

fflush(stdin);

gets(title);

flag?=?1;

while(p)?{

if(strcmp(title,p->title)?==?0)?{

printf("書名?:?");

gets(p->title);

printf("作者?:?");

gets(p->author);

printf("單價?:?");

scanf("%lf",&p->title);

printf("數量?:?");

scanf("%d",&p->quantity);

flag?=?0;

break;

}

p?=?p->next;

}

if(flag)?printf("沒有找到書名為《%s》的圖書。\n",title);

else?printf("修改成功。\n\n");

printf("1、繼續修改?0、返回主菜單\n");

scanf("%d",&an);

}while(an?!=?0);

}

void?FreeList(LinkList?head)?{

Node?*q,*p?=?head;

while(p)?{

q?=?p->next;

free(p);

p?=?q;

}

}

void?Menu(void)?{

system("cls");

printf("**************************************************\n");

printf("*?1、添加?2、顯示?*\n");

printf("*?3、排序?4、刪除?*\n");

printf("*?5、修改?6、退出?*\n");

printf("**************************************************\n\n");

}

int?main()?{

int?choice;

LinkList?head?=?InitList();

do?{

Menu();

printf("Enter?your?choice?:?");

scanf("%d",&choice);

switch(choice)?{

case?1?:?Add(head);?break;

case?2?:?PrintBookList(head);?break;

case?3?:?Sort(head);?break;

case?4?:?Delete(head);?break;

case?5?:?Modufy(head);?break;

default?:?break;

}

}while(choice?!=?6);

FreeList(head);

printf("End!\n");

return?0;

}

  • 上一篇:關於通過java實現圖片格式的轉換
  • 下一篇:2022世界人工智能大會活動看點匯總
  • copyright 2024編程學習大全網