线性表顺序结构基本操作的实现

2018-10-31-15:02:46

  1 /**********************************************
  2     main函数内容:
  3          1. 从键盘输入 n 个整数,填入顺序表;
  4          2. 输出 n 个整数至屏幕;
  5          3. 查找值为某个整数的元素的位序;
  6          4. 在顺序表任一位置插入一个元素;
  7          5. 输出修改后的表至屏幕;
  8          6. 删除任一位置的元素;
  9          7. 输出修改后的表至屏幕;
 10          8. 逆序排列顺序表中的数据元素,并输出至屏幕。
 11 **********************************************/
 12 #include <cstdio>
 13 #include <iostream>
 14 #include <cstdlib>
 15 using namespace std;
 16 
 17 #define OK 1
 18 #define ERROR 0
 19 #define true 1
 20 #define false 0
 21 #define success 1
 22 #define fail 0
 23 #define Overflow -2
 24 #define Initsize 100
 25 #define Growsize 40
 26 
 27 typedef int Elemtype;
 28 typedef int Status;
 29 typedef struct list{
 30     Elemtype*elem;
 31     int Length;
 32     int Listsize;
 33 }Sqlist;
 34 
 35 Status Initlist(Sqlist &L);
 36 Status InsertElem(Sqlist &L,Elemtype e,int aidelem_locate);
 37 Status Deleteelem(Sqlist &L,Elemtype*e,int aidelem_locate);
 38 int Listlength(Sqlist L);
 39 Status Listempty(Sqlist L);
 40 Status ElemLocate(Sqlist L,Elemtype e,int*aidelem_locate);
 41 Status Showlist(Sqlist L);
 42 Status Reservelist(Sqlist L);
 43 int main()
 44 {
 45     Sqlist L;
 46     Initlist(L);
 47 
 48     int idx1,Maxnumber;
 49     scanf("%d",&Maxnumber);
 50     Elemtype*in;
 51     for(in=L.elem,idx1=1;idx1<=Maxnumber;in++,idx1++){
 52       scanf("%d",in);
 53       InsertElem(L,*in,idx1);
 54     }
 55 
 56     Showlist(L);
 57 
 58     int idx2,elem1;
 59     scanf("%d",&elem1);
 60     ElemLocate(L,elem1,&idx2);
 61     printf("%d\n",idx2);
 62 
 63     int idx3,elem2;
 64     scanf("%d %d",&idx3,&elem2);
 65     InsertElem(L,elem2,idx3);
 66     Showlist(L);
 67 
 68     int idx4,elem3;
 69     scanf("%d",&idx4);
 70     Deleteelem(L,&elem3,idx4);
 71     Showlist(L);
 72 
 73     Reservelist(L);
 74     Showlist(L);
 75       return 0;
 76 }
 77 
 78 Status Initlist(Sqlist &L){
 79     L.elem=(Elemtype*)malloc(Initsize*sizeof(Elemtype));
 80     if(!L.elem) exit(Overflow);
 81     L.Length=0;
 82     L.Listsize=Initsize;
 83     return OK;
 84 }
 85 
 86 Status InsertElem(Sqlist &L,Elemtype e,int aidelem_locate){
 87     if(L.Length>=0){
 88       int i;
 89       L.Length+=1;
 90       if(L.Length>=L.Listsize){
 91         L.elem=(Elemtype*)realloc(L.elem,(L.Listsize+Growsize)*sizeof(Elemtype));
 92         L.Listsize+=Growsize;
 93       }
 94       if(aidelem_locate<0||aidelem_locate>L.Length+1)
 95         return ERROR;
 96       for(i=L.Length;i>=aidelem_locate;i--)
 97         L.elem[i]=L.elem[i-1];
 98       L.elem[i+1]=e;
 99       return success;
100     }
101     return fail;
102 }
103 
104 Status Deleteelem(Sqlist &L,Elemtype*e,int aidelem_locate){
105     if(L.Length){
106       *e=L.elem[aidelem_locate];
107       for(int i=aidelem_locate;i<L.Length;i++)
108         L.elem[i]=L.elem[i+1];
109           L.Length-=1;
110       return success;
111     }
112     return fail;
113 }
114 
115 Status ElemLocate(Sqlist L,Elemtype e,int*aidelem_locate){
116     if(L.Length){
117       for(int i=0;i<=L.Length;i++)
118         if(L.elem[i]==e){
119           *aidelem_locate=i;
120           return OK;
121         }
122     }
123     return fail;
124 }
125 
126 Status Showlist(Sqlist L){
127     if(L.Length){
128       int flag;
129       Elemtype*evel;
130       for(evel=L.elem,flag=1;flag<=L.Length;evel++,flag++){
131         printf("%d\t",*evel);
132         if(flag%5==0)
133           printf("\n");
134       }
135       if(L.Length%5!=0)
136       printf("\n");
137       return true;
138     }
139     return false;
140 }
141 
142 Status Reservelist(Sqlist L){
143     if(L.Length){
144       for(int i=0;i<L.Length/2;i++){
145         Elemtype replace=L.elem[i];
146         L.elem[i]=L.elem[L.Length-i-1];
147         L.elem[L.Length-i-1]=replace;
148       }
149       return true;
150     }
151     return false;
152 }
153 /***************************
154     Author:CRUEL_KING
155     Time:2018/10/31
156     Program name:线性表顺序储存结构的基本操作实现.cpp
157 ***************************/

 

posted @ 2018-10-31 15:03  Cruel_King  阅读(647)  评论(0编辑  收藏  举报