Data structure Yan Weimin C language version-linear table sequential storage structure (sequence table) C language implementation related code

1. Operating environment

Here to explain that all the C language codes here are compiled and run based on code::blocks 20.03. Of course, some other integrated development environments should also be possible. I don't like IDEs that are too powerful, because that also means related settings and the complexity of use is greatly improved. Just enough.
In addition, I will mention a more common problem. The error is as follows:
Insert picture description hereThe reason for this is because the C files and header files are not linked together. The solution will be mentioned later.

2. Preparation

1) Project construction

1>Create a new SeqList project

Click File–>New–>Project…–>Console application–>Go, as shown in the figure below, the rest are default, the project name SeqList (sequence list).
Insert picture description here

2>Create two new files Sources and Headers

Create two new files, Sources and Headers
, to store source files and header files respectively, which is conducive to code encapsulation and reuse, and develops good habits.
Insert picture description here

3>Create two C/C++ sources and a C/C++ header

As shown below

Insert picture description here
Two C files, main.c and SeqList.c, are newly created under the source file, and the SeqList.h header file is newly created under the header file. Regarding the naming, considering that everyone should have a certain C language foundation, I will not repeat them here.
Finally the project is built, as shown in the figure below
Insert picture description here

2) Solve the problem at the beginning

The problem at the beginning here is because the three files are not linked together.
1. Right-click the project SeqList -> properties... Open the following figure
Insert picture description here
2. Select Build targets, select three files as shown below and
Insert picture description here
click OK. Closer to home, code first.

3. Implement the source code

1) main.c

#include"SeqList.h"

void main()
{
    SeqList mylist;
    InitSeqList(&mylist);

    ElemType Item;  ///ElemType类型数据,用来传递参数
    int pos;  ///位置下标

    int select ;  //选择参数0~14
    while(select)
    {
        printf("*************************************\n");
        ///       [1]尾部插入        [2]头部插入
        printf("* [1]  push_back    [2] push_font   *\n");
        ///       [3] 显示线性表     [4] 尾部删除
        printf("* [3]  show_list    [4] pop_back    *\n");
        ///       [4]头部删除        [6] 按位置插入
        printf("* [5]  pop_front    [6] insert_pos  *\n");
        ///       [7] 查找           [8] 返回顺序表的长度
        printf("* [7]  find         [8] length      *\n");
        ///       [9] 按位置删除     [10] 按值删除
        printf("* [9]  delete_pos   [10] delete_val *\n");
        ///       [11] 排序          [12] 逆置
        printf("* [11]  sort        [12] resver     *\n");
        ///       [13] 清除          [14] 销毁
        printf("* [13]  clear       [14] destry     *\n");
        printf("* [0]  quit_system                  *\n");  ///按0退出
        printf("*************************************4\n");
       
        printf("请选择:>");
        scanf("%d", &select);
        if(select == 0)
            break;
        switch(select)
        {
        case 1:
            printf("请输入要从尾部插入的数据:>(-1结束)");
            while(scanf("%d",&Item), Item!=-1)
            {
                push_back(&mylist,Item);
            }
            printf("\n");
            break;
        case 2:
            printf("请输入要从头部插入的数据:>(-1结束)");
            while(scanf("%d",&Item), Item!=-1)
            {
                push_front(&mylist,Item);
            }
            printf("\n");
            break;
        case 3:
            show_list(&mylist);
            break;
        case 4:
            pop_back(&mylist);
            break;
        case 5:
            pop_front(&mylist);
            break;
        case 6:
            printf("请输入要插入的数据(按位置插入):>");
            scanf("%d",&Item);
            printf("请输入要插入的下标位置(从0开始):>");
            scanf("%d",&pos);
            insert_pos(&mylist,Item,pos);
            break;
        case 7:
            printf("请输入要查找的数据:>");
            scanf("%d",&Item);
            pos = find(&mylist,Item);
            if(pos == -1)
                printf("要查找的数据 %d 在顺序表里不存在.\n\n",Item);
            else
                printf("要查找的数据 %d 在顺序表中下标位置为 %d.\n\n",Item,pos);

            break;
        case 8:
            printf("顺序表的长度为 %d\n\n",length(&mylist));
            break;
        case 9:
            printf("请输入要删除数据的下标位置:>");
            scanf("%d",&pos);

            delete_pos(&mylist,pos);
            printf("顺序表中下标位置为 %d 的数据已删除.\n\n",pos);
            break;
        case 10:
            printf("请输入要删除的数据:>");
            scanf("%d",&Item);
            delete_val(&mylist,Item);
            break;
        case 11:
            sort(&mylist);
            show_list(&mylist);
            break;
        case 12:
            resver(&mylist);
            show_list(&mylist);
            break;
        case 13:
            clear(&mylist);
            break;
        case 14:
            destroy(&mylist);
            break;
        default:
            printf("输入的选择错误,请重新输入.\n");
            break;
        }
    }

}

#include"SeqList.h"
In general, use <> to reference system header files, and use "" for header files defined by yourself. And #include"SeqList.h" is a file defined by ourselves, don't forget to quote.
A total of fifteen kinds of requirements have been realized, that is, fifteen function modules, 14 kinds of function requirements are printed out, and another is the function module of space increase function. The program starts with the main function. My program design is based on the data structure Yan Weimin's C language version of the textbook, and relying on this, I try to implement the various functions in the book. In order to better realize the interaction with the user, I use a large number of printf functions in the main function, so that the user can specify the execution of the program, that is, what the program does.

2) SeqList.h

#ifndef HEADER_FILES_H_INCLUDED
#define HEADER_FILES_H_INCLUDED

#include<stdio.h>

///初始化顺序表用到的两个函数所在的头文件的引入
#include<malloc.h>
#include<assert.h>

///布尔类型的头文件引入
#include<stdbool.h>

#define SEQLIST_INIT_SIZE 8  ///顺序表初始开辟8个空间
#define INC_SIZE          3  ///顺序表增加3空间
typedef int ElemType;  ///EleType为int类型,方便其他类型的改动

///结构体类型
typedef struct SeqList
{
    ElemType *base;
    int      capacity;
    int      size;
}SeqList;

///空间增加函数
bool Inc(SeqList *list);

///初始化
void InitSeqList(SeqList *list);

///1.尾部插入
void push_back(SeqList *list, ElemType x);
///2.头部插入
void push_front(SeqList *list, ElemType x);

///3.显示线性表
void show_list(SeqList *list);

///4.尾部删除
void pop_front(SeqList *list);
///5.头部删除
void pop_back(SeqList *list);

///6.按位置插入
void insert_pos(SeqList *list, ElemType x, int pos);

///7.查找
int find(SeqList *list,ElemType key);
///8.顺序表的长度
int length(SeqList *list);

///9.按位置删除
void delete_pos(SeqList *list,int pos);
///10.按值删除
void delete_val(SeqList *list,ElemType x);

///11.排序
void sort(SeqList *list);
///12.逆置
void resver(SeqList *list);

///13.清除
void clear(SeqList *list);
///14.摧毁
void destroy(SeqList *list);

SeqList.h calls the system header file, defines the structure type, and declares the function.
It’s worth mentioning that there is no support hereBoolean typeusage of.
I give two solutions here. The
first one is introduced like in my code

#include<stdbool.h>
Header file, because the Boolean type is resolved in the latest C language standard (C99), a total of such header files are provided.
The second type of self-defined, the code is as follows

#dfine BOOL in

Or directly define true and flase

#define FLASE 0
#define TRUE  1

The rest of the notes have been explained clearly, so I won't repeat them here.

3) SeqList.c

#include"SeqList.h"
///初始化顺序表
void InitSeqList(SeqList *list)
{
    list->base = (ElemType *)malloc(sizeof(ElemType) * SEQLIST_INIT_SIZE);
    assert(list->base != NULL);
    list->capacity = SEQLIST_INIT_SIZE;
    list->size = 0;
}

//1.尾部插入
void push_back(SeqList *list, ElemType x)
{
    if(list->size >= list->capacity && !Inc(list))
    {
        printf("顺序表空间已满,%d 不能在尾部插入数据\n\n", x);
        return;
    }
    list->base[list->size] = x;
    list->size++;
}

//2.头部插入
void push_front(SeqList *list, ElemType x)
{
    if(list->size >= list->capacity && !Inc(list))
    {
        printf("顺序表空间已满,不能在头部插入数据\n\n");
        return;
    }

    int i;
    for(i=list->size;i>0;--i)
    {
        list->base[i] = list->base[i-1];
    }
    list->base[0] = x;
    list->size++;
}

//3.显示线性表
void show_list(SeqList *list)
{
    if(list->size == 0)
    {
        printf("顺序表无数据.\n\n");
    }
    else
    {
        printf("顺序表中的数据显示如下:>\n");
        int i;
        for(i=0; i<list->size; ++i)
        {
            printf("%d ", list->base[i]);
        }
        printf("\n");
        printf("\n");
    }
}

//4.尾部删除
void pop_back(SeqList *list)
{
    if(list->size == 0)
    {
        printf("顺序表已空,无法删除尾部数据\n\n");
        return;
    }
    list->size--;
    printf("已删除顺序表尾部一个数据.\n\n");
}

//5.头部删除
void pop_front(SeqList *list)
{
    if(list->size == 0)
    {
        printf("顺序表已空,无法删除头部数据\n\n");
        return;
    }
    int i;
    for(i=0; i<list->size-1; i++)
    {
        list->base[i] = list->base[i+1];
    }
    list->size--;
    printf("已删除顺序表头部一个数据.\n\n");
}

//6.按位置插入
void insert_pos(SeqList *list,ElemType x,int pos)
{
    ///判空
     if(list->size >= list->capacity && !Inc(list))
    {
        printf("顺序表空间已满,不能在该位置插入数据\n\n");
        return;
    }
    ///查看位置是否合理,即是否符合顺序表特点
    if(pos<0 || pos>list->size)
    {
        printf("插入数据的位置不合理,不能插入!!!\n\n");
        return;
    }

    if(pos == 0) ///相当于头插
    {
        push_front(list, x);
    }
    else if(pos == list->size)  ///相当于尾插
    {
        push_back(list, x);
    }
    else
    {
        int i;
        ///元素依次后移,空出pos位置
        for(i=list->size; i>pos; i++)
        {
        list->base[i] = list->base[i-1];
        }
        list->base[pos] = x; ///插入元素
        list->size++; ///长度加一
    }
    printf("\n");
}

//7.查找
int find(SeqList *list,ElemType key)
{
    int i;
    for(i=0; i<list->size; i++)
    {
        if(list->base[i] == key)
        return i;
    }
    return -1;
}

//8.顺序表的长度
int length(SeqList *list)
{
    return list->size;
}

//9.按位置删除
void delete_pos(SeqList *list,int pos)
{
    if(list->size == 0)
    {
        printf("顺序表已空,不能删除该位置数据.\n\n");
        return;
    }

    if(pos<0 || pos>list->size-1)
    {
        printf("要删除数据的下标位置不合理,不能删除!!!\n\n");
        return;
    }

    int i;
    for(i=pos; i<list->size-1; i++)
    {
        list->base[i] = list->base[i+1];
    }
    list->size--;

}

//10.按值删除
void delete_val(SeqList *list ,ElemType x)
{
    if(list->size == 0)
    {
        printf("顺序表已空,不存在该数据.\n\n");
        return;
    }

    int pos = find(list,x);
    if(pos == -1)
    {
        printf("要删除的数据在顺序表中不存在.\n\n");
        return;
    }
    delete_pos(list,pos);
    printf("顺序表中 %d 数据已删除.\n\n",x);
}

///11.排序
void sort(SeqList *list)
{
    if(list->size == 0)
    {
        printf("顺序表已空,无法排序.\n");
    }
    else
    {
        printf("顺序表中数据已从小到大排序:>\n\n");
        ///冒泡排序(从小到大)
        int i,j;
        for(i=0; i<list->size-1; i++)
        {
            for(j=0; j<list->size-1-i; j++)
            {
                if(list->base[j] > list->base[j+1])
                {
                    ElemType temp = list->base[j];
                    list->base[j] = list->base[j+1];
                    list->base[j+1] = temp;
                }
            }
        
    }
}

//12逆置
void resver(SeqList *list)
{
    if(list->size == 0)
    {
        printf("顺序表为空,无法逆置.\n\n");
        return;
    }
    else if(list ->size == 1)
    {
        printf("顺序表已逆置.\n\n");
        return ;
    }

    int low = 0, high = list->size-1;
    ElemType temp;
    while(low < high)
    {
        temp = list->base[low];
        list->base[low] = list->base[high];
        list->base[high] = temp;

        low++;
        high--;
    }
    printf("顺序表已逆置.\n\n");
}

//13.清除
void clear(SeqList *list)
{
    list->size = 0;
    printf("顺序表中数据已清除\n\n");
}

//14.摧毁
void destroy(SeqList *list)
{
    free(list->base);
    list->base = NULL;
    list->capacity = 0;
    list->size = 0;
    printf("顺序表已摧毁\n\n");
}

4) Display of running effect

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

4. Write at the end

So far the functions are all implemented, the test is correct, the following is going to write a two sequence table merged into a sequence table, which is also a classic function in the data structure. I am going to write this book based on the data structure of Yan Weimin's C language version. I'm just a junior undergraduate student. There are many shortcomings in the code. Welcome everyone to leave a message to exchange experience, or code improvement.

Guess you like

Origin blog.csdn.net/weixin_45787909/article/details/108722847