// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>


#define MaxSize 100
#define Elemtype int

typedef struct {
    Elemtype List[MaxSize];
    int size;
    int length;
}SqList;

void InitList(SqList& L) {
    L.size = MaxSize;
    L.length = 0;
}

void DestoryList(SqList& L) {
    L.size = 0;
    L.length = 0;
}

void ClearList(SqList& L) {
    L.length = 0;
}

int IsEmpty(SqList L) {
    if (L.length == 0)
        return true;
    else
        return false;
}

int ListLength(SqList L) {
    return L.length;
}

int GetElem(SqList L, int i, Elemtype& e) {
    if (i < 0) {
        printf("THE ILLEGAL INPUT,YOU CAN'T INPUT NEGATIVE INDEX\n");
        return false;
    }
    if (i >= ListLength(L)) {
        printf("YOUR INPUT EXCEEDS THE LIST'S LENGTH\n");
        return false;
    }
    e = L.List[i];
    return true;
}

int LocateElem(SqList L, Elemtype e) {
    for (int i = 0; i < ListLength(L); i++)
    {
        if (L.List[i] == e)
            return i;
    }
    return -1;
}

int PriorElem(SqList L, Elemtype cur, Elemtype& prior) {
    int PriorIndex = LocateElem(L,cur) - 1;
    if (PriorIndex == -2) {
        printf("Don't Exist the Element\n");
        return false;
    }
    if (PriorIndex == -1) {
        printf("The Element index is ONE,Don't Exist the Prior Element\n");
        return false;
    }
    prior = L.List[PriorIndex];
    return true;
}

int NextElem(SqList L, Elemtype cur, Elemtype& next) {
    int CurrentIndex = LocateElem(L, cur);
    if (CurrentIndex == -1) {
        printf("Don't Exist the Element\n");
        return false;
    }
    if (CurrentIndex + 1 == ListLength(L)) {
        printf("The Element index is THE LAST,Don't Exist the NEXT Element\n");
        return false;
    }
    next = L.List[CurrentIndex + 1];
    return true;
}

int ListInsert(SqList& L, int i, Elemtype e) {
    if (L.size == L.length) {
        printf("THE LIST IS FULL,CAN'T ADD ANY ELEMENT\n");
        return false;
    }
    if (i < 0) {
        printf("ILLEGAL INPUT,YOU PUT INDEX IS LESS THAN ZERO\n");
        return false;
    }
    if (i > ListLength(L)) {
        printf("THE INDEX IS TOO BIG FOR INSERTING NOW\n");
        return false;
    }
    for (int j = ListLength(L)-1; j >= i; j--)
    {
        L.List[j + 1] = L.List[j];
    }
    L.List[i] = e;
    L.length++;
    return true;
}

int ListDelete(SqList& L, int i, Elemtype& e) {
    if (i < 0) {
        printf("ILLEGAL INPUT,YOU PUT INDEX IS LESS THAN ZERO\n");
        return false;
    }
    if (i >= ListLength(L)) {
        printf("YOUR INDEX CANNOT EXIST IN THIS LIST\n");
        return false;
    }
    if (ListLength(L) == 0) {
        printf("THE LIST IS EMPTY, CAN'T DELETE ANY ELEMENT\n");
        return false;
    }
    e = L.List[i];
    for (int j = i+1; j < ListLength(L); j++)
    {
        L.List[j - 1] = L.List[j];
    }
    L.length--;
}

void Visit(int i) {
    printf("%d ",i);
}

int ListTravel(SqList L) {
    for (int i = 0; i < ListLength(L); i++)
    {
        Visit(L.List[i]);
    }
    printf("\n");
    return 0;
}

// Delete the numbers bigger than m
void function(SqList& L,int m) {
    int e;
    for (int i = 0; i < ListLength(L); i++)
    {
        GetElem(L, i, e);
        if (e > m) {
            ListDelete(L, i, e);
            --i;
        }
    }
}
  
int main()
{
    SqList La;
    int num;
    int e;
    InitList(La);
    for (int i = 0; i < 10; i++)
    {
        scanf_s("%d", &num);
        ListInsert(La, i, num);
    }
    ListTravel(La);
    ListDelete(La, 0, e);
    printf("delete num is %d\n", e);
    ListTravel(La);
    function(La, 4);
    ListTravel(La);
    return 0;
}

 

posted on 2023-12-21 16:15  Rabbit_XIN  阅读(2)  评论(0编辑  收藏  举报