-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequence-list.c
More file actions
74 lines (67 loc) · 1.41 KB
/
sequence-list.c
File metadata and controls
74 lines (67 loc) · 1.41 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LIST_SIZE 20
#define LIST_ADD_SIZE 10
typedef struct{
int date;
}date;
typedef struct Date_List{
date *elem;
int length;
int list_size;
}List;
void creatList(List *list){
list->elem = (date *)malloc(LIST_SIZE*sizeof(date));
if(!list->elem){
return exit(0);
}
list->length = 0;
list->list_size = LIST_SIZE;
}
void insertList(List *list,int key,date *newList){
if(!(key >= 0 && key < list->length+1)){
return exit(0);
}
if(list->length >= list->list_size){
date *newBase = (date *)realloc(list->elem,(list->list_size+LIST_ADD_SIZE)*sizeof(date));
if(!newBase){
return exit(0);
}
list->elem = newBase;
list->list_size += LIST_ADD_SIZE;
}
int i = list->length;
for(;i > key;i--){
*&list->elem[i] = *&list->elem[i-1];
}
*&list->elem[key] = *newList;
list->length++;
}
void deleteList(List *list,int key){
if(!(key >= 0 && key < list->length)){
return exit(0);
}
for(;key < list->length-1;key++){
*&list->elem[key] = *&list->elem[key+1];
}
list->length--;
}
int main(){
List *list = (List *)malloc(sizeof(List));
creatList(list);
int i = 0;
for(;i < 10;i++){
date *newDate = (date *)malloc(sizeof(date));
newDate->date = i;
insertList(list,i,newDate);
}
for(i = 0;i < 10;i++){
printf("%d\n",list->elem[i].date);
}
printf("\n\n\n");
deleteList(list,5);
for(i = 0;i < 9;i++){
printf("%d\n",list->elem[i].date);
}
}