鱼C论坛

 找回密码
 立即注册
查看: 2633|回复: 0

[学习笔记] 数据结构自用

[复制链接]
发表于 2021-4-5 23:38:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 guhusf 于 2021-4-6 00:12 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<conio.h>
  5. typedef struct
  6. {
  7.         int num;
  8.         float price;
  9.         char name[20];
  10. }BIP;
  11. typedef struct Node
  12. {
  13.         BIP Data;
  14.         struct Node *Next;
  15. }Node,*List;

  16. InitList(List *L)
  17. {
  18.         (*L)= (List)malloc(sizeof(Node));
  19.         if((*L)==NULL)return 0;
  20.         (*L)->Next=NULL;
  21.         return 1;
  22. }

  23. InsertList(List L,int i,BIP x)
  24. {
  25.         List p=L,s;
  26.         int n=0;
  27.         while(p!=NULL&&n<i-1)
  28.         {
  29.                 p=p->Next;
  30.                 n++;
  31.         }
  32.         if(p==NULL||n>i-1)return 0;
  33.         s= (List)malloc(sizeof(Node));
  34.         s->Data=x;
  35.         s->Next= p->Next;
  36.         p->Next=s;
  37.         return 1;
  38. }

  39. DeleteList(List L,int i,BIP *x)
  40. {
  41.         List p=L,s;
  42.         int n = 0;
  43.         while(p!=NULL&&n<i-1)
  44.         {
  45.                 p=p->Next;
  46.                 n++;
  47.         }
  48.         if(p==NULL||n>i-1)
  49.         return 0;
  50.         s=(List)malloc(sizeof(Node));
  51.         s=p->Next;
  52.         p->Next=s->Next;
  53.         *x=s->Data;
  54.         free(s);
  55.         return 1;
  56.        
  57. }

  58. FindList(List *L,int num)
  59. {
  60.         int n =1;
  61.         List p=(*L)->Next;
  62.         while(p&&p->Data.num<num)
  63.         {
  64.                 p=p->Next;
  65.                 n++;
  66.         }
  67.         return n;
  68. }

  69. CreateList(List *L)
  70. {
  71.         BIP x;
  72.         int n,a;
  73.         List p;
  74.         InitList(L);
  75.         do
  76.         {
  77.                 printf("请输入要添加的序号,名字,价格,用空格隔开:\n");
  78.                 scanf("%d%s%f",&x.num,x.name,&x.price);
  79.                 n = FindList(L,x.num);
  80.                 InsertList(*L,n,x);
  81.                 printf("1/0");
  82.                 scanf("%d",&a);
  83.         }while(a);
  84. }

  85. UpdataList(List L,int i,BIP x)
  86. {
  87.         List p=L->Next;
  88.         int n=1;
  89.         while(p&&n<i)
  90.         {
  91.                 p=p->Next;
  92.                 n++;
  93.         }
  94.         if(p==NULL||n>i)return 0;
  95.         p->Data=x;
  96.         return 1;
  97. }
  98. PrintList(List L)
  99. {
  100.         List p=L->Next;
  101.         if(p==NULL)return 0;
  102.         while(p)
  103.         {
  104.                 printf("%d %s %.2f\n",p->Data.num,p->Data.name,p->Data.price);
  105.                 p=p->Next;
  106.         }
  107.         return 1;
  108. }
  109. menu()
  110. {
  111.         int n;
  112.         while(1)
  113.         {
  114.                 system("cls");
  115.                 printf("********************************\n");
  116.                 printf("欢迎使用系统\n");
  117.                 printf("1.创建数据表**2.插入数据\n");
  118.                 printf("3.删除数据表**4.修改数据\n");
  119.                 printf("5.*输出数据**6.退出管理系统*\n");
  120.                 printf("********************************\n");
  121.                 printf("请选择功能编号(1-6):\n");
  122.                 scanf("%d",&n);
  123.                 getchar();
  124.                 if(n<0||n>6)
  125.                 {
  126.                         printf("输入有误,重新选择,按任意键继续!\n");
  127.                         getch();
  128.                 }
  129.                 else return n;
  130.         }
  131. }
  132. main()
  133. {
  134.         BIP x;
  135.         List L;
  136.         int n,i,num;
  137.         while(1)
  138.         {
  139.        
  140.         n=menu();
  141.         switch(n)
  142.         {
  143.                 case 1:
  144.                         CreateList(&L);       
  145.                         printf("按任意键继续!\n");
  146.                         getch();
  147.                         break;
  148.                 case 2:
  149.                         printf("请输入插入的序号\n");
  150.                         scanf("%d",&x.num);
  151.                         n=FindList(&L,x.num);
  152.                         printf("输入 序号,名字,价格 \n");
  153.                         scanf("%d %s%f",&x.num,x.name,&x.price);
  154.                         InsertList(L,n,x);
  155.                
  156.                         getch();
  157.                         break;
  158.                 case 3:
  159.                         printf("输入要删除的序号\n");
  160.                         scanf("%d",&x.num);
  161.                         i= FindList(&L,x.num);
  162.                         DeleteList(L,i,&x);
  163.                         getch();
  164.                         break;
  165.                 case 4: printf("请输入要修改的序号,名字,价格\n");
  166.                             scanf("%d%s%f",&x.num,x.name,&x.price);
  167.                            
  168.                             i=FindList(&L,x.num);
  169.                                         UpdataList(L,i,x);
  170.                                         printf("按任意键继续!\n");
  171.                                         getch();
  172.                                 break;
  173.                 case 5: printf("事件数据如下:\n");
  174.                                 PrintList(L);
  175.                                 printf("按任意键继续!\n");
  176.                                 getch();
  177.                                 break;
  178.                 case 6: exit(0);        
  179.                        
  180.                
  181.                        
  182.         }
  183. }
  184. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 17:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表