Basic operation implementation of linear table (sequential table) (C language)

Although my code is written in C language, remember to create a C++ file when you create the file!

Without further ado, let’s go straight to the code:

First we create our ownheader file and put the function declaration into the header file (here my header file The name is: "seqlist11.h") (for related operations on creating a header file, please refer to my previous article)

This is my project file. Note that when you create header file source files, you must put them in the same folder.

Header file (stores various function declarations and structure definitions) source code:

#ifndef SEQLIST11_H_INCLUDED
#define SEQLIST11_H_INCLUDED

#define ADD_SIZE 10
#include<stdio.h>
#include<assert.h>
#include<malloc.h>

#define SEQLIST_INIT_SIZE 10
typedef int Elemtype;
typedef struct Seqlist
{
    Elemtype*base;
    int  capacity;
    int  size;
} Seqlist;

bool add(Seqlist*list);

void InitSeqlist(Seqlist*list);
void push_back(Seqlist*list,Elemtype x) ;
void push_front(Seqlist*list,Elemtype x);

void show_list(Seqlist*list);

void pop_front(Seqlist*list);
void pop_back(Seqlist*list);
void insert_pos(Seqlist*list, int pos,Elemtype x);

int find(Seqlist*list,Elemtype key);
int length(Seqlist*list);

void delete_pos(Seqlist*list, int pos);
void delete_val(Seqlist*list, Elemtype key);

void sort(Seqlist*list);
void resver(Seqlist*list);
void clear(Seqlist*list);
void destroy(Seqlist*list);

void merge(Seqlist*lt,Seqlist*la,Seqlist*lb);
#endif // SEQLIST11_H_INCLUDED

The definition of the corresponding declared function in the header file:

#include"seqlist11.h"//引入自己编写的头文件
bool add(Seqlist*list)
{
    Elemtype*newbase=(Elemtype*)realloc(list->base,sizeof(Elemtype)*(list->capacity+ADD_SIZE));
    if(newbase==NULL)
    {
        printf("增加空间失败,内存不足\n");
        return false;
    }
    list->base=newbase;
    list->capacity+=ADD_SIZE;
    return true;
}
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;
}
void push_back(Seqlist*list,Elemtype x)
{
    if(list->size>=list->capacity&&!add(list))
    {
        printf("顺序表空间已满,无法尾部插入数据“\n");
        return;
    }
    list->base[list->size]=x;
    list->size++;

}
void push_front(Seqlist*list,Elemtype x)
{
    if(list->size>=list->capacity&&!add(list))
    {
        printf("顺序表空间已满,无法头部插入数据“\n");
        return;//直接结束该函数
    }
    for(int i=list->size; i>0; i--)
    {
        list->base[i]=list->base[i-1];
    }
    list->base[0]=x;
    list->size++;
}
void show_list(Seqlist*list)
{
    for(int i=0; i<list->size; i++)
        printf("%d ",list->base[i]);
    printf("\n");
}
void pop_front(Seqlist*list)
{
    if(list->size==0)
    {
        printf("该线性表已为空,无法进行头部删除\n");
        return;
    }
    for(int i=0; i<list->size-1; i++)
    {
        list->base[i]=list->base[i+1];
    }
    list->size--;
}
void pop_back(Seqlist*list)
{
    if(list->size==0)
    {
        printf("该线性表已为空,无法进行尾部删除\n");
        return;
    }
    list->size--;
}
void insert_pos(Seqlist*list, int pos,Elemtype x)
{
    if(list->size>=list->capacity&&!add(list))
    {
        printf("顺序表空间已满,无法插入数据“\n");
        return;
    }
    if(pos<0||pos>list->size)
    {
        printf("插入数据的位置不合法\n");
        return;
    }
    for(int i=list->size; i>=pos; --i)
    {
        list->base[i]=list->base[i-1];
    }
    list->base[pos]=x;
    list->size++;
}
int find(Seqlist*list,Elemtype key)
{
    for(int i=0; i<list->size; ++i)
    {
        if(list->base[i]==key)
            return i;
    }
    return -1;
}
int length(Seqlist*list)
{
    return list->size;
}
void delete_pos(Seqlist*list, int pos)
{
    if(list->size==0)
    {
        printf("该线性表已为空,无法进行位置删除\n");
        return;
    }
    if(pos<0||pos>list->size)
    {
        printf("删除的位置(下标)不合法\n");
        return;
    }
    // if(pos==0)
    //   pop_front(&Seqlist);
    //if(pos==list->size-1)
    //  pop_back(&Seqlist);
    for(int i=pos; i<list->size-1; i++)
    {
        list->base[i]=list->base[i+1];
    }
    list->size--;
}
void  delete_val(Seqlist*list, Elemtype key)
{
    int pos=find(list,key);
    if(pos==-1)
    {
        printf("要删除的元素%d不存在\n",key);
        return;
    }
    delete_pos(list,pos);
}
void sort(Seqlist*list)
{
    Elemtype tem;
    for(int i=0; i<list->size-1; i++)
    {
        for(int j=0; j<list->size-i-1; j++)
        {
            if(list->base[j]>list->base[j+1])
            {
                tem=list->base[j+1];
                list->base[j+1]=list->base[j];
                list->base[j]=tem;
            }
        }
    }
}
void resver(Seqlist*list)
{
    Elemtype tem;
    if(list->size==0||list->size==1)
        return;
    int low=0;
    int high=list->size-1;
    while(low<high)
    {
        tem=list->base[high];
        list->base[high]=list->base[low];
        list->base[low]=tem;
        low++;
        high--;
    }
}
void clear(Seqlist*list)
{
    list->size=0;
}
void destroy(Seqlist*list)
{
    free(list->base);
    list->size=0;
    list->capacity=0;
    list->base=NULL;
}
void merge(Seqlist*lt,Seqlist*la,Seqlist*lb)
{
    lt->capacity=la->size+lb->size;
    lt->base=(Elemtype*)malloc(sizeof(Elemtype)*lt->capacity);
    assert(lt->base!=NULL);
    int ia=0;
    int ib=0;
    int ic=0;
    while(ia<la->size&&ib<lb->size)
    {
        if(la->base[ia]<lb->base[ib])
            lt->base[ic++]=la->base[ia++];
        else
            lt->base[ic++]=lb->base[ib++];
    }
    while(ia<la->size)
    {
        lt->base[ic++]=la->base[ia++];
    }
    while(ib<la->size)
    {
        lt->base[ic++]=lb->base[ib++];
    }
    lt->size=la->size+lb->size;
}

Code for the test file (the file used to test the functionality of the function):

#include"seqlist11.h"

int main()
{
    Seqlist mylist;
    Seqlist yourlist;
    Seqlist list;
    InitSeqlist(&mylist);
    InitSeqlist(&yourlist);
    push_back(&mylist,1);
    push_back(&mylist,3);
    push_back(&mylist,5);
    push_back(&mylist,7);
    push_back(&mylist,9);

    push_back(&yourlist,2);
    push_back(&yourlist,4);
    push_back(&yourlist,6);
    push_back(&yourlist,8);
    push_back(&yourlist,10);
    merge(&list,&mylist,&yourlist);
    show_list(&list);
    return 0;
}
/*int main()
{
    Seqlist mylist;
    InitSeqlist(&mylist);
    int select = 1;
    int item;
    int pos;
    while (select)
    {
        printf("-----------------------------------------------------------------\n");
        printf("* [1]    push_back                            [2]    push_front *\n");
        printf("* [3]    showlist                             [4]    pop_front  *\n");
        printf("* [5]    pop_back                             [6]    insert_pos *\n");
        printf("* [7]    find                                 [8]    length     *\n");
        printf("* [9]    delete_pos                           [10]   delete_val *\n");
        printf("* [11]   sort                                 [12]   resver     *\n");
        printf("* [13]   clear                                [14*]   destroy    *\n");
        printf("* [0]    quit_system                           [00]   void       *\n");
        printf("-----------------------------------------------------------------\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);
            }
            break;
        case 2:
            printf("请输入要插入的元素(头插)(-1结束):");
            while(scanf("%d",&item),item!=-1)
            {
                push_front(&mylist,item);
            }
            break;
        case 3:
            show_list(&mylist);
            break;
        case 4:
            pop_front(&mylist);
            break;
        case 5 :
            pop_back(&mylist);
            break;
        case 6 :
            printf("请输入要插入的元素:\n");
            scanf("%d",&item);
            printf("请输入要插入的位置:\n");
            scanf("%d",&pos);
            insert_pos(&mylist,pos,item);
            break;
        case 7 :
            printf("请输入你要查找的元素:\n");
            scanf("%d",&item);
            pos=find(&mylist,item);
            if(pos==-1)
                printf("你要查的元素%d不在线性表内\n",item);
            else
                printf("你要查找的元素%d下标为%d\n",item,pos);

            break;
        case 8 :
            printf("该线性表的长度为%d\n",length(&mylist));
            break;
        case 9 :
            printf("请输入要删除的位置:\n");
            scanf("%d",&pos);
            delete_pos(&mylist, pos);
            break;
        case 10 :
            printf("请输入要删除的元素:\n");
            scanf("%d",&item);
            delete_val(&mylist, item);
            break;
        case 11 :
            sort(&mylist);
            break;
        case 12 :
            resver(&mylist);
            break;
        case 13 :
            clear(&mylist);
            break;
        //case 14:
        //  destroy(&mylist);
        default:
            printf("输入的操作不存在\n");
            break;
        }
    }
    destroy(&mylist);
    return 0;
}*/

Much of the above code (code of the test file) has been commented out. Brothers and sisters, you can refer to my code and then test the corresponding functions yourself.

The above is the sequential implementation code of the linear table. Thank you everyone for reading. I will also upload the chained implementation code of the linear table in the future. I hope you will support it! thank you all! (If you have anything you don’t understand, you can leave a message in the comment area or add Q: 2742256636 to ask. I am also a code novice. I welcome everyone to actively point out my mistakes)

Guess you like

Origin blog.csdn.net/weixin_46713492/article/details/124084034