顺序表操作

来源:互联网 发布:开单软件 编辑:程序博客网 时间:2024/04/28 17:17
#include <stdio.h>#include <stdbool.h>#include <stdlib.h>#define SIZE 10typedef int datatype;typedef struct sequence{datatype data[SIZE];int last;}sqlist, *sqlink;void init_sq(sqlink *p2L){*p2L = malloc(sizeof(sqlist));(*p2L)->last = -1;}bool is_full(sqlink L){return (L->last >= SIZE-1);}void move(sqlink tmp,int i,int x){int m=tmp->last;for(;m>=i;m--)tmp->data[m+1]=tmp->data[m];tmp->data[m+1]=x;(tmp->last)++;}bool insert(sqlink L, datatype x){if(is_full(L))return false;sqlink tmp=L;              if(tmp->last==-1){L->last++;tmp->data[tmp->last]=x;return true;}else{int i=0;while(i<=tmp->last){if(x<tmp->data[i]){move(L,i,x);return true;;}elsei++;}(tmp->last)++;tmp->data[tmp->last]=x;return true;}}void show(sqlink L){int i;for(i=0; i<=L->last; i++){printf("%d\t", L->data[i]);}printf("\n");}bool is_empty(sqlink L){return L->last == -1;}bool delete(sqlink L, datatype x){if(is_empty(L))return false;int i;for(i=0; i<=L->last; i++){if(L->data[i] == x)break;}if(i <= L->last){int j;for(j=i; j<=L->last; j++){L->data[j] = L->data[j+1];}L->last--;return true;}elsereturn false;}int main(void){sqlink L;init_sq(&L);puts("please in put data to sort");int tmp, ret;while(1){ret = scanf("%d", &tmp);if(ret != 1)break;if(tmp >= 0){if(!insert(L, tmp))fprintf(stderr, "insertion falied!\n");elseshow(L);}else if(!delete(L, -tmp))fprintf(stderr, "delete falied!\n");elseshow(L);}printf("\n%d\n",L->last);return 0;}