顺序表的实现[原创]
1/*顺序表的实现*/
2#include <malloc.h>
3#include <stdio.h>
4
5
6#define List_Init_Size 100
7#define ListIncrement 10
8
9typedef struct{ /*顺序表类型定义*/
10 char *elem;
11 int lenght;
12 int listsize;
13}sqlist;
14
15
16int compare(char a,char b){ /*比较函数*/
17 if(a==b) return 1;
18 return 0;
19}
20
21
22void print(sqlist l){ /*输出顺序表的元素到屏幕*/
23 int i=1;
24 while(i<l.lenght){
25 printf("%c ",l.elem[i-1]);
26 i++;
27 }
28 printf("\n");
29}
30
31
32int initlist_sq(sqlist *l){
33 l->elem=(char*)malloc(List_Init_Size*sizeof(char)); /*算法1------初始化一个l指向的顺序表*/
34 if(!l->elem) return 0;
35 l->lenght=0;
36 l->listsize=List_Init_Size;
37 return 1;
38}
39
40
41int listinsert_sq(sqlist *l,int i,char e){ /*算法2------在顺序表中第i个元素之前插入值为e的元素*/
42 char *q,*p,*newbase;
43 if(i<1 || i>l->lenght+1) return 0;
44 if(l->lenght>=l->listsize){
45 newbase=(char*)malloc((l->listsize+ListIncrement)*sizeof(char));
46 if(!newbase) return 0;
47 i=l;
48 while(i<=l->lenght){
49 newbase[i-1]=l->elem[i-1];
50 i++;
51 }
52 l->elem=newbase;
53 l->listsize+=ListIncrement;
54 }
55 q=&(l->elem[i-1]);
56 for(p=&(l->elem[l->lenght-1]);p>=q;--p)
57 *(p+1)=*p;
58
59 *q=e;
60 ++l->lenght;
61
62return 1;
63}
64
65
66int listdelete_sq(sqlist *l,int i,char *e){ /*算法3-------删除顺序表中第i个元素,并用e返回其值*/
67 char *p,*q;
68 if((i<1)||(i>l->lenght)) return 0;
69 p=&(l->elem[i-1]);
70 q=l->elem+l->lenght-1;
71
72 for(++p;p<=q;++p)
73 *(p-1)=*p;
74 --l->lenght;
75
76 return 1;
77}
78
79
80int locelem_sq(sqlist l,char e){ /*算法4--------确定顺序表l中元素值为e的元素位置*/
81 int i=1;
82 char *p; p=l.elem;
83 while(i<=l.lenght && !compare(*p++,e)) ++i;
84 if(i<=l.lenght) return i;
85 else return 0;
86}
87
88
89void mergelist_sq(sqlist la,sqlist lb,sqlist *lc){ /*算法5-------归并两个顺序表,得到一个值按非递减顺序排列的新表*/
90 char *pa,*pb,*pc,*pa_last,*pb_last;
91 pa=la.elem; pb=lb.elem;
92 lc->listsize=lc->lenght=la.lenght+lb.lenght;
93
94 pc=lc->elem=(char*)malloc(lc->listsize*sizeof(char));
95 if(!lc->elem) return 0;
96
97 pa_last=la.elem+la.lenght-1;
98 pb_last=lb.elem+lb.lenght-1;
99
100 while(pa<=pa_last && pb<=pb_last){
101 if(*pa<=*pb) *pc++=*pa++;
102 else *pc++=*pb++;
103 }
104 while(pa<=pa_last) *pc++=*pa++;
105 while(pb<=pb_last) *pc++=*pb++;
106
107}
108
109
110
111
112
113
114
115main(){
116 int j=1,x=97,i=0;
117 char elem='Q',del1,del2,*b,B;
118 sqlist *l,list_sq,*list_sq1,*list_sq2,list_sq3,*m;
119
120 l=&list_sq; m=&list_sq3; b=&B;
121
122 initlist_sq(l); /*------算法1*/
123 l->lenght=10;
124 for(j=1;j<=10;j++){
125 l->elem[j-1]=x++; /*初始化顺序表*/
126 }
127
128 printf("This sqlist's value:");
129 print(*l);
130
131 listinsert_sq(l,5,elem); /*------算法2,*/
132
133 printf("\n\nAfter --- listinsert_sq(l,5,elem)---\n");
134 print(*l);
135
136 listdelete_sq(l,6,b); /*------算法3*/
137 printf("\n\nAfter --- listdelete_sq(l,6,b) ---\n");
138 print(*l);
139
140
141 printf("\n\ng located at %d\n",locelem_sq(list_sq,'g')); /*------算法4*/
142
143
144
145 initlist_sq(list_sq1);
146 initlist_sq(list_sq2);
147 for(j=1,x=97;j<=6;j++,x+=2){
148 list_sq1->elem[j-1]=x;
149 }
150 list_sq1->lenght=6;
151 for(j=1,x=98;j<=6;j++,x+=2){
152 list_sq2->elem[j-1]=x;
153 }
154 list_sq2->lenght=6;
155
156 printf("\n\nlist_sq1's value:");
157 print(*list_sq1);
158
159 printf("list_sq2's value:");
160 print(*list_sq2);
161
162
163 mergelist_sq(*list_sq1,*list_sq2,m); /*------算法5*/
164 printf("\n\nmerged list_sq3:");
165 print(list_sq3);
166
167
168getch();
169}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
2#include <malloc.h>
3#include <stdio.h>
4
5
6#define List_Init_Size 100
7#define ListIncrement 10
8
9typedef struct{ /*顺序表类型定义*/
10 char *elem;
11 int lenght;
12 int listsize;
13}sqlist;
14
15
16int compare(char a,char b){ /*比较函数*/
17 if(a==b) return 1;
18 return 0;
19}
20
21
22void print(sqlist l){ /*输出顺序表的元素到屏幕*/
23 int i=1;
24 while(i<l.lenght){
25 printf("%c ",l.elem[i-1]);
26 i++;
27 }
28 printf("\n");
29}
30
31
32int initlist_sq(sqlist *l){
33 l->elem=(char*)malloc(List_Init_Size*sizeof(char)); /*算法1------初始化一个l指向的顺序表*/
34 if(!l->elem) return 0;
35 l->lenght=0;
36 l->listsize=List_Init_Size;
37 return 1;
38}
39
40
41int listinsert_sq(sqlist *l,int i,char e){ /*算法2------在顺序表中第i个元素之前插入值为e的元素*/
42 char *q,*p,*newbase;
43 if(i<1 || i>l->lenght+1) return 0;
44 if(l->lenght>=l->listsize){
45 newbase=(char*)malloc((l->listsize+ListIncrement)*sizeof(char));
46 if(!newbase) return 0;
47 i=l;
48 while(i<=l->lenght){
49 newbase[i-1]=l->elem[i-1];
50 i++;
51 }
52 l->elem=newbase;
53 l->listsize+=ListIncrement;
54 }
55 q=&(l->elem[i-1]);
56 for(p=&(l->elem[l->lenght-1]);p>=q;--p)
57 *(p+1)=*p;
58
59 *q=e;
60 ++l->lenght;
61
62return 1;
63}
64
65
66int listdelete_sq(sqlist *l,int i,char *e){ /*算法3-------删除顺序表中第i个元素,并用e返回其值*/
67 char *p,*q;
68 if((i<1)||(i>l->lenght)) return 0;
69 p=&(l->elem[i-1]);
70 q=l->elem+l->lenght-1;
71
72 for(++p;p<=q;++p)
73 *(p-1)=*p;
74 --l->lenght;
75
76 return 1;
77}
78
79
80int locelem_sq(sqlist l,char e){ /*算法4--------确定顺序表l中元素值为e的元素位置*/
81 int i=1;
82 char *p; p=l.elem;
83 while(i<=l.lenght && !compare(*p++,e)) ++i;
84 if(i<=l.lenght) return i;
85 else return 0;
86}
87
88
89void mergelist_sq(sqlist la,sqlist lb,sqlist *lc){ /*算法5-------归并两个顺序表,得到一个值按非递减顺序排列的新表*/
90 char *pa,*pb,*pc,*pa_last,*pb_last;
91 pa=la.elem; pb=lb.elem;
92 lc->listsize=lc->lenght=la.lenght+lb.lenght;
93
94 pc=lc->elem=(char*)malloc(lc->listsize*sizeof(char));
95 if(!lc->elem) return 0;
96
97 pa_last=la.elem+la.lenght-1;
98 pb_last=lb.elem+lb.lenght-1;
99
100 while(pa<=pa_last && pb<=pb_last){
101 if(*pa<=*pb) *pc++=*pa++;
102 else *pc++=*pb++;
103 }
104 while(pa<=pa_last) *pc++=*pa++;
105 while(pb<=pb_last) *pc++=*pb++;
106
107}
108
109
110
111
112
113
114
115main(){
116 int j=1,x=97,i=0;
117 char elem='Q',del1,del2,*b,B;
118 sqlist *l,list_sq,*list_sq1,*list_sq2,list_sq3,*m;
119
120 l=&list_sq; m=&list_sq3; b=&B;
121
122 initlist_sq(l); /*------算法1*/
123 l->lenght=10;
124 for(j=1;j<=10;j++){
125 l->elem[j-1]=x++; /*初始化顺序表*/
126 }
127
128 printf("This sqlist's value:");
129 print(*l);
130
131 listinsert_sq(l,5,elem); /*------算法2,*/
132
133 printf("\n\nAfter --- listinsert_sq(l,5,elem)---\n");
134 print(*l);
135
136 listdelete_sq(l,6,b); /*------算法3*/
137 printf("\n\nAfter --- listdelete_sq(l,6,b) ---\n");
138 print(*l);
139
140
141 printf("\n\ng located at %d\n",locelem_sq(list_sq,'g')); /*------算法4*/
142
143
144
145 initlist_sq(list_sq1);
146 initlist_sq(list_sq2);
147 for(j=1,x=97;j<=6;j++,x+=2){
148 list_sq1->elem[j-1]=x;
149 }
150 list_sq1->lenght=6;
151 for(j=1,x=98;j<=6;j++,x+=2){
152 list_sq2->elem[j-1]=x;
153 }
154 list_sq2->lenght=6;
155
156 printf("\n\nlist_sq1's value:");
157 print(*list_sq1);
158
159 printf("list_sq2's value:");
160 print(*list_sq2);
161
162
163 mergelist_sq(*list_sq1,*list_sq2,m); /*------算法5*/
164 printf("\n\nmerged list_sq3:");
165 print(list_sq3);
166
167
168getch();
169}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188