1 #include<stdio.h> 2 #include<stdlib.h> 3 4 //typedef 80 MAXSIZE; 5 #define MAXSIZE 20 6 7 typedef struct Node{ 8 int data; 9 int cursor; 10 }Node,St
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 //typedef 80 MAXSIZE;
5 #define MAXSIZE 20
6
7 typedef struct Node{
8 int data;
9 int cursor;
10 }Node,StaticList[MAXSIZE];
11
12 void InitialSpace(StaticList Space){
13 int i;
14 for(i = 0;i < MAXSIZE - 1; i++){
15 Space[i].cursor = i + 1;
16 }
17 Space[MAXSIZE - 1].cursor = 0;
18 }
19 int Malloc_StaticList(StaticList Space){
20 int index;
21 index = Space[0].cursor;
22 if(index){
23 Space[0].cursor = Space[index].cursor;
24 return index;
25 }
26 else{
27 printf("Space Full, Failed!\n");
28 exit(1);
29 }
30 }
31 void Free_StaticList(StaticList Space,int index){
32 Space[index].data = 0;
33 Space[index].cursor = Space[0].cursor;
34 Space[0].cursor = index;
35 }
36 int InitialStaticList(StaticList Space){
37 int i;
38 i = Malloc_StaticList(Space);
39 Space[i].cursor = 0;
40 return i;
41 }
42 void ClearStaticList(StaticList Space,int n_List){
43 int temp_n_List = n_List;
44 n_List = Space[n_List].cursor;
45 int t_cursor;
46 while(n_List != 0){
47 t_cursor = Space[n_List].cursor;
48 Space[n_List].data = 0;
49 Free_StaticList(Space,n_List);
50 n_List = t_cursor;
51 }
52 Space[temp_n_List].cursor = 0;
53 printf("Clear Static List Executed!\n");
54 }
55 void StaticListEmpty(StaticList space,int n_List){
56 if(space[n_List].cursor)
57 printf("No Empty\n");
58 else
59 printf("Empty!\n");
60 }
61 int StaticList_Length(StaticList Space,int n_List){
62 int count = 0;
63 while(Space[n_List].cursor){
64 count++;
65 n_List = Space[n_List].cursor;
66 }
67 return count;
68 }
69 void InsertStaticList(StaticList Space,int n_List,int position,int e){
70 /*
71 *
72 *
73 * */
74 int length = StaticList_Length(Space,n_List);
75 int copy_n_List = n_List;
76 if(position < 1 || position > length + 1){
77 printf("Insert funtion ERROR: position\n");
78 exit(1);
79 }
80 int new_i = Malloc_StaticList(Space);
81 if(new_i){
82 Space[new_i].data = e;
83 int count = 0,i;
84 for(i = 1; i < position; i++){ //
85 n_List = Space[n_List].cursor;
86 }
87 Space[new_i].cursor = Space[n_List].cursor;
88 Space[n_List].cursor = new_i;
89 printf("INFORMATION: Insert %d to List%d with position=%d executed!\n",e,copy_n_List,position);
90 }
91 }
92 void DeleteStaticList(StaticList space,int n_List,int position){
93 int length = StaticList_Length(space,n_List);
94 int t_n_List = n_List;
95 if(position < 1 || position > length){
96 printf("function DeleteSataicList Error:position!\n");
97 }
98 else
99 {
100 int count = 0,result;
101 while(count < position - 1){
102 n_List = space[n_List].cursor;
103 count++;
104 }
105 result = space[space[n_List].cursor].data;
106 printf("deleteSataisList executed: element %d in position %d of List%d was deleted\n",result,position,t_n_List);
107 int temp = space[n_List].cursor;
108 space[n_List].cursor = space[space[n_List].cursor].cursor;
109 Free_StaticList(space,temp);
110 }
111 }
112 int GetStaticListElement(StaticList space,int n_List,int position){
113 int count = 0;
114 int temp_n_List = n_List;
115 while(space[n_List].cursor && count < position){
116 n_List = space[n_List].cursor;
117 count++;
118 }
119 if(space[n_List].cursor == 0){
120 printf("INFORMATION:get staticList element Error: position!\n");
121 }
122 else{
123 int e;
124 e = space[n_List].data;
125 printf("GetStaticListElement with position=%d from List%d executed! the result is %d\n",position,temp_n_List,e);
126 }
127 }
128 int LocateStaticList(StaticList space,int n_List,int e){
129 n_List = space[n_List].cursor;
130 while(n_List){
131 if(space[n_List].data == e){
132 return n_List;
133 }
134 n_List = space[n_List].cursor;
135 }
136 return 0;
137 }
138 void NextElement_SL(StaticList space,int n_List,int currentElement){
139 if(LocateStaticList(space,n_List,currentElement) == 0)
140 printf("function NextElement Error:currentElement%d dose not exist\n",currentElement);
141 else if(space[LocateStaticList(space,n_List,currentElement)].cursor == 0)
142 printf("function NextElement Error:the currentElement%d is last one of list%d\n",currentElement,n_List);
143 else
144 printf("the nextElement of %d is %d\n",currentElement,space[space[LocateStaticList(space,n_List,currentElement)].cursor].data);
145 }
146 void PriorElement_SL(StaticList space,int n_List,int currentElement){
147 if(!LocateStaticList(space,n_List,currentElement))
148 printf("function PriorElement Error:currentElement%d does not exist\n",currentElement);
149 else if(space[n_List].cursor == LocateStaticList(space,n_List,currentElement))
150 printf("function PriorElement Error:the currentElement%d is the first of the List%d\n",currentElement,n_List);
151 else{
152 n_List = space[n_List].cursor;
153 int temp;
154 while(space[n_List].data != currentElement){
155 temp = n_List;
156 n_List = space[n_List].cursor;
157 }
158 printf("the PriorElement of %d is %d\n",currentElement,space[temp].data);
159 }
160 }
161 void DisplaySpace(StaticList Space){
162 int i;
163 for(i = 0;i < MAXSIZE; i++)
164 printf("%4d",Space[i].cursor);
165 printf("\n");
166 for(i = 0;i < MAXSIZE; i++)
167 printf("%4d",Space[i].data);
168 printf("\n");
169 for(i = 0;i < MAXSIZE; i++)
170 printf("%4d",i);
171 printf("\n");
172 }
173 /*****************************************************/
174 void CreateStaticList(StaticList space,int n_List,int length){
175 if(length > FreeSpaceLength(space)){
176 printf("No remaining Space!\n");
177 }
178 else
179 {
180 int i,new_index;
181 printf("Create Static List %d:\n",n_List);
182 for(i = 1;i <= length;i++){
183 printf("--------enter %d element: ",i);
184 new_index = Malloc_StaticList(space);
185 scanf("%d",&space[new_index].data);
186 space[n_List].cursor = new_index;
187 n_List = new_index;
188 }
189 space[new_index].cursor = 0;
190 }
191 }
192 int FreeSpaceLength(StaticList space){
193 int length = 0;
194 int index = space[0].cursor;
195 while(index){
196 length++;
197 index = space[index].cursor;
198 }
199 return length;
200 }
201 void setdatato0inordertodisplay(StaticList space){
202 int i;
203 for(i = 0;i < MAXSIZE; i++)
204 space[i].data = 0;
205 }
206 void main(){
207 StaticList space;
208 setdatato0inordertodisplay(space);
209 InitialSpace(space);
210
211 int list1 = InitialStaticList(space);
212 int list2 = InitialStaticList(space);
213 int list3 = InitialStaticList(space);
214
215 printf(" SHOW SPACE\n");
216 DisplaySpace(space);
217
218 int length3 = StaticList_Length(space,list3);
219 printf("\nthe length of list3 is %d\n",length3);
220 CreateStaticList(space,list3,5);
221 CreateStaticList(space,list2,25);
222 DisplaySpace(space);
223
224 length3 = StaticList_Length(space,list3);
225 printf("the length of list3 is %d\n",length3);
226 int length2 = StaticList_Length(space,list2);
227 printf("the length of list2 is %d\n",length2);
228
229 CreateStaticList(space,list2,3);
230 DisplaySpace(space);
231 length2 = StaticList_Length(space,list2);
232 printf("the length of list2 is %d\n",length2);
233 InsertStaticList(space,list3,4,666);
234
235 DisplaySpace(space);
236 printf("the length of list3 is %d\n",StaticList_Length(space,list3));
237
238 GetStaticListElement(space,list3,3);
239 GetStaticListElement(space,list3,9);
240 GetStaticListElement(space,list1,1);
241 GetStaticListElement(space,list2,1);
242
243 if(LocateStaticList(space,list3,4)){
244 printf("the position of element 4 in List3 is %d\n",LocateStaticList(space,list3,4));
245 }else
246 printf("locateStaticlist in list3 with 4 is Failed!\n");
247
248 if(LocateStaticList(space,list3,2)){
249 printf("the position of element 2 in List3 is %d\n",LocateStaticList(space,list3,4));
250 }else
251 printf("locateStaticlist in list3 with 2 is Failed!\n");
252
253 printf("\n\ntest NextElemet\n");
254 NextElement_SL(space,list3,9);
255 NextElement_SL(space,list3,7);
256 NextElement_SL(space,list3,3);
257
258 printf("\n\ntest PriorElement\n");
259 PriorElement_SL(space,list3,3);
260 PriorElement_SL(space,list3,8);
261 PriorElement_SL(space,list3,1);
262
263 printf("\n");
264 DeleteStaticList(space,list3,5);
265 printf("\nthe length of list3 is %d\n",StaticList_Length(space,list3));
266 DeleteStaticList(space,list2,35);
267 printf("\nthe length of list2 is %d\n",StaticList_Length(space,list2));
268
269 InsertStaticList(space,list3,2,888);
270 DisplaySpace(space);
271
272 ClearStaticList(space,list3);
273 DisplaySpace(space);
274 }