20230317

数据结构remake第一天
线性表的操作

//
//  baby DataStructrue.cpp
//  dataStructure
//
//  Created by on 2023/3/17.
#include<stdio.h>
#define N 10
#define MAX 20
typedef int SLDataType;
typedef struct
{
    SLDataType array[N];
    int size;
}SeqList;
void SeqListInit(SeqList &L)
{
    int i,n;
    printf("How many numbers in the list?:\n");
    scanf("%d",&n);
    if(n>MAX){
        printf("ERROR!\n");
    }
    L.size=n;
    printf("What in the List?:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&L.array[i]);
    }
}
void SeqListDisplay(SeqList &L)
{
    int i=0;
    printf("Display the list:\n");
    for(i=0;i<L.size;i++)
    {
        printf("%d ",L.array[i]);
    }
    printf("\n");
}
void SeqListInsert(SeqList &L)
{
    int a,b,i;
    i=0;
    printf("'Place to Insert' 'number'\n");
    scanf("%d %d",&a,&b);
    L.size++;
    for(i=L.size-1;i>a;i--){
        L.array[i]=L.array[i-1];
    }
    L.array[a]=b;
}
void SeqListDelete(SeqList &L){
    int i,j,fun2;
    printf("press 1 to match the number or press 2 to delete the number on specific location\n");
    printf("Notice:Currently, only one number is supported, if there are multiple identical numbers that require this operation, please do it multiple times, after all, MAX is only 20 :)\n");
    scanf("%d",&fun2);
    if(fun2 == 1){
        int num;
        printf("delete the number:");
        scanf("%d",&num);
        for(i=0;i<L.size;i++){
            if(L.array[i]==num){
                for(j=i;j<L.size-1;j++){
                    L.array[j]=L.array[j+1];
                }
            }
        }
        L.size--;
    }
    else if(fun2 == 2){
        int place;
        printf("where to delete:");
        scanf("%d",&place);
        for(i=place;i<L.size-1;i++){
            L.array[i]=L.array[i+1];
        }
        L.size--;
    }
}
void SeqListSearch(SeqList &L){
    int i,num;
    printf("search the number:");
    scanf("%d",&num);
    for(i=0;i<L.size;i++){
        if(L.array[i]==num){
            printf("%d is at the %d st/nd/rd/th place of the list\n",num,i+1);
            break;
        }
    }
}
void SeqListChange(SeqList &L){
    int a,b;
    printf("'Place to change' 'number'\n");
    scanf("%d %d",&a,&b);
    L.array[a]=b;
}
int main()
{
    SeqList L;
    SeqListInit(L);
    SeqListDisplay(L);
    int fun1 = 0;
    printf("press 1 to insert\nprint 2 to delete\npress 3 to search\npress 4 to change\npress 0 to finish\n");
    while(1){
        printf("What to do next?\n");
        scanf("%d",&fun1);
        printf("%c",fun1);
        if(fun1 == 1){
            SeqListInsert(L);}
        else if(fun1 == 2){
            SeqListDelete(L);}
        else if(fun1 == 3){
            SeqListSearch(L);}
        else if(fun1 == 4){
            SeqListChange(L);}
        else if(fun1 == 0){
            printf("goodbye!\n");
            SeqListDisplay(L);
            break;}
        SeqListDisplay(L);
    }
    return 0;
}
posted @ 2023-03-17 19:58  noobwei  阅读(11)  评论(0编辑  收藏  举报