1 typedef struct node 2 { 3 ElemType data; 4 struct node*prior, *next; 5 }dlink; 6 7 void initlist(slink *&sq) 8 { 9 sq = (slink*) malloc(sizeof(slink)); 10 sq->next = NULL; 11 } 12 13 int getlen(slink*sq) 14 { 15 int i = 0; 16 slink*p = sq->next;//sq->next; 17 while (p != NULL) 18 { 19 i++; 20 p = p->next; 21 } 22 return i; 23 } 24 25 slink*getelem(slink*sq, int i) 26 { 27 int j = 1; 28 slink*p = sq->next; 29 if (i<1 || i>sq.getlen(sq)) 30 return NULL; 31 while (j < i) 32 { 33 p = p->next; 34 j + +; 35 } 36 return p;//返回第二个结点的指针 37 } 38 39 slink*locate(slink*sq, ElemType x) 40 { 41 slink*p = sq->next; 42 while (p != null && p->data != x) 43 p = p->next; 44 return p; 45 } 46 47 int inselem(slink*sq, ElemType x, int i) 48 { 49 int j = 1; 50 slink*p = sq, *s; 51 s = (slink*) malloc(sizeof(slink)); 52 s->data = x; 53 s->next = NULL; 54 if (i<1 || i>getlen(sq) + 1) 55 return 0; 56 while (j < i)//查找第i-1个结点,用p指向它,sq应该是头结点 57 { 58 p = p->next; 59 j++; 60 } 61 s->next = p->next; 62 p->next = s; 63 return 1; 64 } 65 66 int delelem(slink *sq, int i) 67 { 68 int j = 1; 69 slink*p = sq, *q; 70 if (i<1 || i>getlen(sq)) 71 return 0; 72 while (j < i) 73 { 74 p = p->next; 75 j++; 76 } 77 q = p - next;//q=p->next;p->next=q->next; 78 p->next = q->next; 79 free(p); 80 return 1; 81 } 82 83 void displist(slink*sq) 84 { 85 slink*p = sq->next; 86 while (p != NULL) 87 { 88 cout << p->data << " "; 89 p = p->next; 90 } 91 cout << endl; 92 } 93 94 void move(sqlist A)//从左向右找A.data[i],从右向左找A.data[j],直到i>j为止,巧妙的方式 95 { 96 int i = 0, j = A.len - 1, k;//线性表,每个元素都是整数,用最少时间把所有为负数的元素移到全为正数的前面 97 ElemType temp; 98 while (i <= j) 99 { 100 while (A.data[i] <= 0) 101 i++;//统计负数的个数 102 while (A.data[j] >= 0) 103 j--;//统计正数的个数 104 if (i < j) 105 { 106 temp = A.data[i]; 107 A.data[i] = A.data[j]; 108 A.data[j] = temp; 109 } 110 } 111 } 112 113 void sort(sqlist s.sqlist t) 114 { 115 sqlist r; 116 int i = 0, j = 0, k = 0; 117 while (i < s.len && j < t.len) 118 { 119 if (s.data[i] < t.data[j]) 120 { 121 r.data[k] = s.data[i]; 122 i++; 123 k++; 124 } 125 else if (s.data[i] > t.data[j]) 126 { 127 r.data[k] = t.data[j]; 128 j++; 129 k++; 130 } 131 else 132 { 133 r.data[k] = s.data[i]; 134 i++; 135 k++; 136 r.data[k] = t.data[j]; 137 j++; 138 k++; 139 } 140 } 141 while (i < s.len) 142 { 143 r.data[k] = s.data[i]; 144 i++; 145 k++; 146 } 147 while (j < t.len) 148 { 149 r.data[k] = t.data[k]; 150 i++; 151 k++; 152 } 153 } 154 155 struct pointer 156 { 157 int number; 158 struct pointer*next; 159 }; 160 void combine(struct pointer*&f, struct pointer*g) 161 { 162 struct pointer *h, *p; 163 h = (struct pointer*)malloc(sizeof(struct pointer)); 164 h->next = NULL; 165 p = h; 166 while (f != NULL && g != NULL) 167 { 168 if (f->number >= g->number) 169 { 170 p->next = f; 171 p = p->next; 172 f = f->next; 173 } 174 else 175 { 176 p->next = g; 177 p = p->nex; 178 g = g->next; 179 } 180 if (f == NULL) 181 p->next = g; 182 if (g == NULL) 183 p->next = f; 184 f = h->next;//f指向合并链表的开始结点,非头结点 185 free(h); 186 } 187 } 188 189 const int StackSize = 100; 190 typedef struct sqst 191 { 192 ElemType data[StackSize]; 193 int top; 194 }sqstack; 195 196 void initstack(sqstack *&sq) 197 { 198 sq = (sqstack*) malloc(sizeof(sqstack)); 199 sq->top = -1; 200 } 201 202 int push(sqstack *sq, ElemType x) 203 { 204 if (sq->top == StackSize - 1) 205 return 0; 206 else 207 { 208 sq->top++; 209 sq->data[sq->top] = x; 210 return 1; 211 } 212 } 213 214 int pop(sqstack *sq, ElemType &x) 215 { 216 if (sq->top == -1)//栈空 217 return 0; 218 else 219 { 220 x = sq->data[sq->top]; 221 sq->top--; 222 return 1; 223 } 224 } 225 226 int gettop(sqstack*sq, ElemType &x) 227 { 228 if (sq->top == -1) 229 return 0; 230 else 231 { 232 x = sq->data[sq->top]; 233 return 1; 234 } 235 } 236 237 int empty(sqstack *sq) 238 { 239 if (sq->top == -1) 240 return 1; 241 else 242 { 243 return 0; 244 } 245 } 246 247 typedef struct stnode 248 { 249 ElemType data; 250 struct stnode*next; 251 }lkstack; 252 lkstack *ls; 253 254 void initstack(lkstack*&ls) 255 { 256 lkstack *p; 257 p = (lkstack*)malloc(sizeof(lkstack)); 258 p->data = x; 259 p->next = ls;//创建结点 260 ls = p; 261 } 262 263 int pop(lkstack *ls, ElemType&x) 264 { 265 lkstack *p; 266 if (ls == NULL)//栈空,下溢出 267 return 0; 268 else 269 { 270 p = ls; 271 x = p->data; 272 ls = p->next; 273 free(p); 274 return 1; 275 } 276 } 277 278 int gettop(lkstack *ls, ElemType &x) 279 { 280 if (ls == NULL) 281 return 0; 282 else 283 { 284 x = ls->data; 285 return 1; 286 } 287 } 288 289 int empty(lkstack *ls) 290 { 291 if (ls = NULL) 292 return 1; 293 else 294 return 0; 295 } 296 297 const int QueueSize = 20; 298 typedef struct sqqueue 299 { 300 ElemType data[QueueSize]; 301 int front, rear; 302 }squeue; 303 squeue *qu; 304 305 void initqueue(squeue*&qu) 306 { 307 qu = (squeue*) malloc(sizeof(squeue)); 308 qu->rear = qu->front = 0; 309 } 310 311 int enqueue(squeue*sq, ElemType x) 312 { 313 if ((sq->rear + 1)%QueueSize == sq->front) 314 return 0; 315 sq->rear = (sq->rear + 1)%QueueSize;//队尾指针进1 316 sq->data[sq->rear] = x; 317 return 1; 318 } 319 320 #define MAXSIZE 100 321 typedef struct 322 { 323 ElemType elem[MAXSIZE]; 324 int top; 325 }SqStack; 326 327 void InitStack(SqStack *s) 328 { 329 s->top = -1; 330 } 331 332 int StackEmpty(SqStack *s) 333 { 334 if (s.top == -1) 335 return 1; 336 else 337 return 0; 338 } 339 340 void Push(SqStack *s, ElemType e) 341 { 342 if(s->top = MAXSIZE - 1) 343 { 344 printf("StackSize is full "); 345 return; 346 } 347 s->top++; 348 s->elem[s->top] = e; 349 } 350 351 void Pop(SqStack *s, ElemType *e) 352 { 353 if (s->top == -1) 354 { 355 printf("Stack is empty"); 356 return; 357 } 358 *e = s->elem[s->top]; 359 s->top--; 360 } 361 362 void GetTop(SqStack s, ElemType *e) 363 { 364 if (s.top == -1) 365 { 366 printf("The Stack is Empty!"); 367 return; 368 } 369 *e = s.elem[s->top]; 370 } 371 372 typedef struct node 373 { 374 ElemType data; 375 struct node*next; 376 }Node,*LinkStack; 377 void Push(LinkStack*s, ElemType e) 378 { 379 Node*p; 380 p = (Node*) malloc(sizeof(Node)); 381 p->data = e; 382 p->next = *s; 383 *s = p; 384 } 385 386 void Pop(LinkStack*s, ElemType*e) 387 { 388 Node*p; 389 if (*s = NULL) 390 { 391 printf("stack is empty! "); 392 return; 393 } 394 *e = (*s)->data; 395 p = *s; 396 (*s) = (*s)->next; 397 free(p); 398 }