顺序表的建立、输入、输出、查找、插入、删除(数据结构)

1.顺序表的基本操作实践。

(1)建立4个元素的顺序表list[]={2,3,4,5},实现顺序表建立的基本操作。

(2)在list[]={2,3,4,5}的元素4和5之间插入一个元素9,实现顺序表插入的基本操作。

(3)在list[]={2,3,4,9,5}中删除指定位置(i=3)上的元素4,实现顺序表的删除的基本操作。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAXSIZE 10
using namespace std;

typedef int ElemType;
typedef struct {
    ElemType a[MAXSIZE];
    int length;
} S;
void CreatList(S &L) {
    scanf("%d", &L.length);
    for(int i = 1; i <= L.length; i ++) scanf("%d",&L.a[i]);
}   //创建列表
void PutList(S L) {
    for(int i = 1; i <= L.length; i ++) {
        printf("%d ",L.a[i]);
    }
    printf("
");
}  //输出列表
void InserElem(S &L, int i, ElemType x) {               j      i
    if(i < 1 || i > L.length) return;             2 3 4 5      9
    for(int j = L.length+1; j > i; j --) {              j-1  j
        L.a[j] = L.a[j-1];                        2 3 4  9   5
    }
    L.a[i] = x;
    L.length++;
} //插入
void DeleElem(S &L, int i) {
    for(int j = i; j < L.length; j ++) {
        L.a[j] = L.a[j+1];                         j j+1
    }                                          2 3 4  9  5
    L.length--;
}//删除int main() {
    S L;
    CreatList(L);
    InserElem(L,4,9);
    PutList(L);
    DeleElem(L,3);
    PutList(L);
    return 0;
}

结果

E:c++>b
4
2 3 4 5
2 3 4 9 5
2 3 9 5
原文地址:https://www.cnblogs.com/bearkid/p/8824662.html