-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inverse.c
108 lines (96 loc) · 1.9 KB
/
Inverse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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);
// 顺序表初始化
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;
}
// 顺序表输出
Status Output(SeqList L) {
int i;
if (!L.n)
return ERROR;
for (i = 0; i <= L.n - 1; i++)
printf("%d ", L.element[i]); //从前往后逐个输出元素
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); // 对线性表初始化
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);
}