Post

数据结构 第一次实验课任务

顺序表的实现

实现图片:
代码

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXSIZE 10

enum boolean{false, true};
typedef enum boolean bool;
typedef int ElementType;



// list 结构体
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};



// 定义接口


//创建并返回一个空的线性表;
List MakeEmpty(){
    List list = malloc(sizeof(struct LNode));
    list->Last=-1;
    return list;
}

// 返回线性表中X的位置。若找不到则返回0;
int Find( List L, ElementType X ){
    if(L==NULL) return -1;
    int n = L->Last; 
    do{
        if(L->Data[n]==X)
        return n+1;

    }while(--n);

    return -1;
}

//查找线性表中位置为i的数据;
int Get(List L, int i ){
    if(L==NULL) return -1;
    if(L->Last<i-1) return -1;
    return L->Data[i-1];
}

//将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;
bool Insert( List L, ElementType X, Position P ){
    if(L->Last==MAXSIZE-1) {
        printf("FULL\n");
        return false;
    }
    if(P<1 || P>L->Last+2){
        printf("ILLEAGAL POSITION\n");
        return false;
    }
    if(L==NULL){
        printf("STRUCTURE CRASHED\n");
        return false;
    }
    for(int i=L->Last;i>=P-1;i--){
        L->Data[i+1] = L->Data[i];
    }
    L->Data[P-1]=X;
    L->Last++;
    return true;
    
}

//将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。
bool Delete( List L, Position P ){
    if(P>L->Last || P<1) {
        printf("POSITION P EMPTY\n");
        return false;
    }
    if(L==NULL){
        printf("STRUCTURE CRASHED\n");
        return false;
    }

    for(int i=P-1;i<=L->Last;i++){
        L->Data[i] = L->Data[i+1];
    }
    L->Last--;
    return true;
}


enum {TailInsert=1, PosInsert,Del,PosFind,ValFind};

void ShowList(List L){
    if(L==NULL || L->Last<0 ) return;
    for(int i=0;i<=L->Last;i++){
        printf("%d\t",i+1);
    }
    printf("\n");
    for(int i=0;i<=L->Last;i++){
        printf("%d\t",L->Data[i]);
    }
    printf("\n");
}

void RandomList(List L){
    srand((unsigned)time(NULL));
    for(int i=0; i<MAXSIZE-1; i++){
        Insert(L,rand()%100,L->Last+2);
    }
}

void TInsert(List L){
    printf("请输入插入数值:");
    int num;
    scanf("%d",&num);
    Insert(L,num,L->Last+2);
    return;
}
void PInsert(List L){
    int num, pos;
    printf("请输入插入数值:");
    scanf("%d",&num);
    printf("请输入插入位置:");
    scanf("%d",&pos);
    Insert(L,num,pos);
    return;
}

void DList(List L){
    int pos;
    printf("请输入删除第几个位置的元素:");
    scanf("%d",&pos);
    Delete(L,pos);
    return;

}

void PFind(List L){
    int pos;
    printf("请输入查找第几个位置的元素:");
    scanf("%d",&pos);
    printf("结果:%d\n",Get(L, pos));
    return;
}

void VFind(List L){
    int val;
    printf("请输入查找元素的值:");
    scanf("%d",&val);
    printf("结果:%d\n",Find(L, val));
    return;
}


int main(){
    List L = MakeEmpty();
    RandomList(L);
    /* 
    随机新建表
    1. 尾部插入
    2. 按位置插入
    3. 删除
    4. 按位置查找
    5. 按指查找
    6. break
    */
   int choice;
   while(1){
    printf("\n\n\n最大可存元素数为 %d ,当前列表:\n", MAXSIZE);
    ShowList(L);
    printf("\
    1. 尾部插入\n\
    2. 按位置插入\n\
    3. 删除\n\
    4. 按位置查找\n\
    5. 按指查找\n\
    6. break\n\
    请输入想要进行的操作:\
    ");
    scanf("%d",&choice);
    if(choice>6 || choice < 1) {
        printf("非法输入\n");
        continue;
    }
    switch (choice)
    {
    case TailInsert:
        /* code */
        TInsert(L);
        break;
    case PosInsert:
        /* code */
        PInsert(L);
        break;
    case Del:
        /* code */
        DList(L);
        break;
    case PosFind:
        /* code */
        PFind(L);
        break;
    case ValFind:
        /* code */
        VFind(L);
        break;
    case 6:
        return 0;
    default:
        continue;
    }
   }
        


}
This post is licensed under CC BY 4.0 by the author.