结构体数组

结构体数组

  结构体数组相对于顺序表的优势在于可以用每个结点存储每个个体的信息,相对于链表的优势在于操作简便;结构体数组的劣势在于插入、删除操作占用资源过多,并且有时候无法分配足够的连续空间。
  

结构体数组的实现及操作

以学生信息管理为例
类型定义

#define LIST_LEN 1000
typedef struct{
    char num[10];       //学号
    char name[10];      //姓名
    int rank;           //名次
}Student;
Student SaList[LIST_LEN];

结构体数组的查找、插入、删除、排序

#include <stdio.h>
#include <stdlib.h>

#define LIST_LEN 1000
typedef struct{
    char num[10];       //学号
    char name[10];      //姓名
    int rank;           //名次
}Student;
Student SaList[LIST_LEN];

void print_Sa(Student* L, int n);
int cmp(const void *a ,const void *b);
int Locate_Sa(Student* L, int n, char* num);
void Insert_Sa(Student* L, int n, int locate);
void Delet_Sa(Student* L, int n, int locate);

int main(void)
{
    int i,j,k,n;
    printf("请输入学生人数: ");
    scanf("%d",&n);
    getchar();

    //学生信息的录入
    printf("请录入学生信息:\n");
    for(i = 0; i < n; i++){
        printf("\n第%d个学生:\n",i+1);
        printf("学号: ");
        gets(SaList[i].num);
        printf("姓名: ");
        gets(SaList[i].name);
        printf("名次: ");
        scanf("%d",&SaList[i].rank);
        getchar();
    }

    printf("\n--------------------------\n按名次排序为:\n");
    //按学生的名次升序排序
    qsort(SaList, n, sizeof(SaList[0]), cmp);
    print_Sa(SaList,n);

    //根据学号查找学生,并返回序号
    printf("\n--------------------------\n请输入要查找的学生的学号: ");
    char findnum[10];
    int locate;
    gets(findnum);
    locate = Locate_Sa(SaList, n, findnum);
    printf("学号为%s的学生的序号为%d\n", findnum, locate);

    //在某个学生前插入一个学生的信息
    printf("\n--------------------------\n请输入在哪个学生前插入(输入学号): ");
    gets(findnum);
    locate = Locate_Sa(SaList, n, findnum);
    Insert_Sa(SaList, n, locate);
    n++;
    printf("\n--------------------------\n现在列表数据为:\n");
    print_Sa(SaList, n);

    //删除某个学生的信息
    printf("\n--------------------------\n请输入要删除的学生的学号: ");
    gets(findnum);
    locate = Locate_Sa(SaList, n, findnum);
    Delet_Sa(SaList, n, locate);
    n--;
    printf("\n--------------------------\n现在列表数据为:\n");
    print_Sa(SaList, n);
    return 0;
}

int Locate_Sa(Student* L, int n, char* num)
{
    int i;
    for(i = 0; i < n; i++)
        if(!strcmp(num, L[i].num))
            return ++i;
}

void Insert_Sa(Student* L, int n, int locate)
{
    int i;
    char num[10],name[10];
    int rank;
    for(i = n-1; i >= locate-1; i--)
        L[i+1] = L[i];
    printf("请输入要插入学生的学号:");
    gets(num);
    printf("请输入要插入学生的姓名:");
    gets(name);
    printf("请输入要插入学生的名次:");
    scanf("%d",&rank);
    getchar();
    strcpy(L[locate-1].num, num);
    strcpy(L[locate-1].name, name);
    L[locate-1].rank = rank;
}

void Delet_Sa(Student* L, int n, int locate)
{
    int i;
    for(i = locate; i < n; i++)
        L[i-1] = L[i];
}

int cmp( const void *a ,const void *b)
{
    return (*(Student *)a).rank > (*(Student *)b).rank ? 1 : -1;
}

void print_Sa(Student* L, int n)
{
    int i;
    for(i = 0; i < n; i++){
        printf("学号: ");
        puts(L[i].num);
        printf("姓名: ");
        puts(L[i].name);
        printf("名次: ");
        printf("%d\n\n",L[i].rank);
    }
}

运行结果

运行结果
运行结果
运行结果
运行结果
运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值