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("\n");
}  //输出列表
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

 

相关文章:

  • 2021-11-28
  • 2019-06-11
  • 2021-10-08
  • 2021-11-21
  • 2021-12-12
  • 2021-06-24
  • 2019-08-07
  • 2019-01-25
猜你喜欢
  • 2020-06-13
  • 2021-10-26
  • 2021-11-13
  • 2020-06-21
  • 2021-11-20
  • 2021-12-04
  • 2021-08-31
  • 2021-12-05
相关资源
相似解决方案