forked from zhuxinquan/Data-Structure-And-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_reverse.c
63 lines (55 loc) · 1.2 KB
/
sequence_reverse.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
/*************************************************************************
> File Name: sequence_reverse.c
> Author: zhuxinquan
> Mail: [email protected]
> Created Time: 2015年09月20日 星期日 17时42分27秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define N 20
struct seqlist{
int data[N];
int length;
};
typedef struct seqlist * Sequence;
void CreatSequence(Sequence L)
{
int x, i = 0;
printf("please input and enter -1 end:\n");
scanf("%d", &x);
while(x != -1)
{
L->data[i++] = x;
scanf("%d", &x);
}
L->length = i;
}
void reverse(Sequence L)
{
int i, temp;
int middle = (L->length + 1)/2;
for(i = 0; i < middle; i++)
{
temp = L->data[i];
L->data[i] = L->data[L->length - i -1];
L->data[L->length -i -1] = temp;
}
}
void PrintSequence(Sequence L)
{
int i;
for(i = 0; i < L->length; i++)
{
printf("%3d", L->data[i]);
}
printf("\n");
}
int main(void)
{
Sequence L;
L = (Sequence)malloc(sizeof(struct seqlist));
CreatSequence(L);
PrintSequence(L);
reverse(L);
PrintSequence(L);
}