-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.cpp
108 lines (92 loc) · 1.69 KB
/
order.cpp
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>
typedef int ElemType;
typedef int Status;
#define ERROR 0
#define OK 1
typedef struct Node {
ElemType element;
struct Node * link;
}Node;
typedef struct {
struct Node* head;
int n;
}HeaderList;
Status Init(HeaderList *h);
Status Output(HeaderList *h);
Status order(HeaderList *h, int mSize);
Status Insert(HeaderList *h, int i, ElemType x);
// Status Delete(SeqList *L,int i);
// void Destory(SeqList *L);
// 单链表的初始化
Status Init(HeaderList *h) {
h->head=(Node*)malloc(sizeof(Node));
if(!h->head){
return ERROR;
}
h->head->link = NULL;
h->n = 0;
return OK;
}
Status order(HeaderList *h, int n){
int flag=-1;
Node *p = h->head->link,*q=p->link;
while(p->link!=NULL){
if(p->element>=q->element){
flag=-1;
break;
}
else flag=1;
p=q;
q=q->link;
}
if(flag==-1){
printf("No");
}
else printf("Yes");
return OK;
}
Status Insert(HeaderList *h, int i, ElemType x) {
Node *p, *q;
int j;
if (i<-1 || i>h->n - 1)
return ERROR;
p = h->head;
for (j = 0; j <= i; j++) {
p = p->link;
}
q = (Node*)malloc(sizeof(Node));
q->element = x;
q->link = p->link;
p->link = q;
h->n++;
return OK;
}
// 单链表的输出
Status Output(HeaderList *h) {
Node *p;
if (!h->n)
return ERROR;
p = h->head->link;
while (p) {
printf("%d ",p->element);
p = p->link;
}
return OK;
}
int main()
{
int i, x, nn;
HeaderList list;
scanf("%d", &nn);
printf("\n");
Init(&list);
for (i = 0; i < nn; i++) {
scanf("%d", &x);
Insert(&list, i - 1, x);
}
Output(&list);
printf("\n");
order(&list,nn);
return 0;
}