2021-01-15

C语言创建顺序表案例-简单的学生信息系统

一、头文件

#include <stdio.h>
#include <string.h>
#define MAXSIZE 100


typedef struct
{
    DATA ListData[MAXSIZE+1]; //这里考虑到人们的习惯,0位上不赋值;
    int ListLen;
}SeqListType;
void SeqListInit(SeqListType *SL);
int SeqListLength(SeqListType *SL);
int SeqListAdd(SeqListType *SL,DATA data);
int SeqListInsert(SeqListType *SL,int n,DATA data);
int SeqListDelete(SeqListType *SL,int n);
DATA *SeqListFindByNum(SeqListType *SL,int n);
int SeqListFindByCont(SeqListType *SL,char *key);
int SeqLIstAll(SeqListType *SL);

二、自定义函数

void SeqListInit(SeqListType * SL)
{
    SL->ListLen=0;
}
int SeqListLength(SeqListType *SL)
{
    return (SL->ListLen);
}
int SeqListAdd(SeqListType *SL,DATA data)
{
    if(SL->ListLen>=MAXSIZE)
    {
        printf("顺序表已满,不能再添加节点了!\n");
        return 0;
    }
    SL->ListData[++SL->ListLen]=data;
    return 1;
}
int SeqListInsert(SeqListType *SL,int n,DATA data)
{
    int i;
    if(SL->ListLen>=MAXSIZE)
    {
        printf("顺序表已满,不能插入节点!/n");
        return 0;
    }
    if(n<1 || n>SL->ListLen-1)
    {
        printf("插入节点序号错误,不能插入元素!\n");
        return 0;
    }
    for(i=SL->ListLen;i>=n;i--)
        SL->ListData[i+1]=SL->ListData[i];
    SL->ListData[n]=data;
    SL->ListLen++;
    return 1;
}
int SeqListDelete(SeqListType *SL,int n)
{
    int i;
    if(n<1 || n>SL->ListLen+1)
    {
        printf("删除节点序号错误,不能删除节点!\n");
        return 0;
    }
    for(i=n;i<SL->ListLen;i++)
        SL->ListData[i]=SL->ListData[i+1];
    SL->ListLen--;
    return 0;
}
DATA *SeqListFindByNum(SeqListType *SL,int n)
{
    if(n<1 || n>SL->ListLen+1)
    {
        printf("节点序号错误,不能返回节点!\n");
        return NULL;
    }
    return &(SL->ListData[n]);
}
int SeqListFindByCont(SeqListType *SL,char *key)
{
    int i;
    for(i=1;i<=SL->ListLen;i++)
        if(strcmp(SL->ListData[i].key,key)==0)
            return i;
    return 0;
}
/*遍历函数与定义的数据结构有关,所以可以在测试函数中进行设定,这里给出适合本例的代码*/
int SeqLIstAll(SeqListType *SL)
{
    int i;
    for(i=1;i<=SL->ListLen;i++)
        printf("(%s,%s,%d)\n",SL->ListData[i].key,SL->ListData[i].name,SL->ListData[i].age);
}

三、测试函数

#include <stdio.h>
#include <conio.h>



typedef struct
{
    char key[15];
    char name[20];
    int age;
} DATA;
#include "2-1SeqList.h"
#include "2-2Seqlist.c"


int main()
{
    int i;
    SeqListType SL;
    DATA data,*data1;
    char key[15];

    SeqListInit(&SL);

    do{
        printf("输入添加的节点(学号 姓名 年龄):");
        fflush(stdin);      //清空缓冲区
        scanf("%s%s%d",&data.key,&data.name,&data.age);
        if(data.age)
        {
            if(!SeqListAdd(&SL,data))
                break;
        }else
            break;
    }while(1);
    printf("\n顺序表中的节点顺序为:");
    SeqLIstAll(&SL);

    fflush(stdin);
    printf("\n要取出的节点序号:");
    scanf("%d",&i);
    data1=SeqListFindByNum(&SL,i);
    if(data1)
        printf("第%d个节点为:(%s%s%d)\n,i,data1->key,data1-name,data1->age");

    fflush(stdin);
    printf("\n要查找的关键字:");
    scanf("%s",key);
    i=SeqListFindByCont(&SL,key);
    data1=SeqListFindByNum(&SL,i);
    if(data1)
        printf("第%d个节点为:(%s%s%d)\n,i,data1->key,data1-name,data1->age");
    getch();
    return 0;
}

四、结语,花费4个小时复习一下顺序表,不过代码出了点小问题,有兴趣的码友可以运行,找找错,告诉本宝!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值