顺序表操作集合(初始化 创建 插入 删除 清空 销毁等)

代码如下:
其中case 7 8 9的scanf函数中%号前均有空白,详细原因参考两次调用scanf函数的问题
若有错误和可以改进的地方,敬请评论区指正。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
#define  OVERFLOW -2
#define ElemType char

typedef struct{
    ElemType* elem;   //存储空间基地址
    int len;          //当前长度
    int listsize;     //当前分配的顺序表长度
}SqList;

SqList List;

void menu();
void InitSqList(SqList& L);
void ExtendSqList(SqList& L);
void DestroySqList(SqList& L);
void CreateSqList(SqList& L);
void PrintSqList(SqList L);
void SqListInsert(SqList& L,int i,ElemType e);
void SqListDelete(SqList& L,int i);
void ClearSqList(SqList& L);
bool SqListEmpty(SqList L);
int LocateElem(SqList L,ElemType e);
ElemType PriorElem(SqList L,ElemType cur,ElemType& pre);
ElemType NextElem(SqList L,ElemType cur,ElemType& next);
ElemType GetElem(SqList L,int i,ElemType& e);

int main(){
    menu();
    int order;
    while(1){
        puts("Please enter order:");
        scanf("%d",&order);
        switch(order){
            case 1:
                InitSqList(List);
                puts("Init right!");
                break;
            case 2:
                puts("Please enter SqList:");
                CreateSqList(List);
                break;
            case 3:
                int m;
                ElemType e;
                puts("Please enter insert-sequence m && insert-elem e:");
                scanf("%d %c",&m,&e);
                SqListInsert(List,m,e);
                puts("After insert:");
                PrintSqList(List);
                break;
            case 4:
                int j;
                puts("Please enter delete-sequence j:");
                scanf("%d",&j);
                SqListDelete(List,j);
                puts("After delete:");
                PrintSqList(List);
                break;
            case 5:
                ClearSqList(List);
                puts("Clear correct!");
                break;
            case 6:
                if(SqListEmpty(List))
                    puts("The SqList is empty!");
                else
                    puts("The SqList is NOT empty!");
                break;
            case 7:
                ElemType x;
                puts("Please enter elem to confirm It's sequence:");
                scanf(" %c",&x);
                printf("%d\n",LocateElem(List,x)+1);
                break;
            case 8:
                ElemType cur,pre;
                puts("Please enter cur:");
                scanf(" %c",&cur);
                printf("%c\n", PriorElem(List,cur,pre));
                break;
            case 9:
                ElemType cur1,next;
                puts("Please enter cur1:");
                scanf(" %c",&cur1);
                printf("%c\n", NextElem(List,cur1,next));
                break;
            case 10:
                int k;
                ElemType elem;
                puts("Please enter sequence k to confirm It's value:");
                scanf("%d",&k);
                printf("%c\n", GetElem(List,k,elem));
                break;
            case 11:
                PrintSqList(List);
                break;
            case 12:
                DestroySqList(List);
                break;
            case 13:
                exit(0);
            default:break;
        }
    }
}

void menu(){
    puts("1-初始化 2-创建 3-插入 4-删除");
    puts("5-清空 6-判空 7-给值定位 8-前驱");
    puts("9-后继 10-给位求值 11-打印 12-销毁 13-退出");
}

void InitSqList(SqList& L){
    L.elem = (ElemType*) malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem) exit(OVERFLOW);
    L.len = 0;
    L.listsize = LIST_INIT_SIZE;
}

void ExtendSqList(SqList& L){
    ElemType* newbase = (ElemType*) realloc(L.elem,(L.listsize+LIST_INCREMENT* sizeof(ElemType)));
    if(!newbase) exit(OVERFLOW);
    L.elem = newbase;
    L.listsize += LIST_INCREMENT;
}

void DestroySqList(SqList& L){
    free(L.elem);
    L.len = 0;
    L.listsize = 0;
    printf("Destroyed!\n");
}

void CreateSqList(SqList& L){
    scanf("%s",L.elem);
    L.len += strlen(L.elem);
}

void PrintSqList(SqList L){
    for(int i=0;i<L.len;i++){
        printf("%c",L.elem[i]);
    }
    printf("\n");
}

/*此时i的范围为[1,L.len+1]
 * 在第i个元素之前插入
 *从i个元素起,均后移一位
 * 移动元素期望值:n/2
 */
void SqListInsert(SqList& L,int i,ElemType e){
    if(i<1||i>L.len+1)
        exit(0);
    if(L.len>=L.listsize)
        ExtendSqList(L);
    ElemType* p;
    ElemType* q;
    q = &(L.elem[i-1]);
    for(p=&(L.elem[L.len-1]);p>=q;--p)
        *(p+1) = *p;
    *q = e;
    L.len++;
}

//移动元素期望值:(n-1)/2
void SqListDelete(SqList& L,int i){
    if((i<1)||(i>L.len))
        exit(0);
    ElemType* p;
    ElemType* q;
    p = &(L.elem[i-1]);
    q = &(L.elem[L.len-1]);
    for(++p;p<=q;++p)
        *(p-1) = *p;
    L.len--;
}

void ClearSqList(SqList& L){
    L.len = 0;
}

bool SqListEmpty(SqList L){
    if(L.len == 0)
        return true;
    else
        return false;
}

int LocateElem(SqList L,ElemType e){
    for(int i=0;i<L.len;i++){
        if(L.elem[i]==e){
            return i;
        }
    }
}

ElemType PriorElem(SqList L,ElemType cur,ElemType& pre){
    if(LocateElem(L,cur)>=1&& LocateElem(L,cur)<=L.len-1)
        pre = L.elem[LocateElem(L,cur)-1];
    else
        exit(0);
    return pre;
}

ElemType NextElem(SqList L,ElemType cur,ElemType& next){
    if(LocateElem(L,cur)>=0&& LocateElem(L,cur)<L.len-1)
        next = L.elem[LocateElem(L,cur)+1];
    else
        exit(0);
    return next;
}

ElemType GetElem(SqList L,int i,ElemType& e){
    while(i<=0&&i>L.len-1)
        exit(0);
    e = L.elem[i-1];
    return e;
}

上一篇:448. 找到所有数组中消失的数字


下一篇:【知识总结】前端高频HTML、CSS、浏览器相关的面试题合集