guhusf 发表于 2021-4-5 23:38:56

数据结构自用

本帖最后由 guhusf 于 2021-4-6 00:12 编辑

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct
{
        int num;
        float price;
        char name;
}BIP;
typedef struct Node
{
        BIP Data;
        struct Node *Next;
}Node,*List;

InitList(List *L)
{
        (*L)= (List)malloc(sizeof(Node));
        if((*L)==NULL)return 0;
        (*L)->Next=NULL;
        return 1;
}

InsertList(List L,int i,BIP x)
{
        List p=L,s;
        int n=0;
        while(p!=NULL&&n<i-1)
        {
                p=p->Next;
                n++;
        }
        if(p==NULL||n>i-1)return 0;
        s= (List)malloc(sizeof(Node));
        s->Data=x;
        s->Next= p->Next;
        p->Next=s;
        return 1;
}

DeleteList(List L,int i,BIP *x)
{
        List p=L,s;
        int n = 0;
        while(p!=NULL&&n<i-1)
        {
                p=p->Next;
                n++;
        }
        if(p==NULL||n>i-1)
        return 0;
        s=(List)malloc(sizeof(Node));
        s=p->Next;
        p->Next=s->Next;
        *x=s->Data;
        free(s);
        return 1;
       
}

FindList(List *L,int num)
{
        int n =1;
        List p=(*L)->Next;
        while(p&&p->Data.num<num)
        {
                p=p->Next;
                n++;
        }
        return n;
}

CreateList(List *L)
{
        BIP x;
        int n,a;
        List p;
        InitList(L);
        do
        {
                printf("请输入要添加的序号,名字,价格,用空格隔开:\n");
                scanf("%d%s%f",&x.num,x.name,&x.price);
                n = FindList(L,x.num);
                InsertList(*L,n,x);
                printf("1/0");
                scanf("%d",&a);
        }while(a);
}

UpdataList(List L,int i,BIP x)
{
        List p=L->Next;
        int n=1;
        while(p&&n<i)
        {
                p=p->Next;
                n++;
        }
        if(p==NULL||n>i)return 0;
        p->Data=x;
        return 1;
}
PrintList(List L)
{
        List p=L->Next;
        if(p==NULL)return 0;
        while(p)
        {
                printf("%d %s %.2f\n",p->Data.num,p->Data.name,p->Data.price);
                p=p->Next;
        }
        return 1;
}
menu()
{
        int n;
        while(1)
        {
                system("cls");
                printf("********************************\n");
                printf("欢迎使用系统\n");
                printf("1.创建数据表**2.插入数据\n");
                printf("3.删除数据表**4.修改数据\n");
                printf("5.*输出数据**6.退出管理系统*\n");
                printf("********************************\n");
                printf("请选择功能编号(1-6):\n");
                scanf("%d",&n);
                getchar();
                if(n<0||n>6)
                {
                        printf("输入有误,重新选择,按任意键继续!\n");
                        getch();
                }
                else return n;
        }
}
main()
{
        BIP x;
        List L;
        int n,i,num;
        while(1)
        {
       
        n=menu();
        switch(n)
        {
                case 1:
                        CreateList(&L);       
                        printf("按任意键继续!\n");
                        getch();
                        break;
                case 2:
                        printf("请输入插入的序号\n");
                        scanf("%d",&x.num);
                        n=FindList(&L,x.num);
                        printf("输入 序号,名字,价格 \n");
                        scanf("%d %s%f",&x.num,x.name,&x.price);
                        InsertList(L,n,x);
               
                        getch();
                        break;
                case 3:
                        printf("输入要删除的序号\n");
                        scanf("%d",&x.num);
                        i= FindList(&L,x.num);
                        DeleteList(L,i,&x);
                        getch();
                        break;
                case 4: printf("请输入要修改的序号,名字,价格\n");
                            scanf("%d%s%f",&x.num,x.name,&x.price);
                           
                            i=FindList(&L,x.num);
                                        UpdataList(L,i,x);
                                        printf("按任意键继续!\n");
                                        getch();
                                break;
                case 5: printf("事件数据如下:\n");
                                PrintList(L);
                                printf("按任意键继续!\n");
                                getch();
                                break;
                case 6: exit(0);      
                       
               
                       
        }
}
}
页: [1]
查看完整版本: 数据结构自用