Home  >  Article  >  What is the use of linear tables?

What is the use of linear tables?

coldplay.xixi
coldplay.xixiOriginal
2020-06-29 09:58:303386browse

线性表有的用法:1、【StuData *elem】为指向动态分配的内存的首地址;2、【int length】为保存已存储的数据据元素的数目;3、【void welcome int】为输出欢迎界面,并提示用户执行相应的操作。

What is the use of linear tables?

线性表有的用法:

用顺序表举例说明

要求:

定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:

(1) 根据指定学生个数,逐个输入学生信息;

(2) 逐个显示学生表中所有学生的相关信息;

(3) 根据姓名进行查找,返回此学生的学号和成绩;

(4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);

(5) 给定一个学生信息,插入到表中指定的位置;

(6) 删除指定位置的学生记录;

(7) 统计表中学生个数。

typedef struct
{
    char stuID[ID_SIZE];        //学生学号
    char stuName[NAME_SIZE];    //学生姓名 
    double stuScore;        //学生成绩 
} StuData;
typedef  struct {
 Student  *elem;     //指向数据元素的基地址
  int  length;       //线性表的当前长度                                                           
  }SqList;
/*
 * 顺序表
 * 一个简陋的学生信息管理程序
 * Data: 10/13/2017  20:42
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <errno.h>
#include <inttypes.h>
#define STU_NUM_MAX 100
#define ID_SIZE 8
#define NAME_SIZE 20
typedef struct
{
    char stuID[ID_SIZE];        //学生学号
    char stuName[NAME_SIZE];    //学生姓名
    double stuScore;        //学生成绩
} StuData;
typedef StuData* stuPtr;
typedef struct
{
    StuData *elem;        //指向动态分配的内存的首地址
    int length;        //保存已存储的数据据元素的数目
} SqList;
void welcome(int *p_choose);
/*
 * 功能:输出欢迎界面,并提示用户执行相应的操作
 * 参数:指向choose的指针,通过指针改变变量choose的值,根据其值执行相应的操作
 * 返回值:无
 */
void InitList(SqList *p_seq);
/*
 * 功能:一次性分配所有的储存空间,初始化
 * 参数:SqList的指针
 * 返回值:无
 */
void add(SqList *p_seq);
/*
 * 功能:
 *
 *
 */
stuPtr info_input(stuPtr info);
/*
 * 功能:对数组赋值,其长度不超过len
 * 参数:stuID: 指向数组的指针   size_t
 * 返回值:传入的指针
 */
void NodeDelete(SqList *p_seq, int locate);
/*
 * 功能:删除指定序号的数据元素
 * 参数:p_seq: SqList的指针   locate: 序号(第几个)
 * 返回值:无
 */
StuData *search(stuPtr p, size_t len, char *target);
/*
 * 功能:根据指定的字符串遍历查找是否存在相应的ID or Name
 * 参数:p: 指向第一个顺序元素   len: 已存储的数据元素的长度   target: 需要查找的字符
 * 返回值:指向查找到的节点,不存在则返回NULL
 */
void print(StuData *elem, size_t len);
/*
 * 功能:打印一定长度的数据元素
 * 参数:elem: 指向某个数据元素   len: 需要打印多少个数据元素(长度)
 * 返回值:无
 */
void save(FILE *stream, stuPtr p, size_t len);
/*
 * 功能:将输入的信息保存到文件中
 * 参数:stream: 指定的文件输入流  p: 指向第一个数据元素   len: 数据元素的长度
 * 返回值:无
 */
int main(void)
{
    int choose;
    char ans = &#39;y&#39;;
    SqList L;
    InitList(&L);
    system("color 2F");
    while (1)
    {
        fflush(stdin);
        ans = &#39;y&#39;;
        welcome(&choose);
        switch (choose)
        {
            case 1:
            {
                while (ans == &#39;y&#39;)
                {
                    if (L.length >= STU_NUM_MAX)
                    {
                        printf("\a\n\tWarning: Memory is full!\n");
                        break;
                    }
                    else
                    {
                        //info_input(&info);
                        add(&L);
                        printf("\n\nAdd succeefully!               stu&#39;s num %u\n", L.length);
                        printf("Continue?[y]\n");
                        fflush(stdin);
                        ans = getchar( );
                        if (ans == &#39;\n&#39;)
                        {
                            ans = &#39;y&#39;;
                        }
                        system("cls");
                    }
                }
                break;
            }
            case 2:
            {
                int locate;
                while (ans == &#39;y&#39;)
                {
                    printf("Please enter the node number you want to delete: ");
                    scanf("%d", &locate);
                    NodeDelete(&L, locate);
                    printf("\a\n\n\t\tDelete Successfully\n");
                    printf("Continue?[y]");
                    fflush(stdin);
                    ans = getchar( );
                    if (ans == &#39;\n&#39;)
                    {
                        ans = &#39;y&#39;;
                    }
                    system("cls");
                }
                break;
            }
            case 3:
            {
                StuData *locate;
                char target[NAME_SIZE];
                while (ans == &#39;y&#39;)
                {
                    printf("Please enter the ID/Name of the student you want to find: ");
                    scanf("%s", target);
                    locate = search(L.elem, L.length, target);
                    if (locate == NULL)
                    {
                        printf("\a\n\t\tSorry! There is no such person!\n");
                    }
                    else
                    {
                        printf("\aFind successfully!\n");
                        print(locate, 1);
                    }
                    printf("Continu?[y] ");
                    fflush(stdin);
                    ans = getchar( );
                    if (ans == &#39;\n&#39;)
                    {
                        ans = &#39;y&#39;;
                    }
                    system("cls");
                }
                break;
            }
            case 4:
            {
                printf("All of the stu&#39;s info are:\n\n");
                print(L.elem, L.length);
                getchar( );
                getchar( );
                system("cls");
                break;
            }
            case 5:
            {
                FILE *stream;
                if ((stream = fopen("info.dat", "w+")) == NULL)
                {
                    perror("\a\n\n\t\tSorry: Open fail!\n");
                    break;
                }
                else
                {
                    save(stream, L.elem, L.length);
                    getchar( );
                    sleep(3);
                    fclose(stream);
                    system("cls");
                    break;
                }
            }
            case 6:
            {
                free(L.elem);
                L.elem = NULL;
                printf("\a\n\n\t\tBye Bye!\n\n");
                sleep(2);
                system("cls");
                system("color 0F");
                exit(0);
            }
            default :
            {
                printf("\a\n\tSorry! I have not develop the function what you want!\n");
                sleep(2);
                system("cls");
                break;
            }
        }
    }
    return 0;
}
void welcome(int *p_choose)
{
    printf("\n\n\n                       WELCOME\n");
    printf("------------------------------------------------------\n");
    printf("--                1.增加指定学生信息\n");
    printf("--                2.删除指定位置信息\n");
    printf("--                3.按学号或姓名查找\n");
    printf("--                4.显示所有学生信息\n");
    printf("--                5.保存\n");
    printf("--                6.退出\n");
    printf("------------------------------------------------------\n");
    printf("请输入那想要执行的操作的序号: ");
    scanf("%d", p_choose);
    system("cls");
}
void InitList(SqList *p_seq)
{
    p_seq->elem = (StuData *)malloc(STU_NUM_MAX*sizeof(StuData));
    if (p_seq->elem == NULL)
    {
        perror("\n\n\t\tError: memory may full");    //perror??????????????
        _exit(1);
    }
    else
    {
        p_seq->length = 0;
    }
}
void add(SqList *p_seq)
{
    printf("Please enter information:\n");
    while (1)
    {
        printf("ID: ");
        scanf("%s", p_seq->elem[p_seq->length].stuID);
        if (strlen(p_seq->elem[p_seq->length].stuID) >= ID_SIZE)
        {
            printf("It&#39;s too long, enter again\n");
            sleep(1);
            system("cls");
        }
        else
        {
            break;
        }
    }
    while (1)
    {
        printf("Name: ");
        scanf("%s", p_seq->elem[p_seq->length].stuName);
        if (strlen(p_seq->elem[p_seq->length].stuName) >= NAME_SIZE)
        {
            printf("It&#39;s too long, enter again\n");
            sleep(1);
            system("cls");
        }
        else
        {
            break;
        }
    }
    while (1)
    {
        printf("Score: ");
        scanf("%lf", &p_seq->elem[p_seq->length].stuScore);
        if (p_seq->elem[p_seq->length].stuScore <0 || p_seq->elem[p_seq->length].stuScore > 100)
        {
            printf("The score is percentage system\n");
            sleep(1);
            system("cls");
        }
        else
        {
            break;
        }
    }
    p_seq->length++;
}
void NodeDelete(SqList *p_seq, int locate)
{
    for (int i=locate; i<=p_seq->length; i++)
    {
        memccpy((p_seq->elem[i-1]).stuID, (p_seq->elem[i]).stuID, &#39;\0&#39;, ID_SIZE);
        memccpy((p_seq->elem[i-1]).stuName, (p_seq->elem[i]).stuName, &#39;\0&#39;, NAME_SIZE);
        (p_seq->elem[i-1]).stuScore = (p_seq->elem[i]).stuScore;
    }
    p_seq->length--;
}
stuPtr search(stuPtr p, size_t len, char *target)
{
    for (unsigned i=0; i<len; i++)
    {
        if (strcmp(p[i].stuID, target) == 0)
        {
            return (p+i);
        }
        else if (strcmp(p[i].stuName, target)== 0)
        {
            return (p+i);
        }
    }
    return NULL;
}
void print(StuData *elem, size_t len)
{
    printf("      ID                Name                Score\n");
    printf("    -----------------------------------------------\n");
    if (len <= 0)
    {
        printf("\a\n\t\tWarning: NO data exist!\n");
    }
    else
    {
        unsigned count = 0;
        for (unsigned i=0; i<len; i++)
        {
            printf("%3d-", ++count);
            printf(" %s\t\t", elem[i].stuID);
            printf(" %s\t\t", elem[i].stuName);
            printf(" %.2lf\n", elem[i].stuScore);
        }
        printf("    num: %lu\n", len);
    }
    printf("    -----------------------------------------------\n");
}
void save(FILE *stream, stuPtr p, size_t len)
{
    if (len <= 0)
    {
        printf("\a\n\n\t\tSorry: No data exist!\n");
        sleep(2);
        return;
    }
    else
    {
        unsigned count = 0;
        fprintf(stream, "      ID                  Name                 Score\n");
        fprintf(stream, "    -------------------------------------------------\n");
        for (unsigned i=0; i<len; i++)
        {
            fprintf(stream, "%3d-", ++count);
            fprintf(stream, " %s\t\t", p[i].stuID);
            fprintf(stream, " %s\t\t", p[i].stuName);
            fprintf(stream, " %.2lf\n", p[i].stuScore);
        }
        fprintf(stream,"\n    -------------------------------------------------\n");
        printf("\a\n\n\tSave successfully!\n\n");
    }
}

相关学习推荐:PHP编程从入门到精通

The above is the detailed content of What is the use of linear tables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn