顺序表的基本操作

下面展示一些 内联代码片

// A code block
var foo = 'bar';

头文件

// An highlighted block
#pragma once
#define  MAXLISTSIZE  100 //预设的存储空间最大容量 

# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef int Status;
#include<stdio.h>
#include<stdlib.h>

# include<malloc.h>
typedef int  ElemType;
typedef struct {
    ElemType* elem;
    int length; 
    int listsize;
} SqList;


 Status InitList(SqList*& L, int maxsize);  // 构造一个最大存储容量为 maxsize 的空的顺序表 L
void DestroyList(SqList& L);
Status ListEmpty(SqList& L);

Status GetElem(SqList L, int i, int& e);
Status Createlist(SqList& L, int n);
Status Insertlist(SqList L, int i, int e);
Status Detelelist(SqList &L, int i);
Status InitList(SqList &L, int maxsize) {

    L.elem = (ElemType*)malloc(maxsize * sizeof(ElemType));
    if (!L.elem)
     exit(OVERFLOW);
    L.length = 0;
    L.listsize = maxsize;
    return OK;
}

void printflist(SqList &L,int n) {

    if (ListEmpty(L))
        printf("数组为空");


    for (int i = 0; i < n; i++) {
        printf("%d ", L.elem[i]);
    }

}

int Desteroy(SqList& L) {

    if (!L.elem) {
        return ERROR;
    }


    free(L.elem);
    return 0;
}
Status ListEmpty(SqList& L) {
    if (!L.elem) {

        return ERROR; 
    }
    if (L.length == 0)
        return TRUE;
    else
        return FALSE;



}//从顺序表L中查找第i个元素,由参数e返回其元素的值
Status GetElem(SqList L, int i, int& e)
{
    if (!L.elem) {
        return ERROR;

    }


    if (i<1 || i>L.length - 1) {
        return ERROR;
    }
    int* q = L.elem;
    e = *(q - 1 + i);
    return OK;
}

//在顺序表L中查找元素e的位置,不存在则返回0
int LocateElem(SqList L, int e)
{
    if (!L.elem) {
        return 0;
    }
    int i = 1;
    int* p= (L.elem);
    while (i < L.length && *p++ != e) {

        ++i;

    }
    if (i < L.length) {
        return i;
    }
    return 0;
}

Status Insertlist(SqList L,int i,int e)
{
    if (!L.elem) {
        return ERROR;
    }
    if (i<1 || i>L.length - 1) {

        return ERROR;
    }
    int j;
    for(j = L.length - 1;j>i;j--)
    {
        L.elem[j] = L.elem[j - 1];
    }
    L.elem[i] = e;
    L.length++;
    return OK;
}
Status Detelelist(SqList &L, int i) {
    if (i<1 || i>L.length - 1) {
        return ERROR;
    }
   
    for (int j = i; j < L.length-1; j++)
    {
        L.elem[j-1] = L.elem[j ];

    }
    L.length--;

    return OK;
}
Status Createlist(SqList &L,int n) {
    L.length = 0;

    for (int i = 0; i < n; i++) {
        L.length++;
        scanf_s("%d", &L.elem[i]);
    }
    return OK;

}

#include"sqlist.h"
int main() {

    SqList head;
    int n,e,k;
    scanf_s("%d", &n);
    InitList(head, 100);
    Createlist(head,n);
    printflist(head,n);
    printf("\n请插入元素:");
    scanf_s("%d", &e);
  
    if (LocateElem(head, e))
    {
        int w;
        printf("请输入插入位次:\n");
        scanf_s("%d", &w);
        Insertlist(head, w, e);
        printflist(head, n);
    }
      printf("\n请查找第k个元素:");
       scanf_s("%d", &k);
    int h;
    if (GetElem(head, k, h)) {
        printf("查找到了%d\n", h);
      
    }
    else {
        printf("未查找到\n");
    }
    int p;
    printf("请删除第几位元素:\n");
    scanf_s("%d", &p);
    if (LocateElem(head, p)) {
        Detelelist(head, p);
        printflist(head, n);
    }



}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值