20221320 冯泰瑞 2022-2023-2 数据结构第一次作业---顺序表的两种定义和七种操作(顺序表的创建,判断是否为空,查找,前插,后插,按值删,按位删)

20221320 冯泰瑞 2022-2023-2 数据结构第一次作业---顺序表的两种定义和七种操作(顺序表的创建,判断是否为空,查找,前插,后插,按值删,按位删)

一、静态定义

#include <stdio.h>
#define MAXNUM 100//定义顺序表的最大长度
struct SeqList{
	int elem[MAXNUM];//顺序表的静态分配
	int n;
};
struct SeqList InitList(struct SeqList List);
int main()
{
    struct SeqList List;
    List=InitList(List);//将初始化的顺序表的值放在静态顺序表中
    int i;
    for(i=0;i<List.n;i++)
    {
        printf("%d\t",List.elem[i]);//打印顺序表中的值
    }
    return 0;
}
struct SeqList InitList(struct SeqList List)
{
    int i;
    printf("Input the value of List.n(List.n<MAXNUM):");
    scanf("%d",&List.n);//输入顺序表的实际长度
    for(i=0;i<List.n;i++)
    {
        List.elem[i]=i+1;//初始化顺序表的值
    }
    return List;
}

二、动态定义

#include <stdio.h>
#include <stdlib.h>//malloc函数的头文件
typedef struct SeqList{
	int MAXNUM;
	int *element;//指向顺序表的指针
	int n;
}*List;
List createNullList_seq(int m);//创建一个空顺序表并对其初始化
int  isNullList_seq(List list );//判断顺序表是否为空
int  locate_seq(List list,int x);//查找顺序表中的该值的下标
int  insertPre_seq(List list, int p,int x );//在下标为p的元素之前,插入一个值为x的元素
int  insertPost_seq(List list, int p,int x );//在下标为p的元素之后,插入一个值为x的元素
int  deleteP_seq(List list, int p);//删除下标为p的元素
int  deleteV_seq(List list,int x );//删除顺序表中第一个值为x的元素
int main()
{
    int m;
    printf("Input the max number of the sequence list:");
    scanf("%d",&m);
    List list=createNullList_seq(m);
    if(list==NULL)
    {
        printf("Required False!");
    }
    else
    {
        int i;
        printf("Now the sequence list is ");
        if(list->n>0)
        {
            for(i=0;i<list->n;i++)
        {
            printf("%d\t",list->element[i]);//对创建的顺序表的值进行打印
        }
        printf("\n");
        }
        else
        {
            printf("NULL\n");
        }
    }
    int flag,choice=1;
    while(choice!=0)
    {
        printf("What operation do you want to make?\n");
        printf("Input 1 to check whether the sequence list is empty or not.\n");
        printf("Input 2 to search for the subscript.\n");
        printf("Input 3 to insert a value before the subscript p.\n");
        printf("Input 4 to insert a value after the subscript p.\n");
        printf("Input 5 to delete the value whose subscript is p.\n");
        printf("Input 6 to delete the first value x in the sequence.\n");
        printf("Input 7 to exit the program,\n");
        scanf("%d",&flag);
        int ret,x,p;
        switch(flag)
        {
            case 1:
            ret=isNullList_seq(list);//不可以在case语句中定义其他变量
            if(ret!=0)
            {
                printf("The sequence list is not empty.\n");
            }
            else
            {
                printf("The sequence list is empty.\n");
            }
            break;
            case 2:
            printf("Input the value x you want to search(x<list->n):");
            scanf("%d",&x);
            ret=locate_seq(list,x);
            if(ret!=-1)
            {
                printf("The very subscript is %d.\n",ret);
            }
            else
            {
                printf("Can't find the very value!\n ");
            }
            break;
            case 3:
            printf("Input the position where you want to insert the value.\n");
            scanf("%d",&p);
            printf("Input the value you want to insert.\n");
            scanf("%d",&x);
            ret=insertPre_seq(list,p,x);
            if(ret!=-1)
            {
                int i;
                for(i=0;i<list->n;i++)
                    {
                        printf("%d\t",list->element[i]);
                    }
                printf("\n");
            }
            else
            {
                printf("There is no enough space to insert the value.\n");
            }
            break;
            case 4:
            printf("Input the position where you want to insert the value.\n");
            scanf("%d",&p);
            printf("Input the value you want to insert.\n");
            scanf("%d",&x);
            ret=insertPost_seq(list,p,x);
            if(ret!=-1)
            {
                int i;
                for(i=0;i<list->n;i++)
                    {
                        printf("%d\t",list->element[i]);
                    }
                printf("\n");
            }
            else
            {
                printf("There is no enough space to insert the value.\n");
            }
            break;
            case 5:
            printf("Input the position which you want to delete.\n");
            scanf("%d",&p);
            ret=deleteP_seq(list,p);
            if(ret!=-1)
            {
                int i;
                for(i=0;i<list->n;i++)
                    {
                        printf("%d\t",list->element[i]);
                    }
                printf("\n");
            }
            else
            {
                printf("The position you input is out of range!\n");
            }
            break;
            case 6:
            printf("Input the value which you want to delete.\n");
            scanf("%d",&x);
            int ret=deleteV_seq(list,x);
            if(ret!=-1)
            {
                int i;
                for(i=0;i<list->n;i++)
                    {
                        printf("%d\t",list->element[i]);
                    }
                printf("\n");
            }
            else
            {
                printf("Can't find the value you input!\n");
            }
            break;
            case 7:
            exit(0);
            break;
        }
    }
    free(list);
    return 0;
}

List createNullList_seq(int m)
{
    List list;
    list=(List)malloc(sizeof(List));
    list->element=(int*)malloc(sizeof(int)*m);
    list->n=0;
    list->MAXNUM=m;
    printf("Input the value of List.n:(List.n<MAXNUM):");
    scanf("%d",&list->n);
    int i;
    for(i=0;i<list->n;i++)
    {
        list->element[i]=i+1;
    }
    return list;
}

int  isNullList_seq(List list )
{
    if (list->n==0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int  locate_seq(List list,int x)
{
    int i;
    for(i=0;i<list->n;i++)
    {
        if(list->element[i]==x)
        {
            return i+1;
        }
    }
    return -1;
}

int  insertPre_seq(List list, int p,int x )
{
    int i;
    if(list->n+1<list->MAXNUM)
    {
        for(i=list->n-1;i>=p-1;i--)
        {
            list->element[i+1]=list->element[i];
        }
        list->element[p-1]=x;
        list->n++;
        return 0;
    }
    else
    {
        return -1;
    }
}

int  insertPost_seq(List list, int p,int x )
{
    int i;
    if(list->n+1<list->MAXNUM)
    {
        for(i=list->n-1;i>=p;i--)
        {
            list->element[i+1]=list->element[i];
        }
        list->element[p]=x;
        list->n++;
        return 0;
    }
    else
    {
        return -1;
    }
}

int  deleteP_seq(List list,int p)
{
    int i;
    if(p<=list->n&&p>=0)
    {
        for (i=p;i<list->n;i++)
        {
            list->element[i-1]=list->element[i];
        }
        list->n--;
        return 0;
    }
    else
    {
        return -1;
    }
}

int  deleteV_seq(List list,int x)
{
    int i,flag=0;
    for(i=0;i<list->n;i++)
    {
        if(list->element[i]==x)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        return -1;
    }
    else
    {
        for(;i<list->n-1;i++)
        {
            list->element[i]=list->element[i+1];
        }
        list->n--;
        return 0;
    }
}
posted @ 2023-03-16 10:37  20221320冯泰瑞  阅读(23)  评论(0编辑  收藏  举报