Inversion of sequence table

Summer vacation is almost over, just want to make up the notes of data structure.

subject

Write a program to realize the inversion of the sequence table.

Part of code

void Inverse(SeqList *L, int mSize) {
    int i, temp;
    for (i = 0; i < mSize / 2; i++) {
        temp = L->element[i];
        L->element[i] = L->element[mSize - 1 - i];
        L->element[mSize - 1 - i] = temp;
    }
}

Complete program

#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;

typedef struct {
    int n;
    int maxLength;
    ElemType *element;
}SeqList;


Status Init(SeqList *L, int mSize);
Status Output(SeqList L);
void Inverse(SeqList *L, int mSize);
Status Insert(SeqList *L, int i, ElemType x);
// Status Delete(SeqList *L,int i);
// void Destory(SeqList *L);


// Sequence table initialization
Status Init(SeqList *L, int mSize) {
    L->maxLength = mSize;
    L->n = 0;
    L->element = (ElemType*)malloc(sizeof(ElemType)*mSize);
    if(!L->element)
        return ERROR;
    return OK;
}

void Inverse(SeqList *L, int mSize) {
    int i, temp;
    for (i = 0; i < mSize / 2; i++) {
        temp = L->element[i];
        L->element[i] = L->element[mSize - 1 - i];
        L->element[mSize - 1 - i] = temp;
    }
}

Status Insert(SeqList *L, int i, ElemType x) {
    int j;
    if (i<-1 || i>L->n - 1)
        return ERROR;
    if (L->n == L->maxLength)
        return ERROR;
    for (j = L->n - 1; j > i; j--) {
        L->element[j + 1] = L->element[j];
    }
    L->element[i + 1] = x;
    L -> n = L->n + 1;
    return OK;
}


// Sequence table output
Status Output(SeqList L) {
    int i;
    if (!L.n)
        return ERROR;
    for (i = 0; i <= L.n - 1; i++)
        printf("%d ", L.element[i]);  //Output elements one by one from front to back
    return OK;
}

/*
Status Delete(SeqList *L,int i){
    int j;
    if (i<0 || i>L->n - 1)
        return ERROR;
    if (!L->n)
        return ERROR;
    for (j =i+1; j < L->n; j++) {
        L->element[j - 1] = L->element[j];
    }
    L -> n --;
    return OK;
}
*/

/*
void Destory(SeqList *L){
    (*L).n=0;
    (*L).maxLength=0;
    free((*L).element);
}
*/

int main()
{
    int i,x,nn;
    scanf("%d",&nn);
    //printf("\n");
    SeqList list;
    Init(&list, nn);  // Initialize linear table
    for (i = 0; i < nn; i++) {
        scanf("%d",&x);
        Insert(&list, i - 1, x);
    }
    Output(list);
    Inverse(&list,nn);
    printf("\n");
    Output(list);
    printf("\n");
    // Delete(&list,0);
    // Destory(&list);
}

experimental result

Copyright notice: This is the original article of the blogger. If there is any mistake, please point out in the comment area. Thank you very much. If you want to reprint and indicate the source, you can ~
This article starts from personal blog: Wonz の Blog .

Added by Ang3l0fDeath on Fri, 03 Jan 2020 10:55:50 +0200