顺序表

来源:互联网 发布:制作小游戏的软件 编辑:程序博客网 时间:2024/04/20 22:29
  1. #include<iostream>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2
  8. #define LIST_INIT_SIZE 10
  9. #define LISTINCREMENT 10
  10. typedef int Status;
  11. typedef int ElemType; 
  12. typedef Status(*compare)(ElemType,ElemType);
  13. typedef struct{
  14. ElemType *elem;
  15. int length;
  16. int listsize;
  17. }SqList;
  18. Status InitList_sq(SqList &L)            //初始化顺序表
  19. {
  20. L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  21. if(!L.elem) exit(OVERFLOW);
  22. L.length=0;
  23. L.listsize=LIST_INIT_SIZE;
  24. return OK;
  25. }
  26. Status InsertList_sq(SqList &L,int i,ElemType e)          //插入 
  27. {
  28.    if(i<1||i>L.length+1) return ERROR;    
  29.    if(L.length==L.listsize) 
  30.    {
  31.       ElemType* newbase=(ElemType*)realloc(L.elem,
  32.                 (L.listsize+LISTINCREMENT)*sizeof(ElemType));
  33.       if(!newbase) exit(OVERFLOW);
  34.       L.elem=newbase;
  35.       L.listsize+=LISTINCREMENT;
  36.    }
  37.    for(int j=L.length;j>=i;j--)
  38.    *(L.elem+j)=*(L.elem+j-1);
  39.    *(L.elem+i-1)=e;
  40.    L.length++;
  41.    return OK;
  42. }
  43. Status DeleteList_sq(SqList &L,int i,ElemType &e)          //删除 
  44. {
  45.    if(i<1||i>L.length) return ERROR;
  46.    e=*(L.elem+i-1);
  47.    for(int j=i;j<L.length;j++)
  48.    *(L.elem+j-1)=*(L.elem+j);
  49.     --L.length;
  50.     return OK;}
  51.     
  52. int LocateElem_sq(SqList &L,ElemType e,
  53.               Status(*compare)(ElemType a,ElemType b)){
  54. //返回顺序表中第一个和e满足compare的元素位置 
  55.    int i;
  56.    for(i=0;i<L.length&&!((*compare)(L.elem[i],e));i++);
  57.    if(i==L.length) 
  58.         return 0;
  59.    else 
  60.         return i+1;
  61. }
  62. void MergeList_sq(SqList &La,SqList &Lb,SqList &Lc){
  63. // 已知单链线性表La和Lb的元素按值非递减排列。
  64. // 归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列。
  65.     Lc.listsize=Lc.length=La.length+Lb.length;
  66.     ElemType *pa=La.elem,*pb=Lb.elem,
  67.     *pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
  68.     if(!Lc.elem) exit(OVERFLOW);
  69.     ElemType *pa_last=La.elem+La.length-1,
  70.     *pb_last=Lb.elem+Lb.length-1;
  71.     while(pa<=pa_last&&pb<=pb_last)
  72.     {
  73.      if(*pa<=*pb) *pc++=*pa++;
  74.      else         *pc++=*pb++;
  75.      }
  76.     while(pa<=pa_last) *pc++=*pa++;
  77.     while(pb<=pb_last) *pc++=*pb++;
  78. }
  79. void sort(SqList &L)
  80. {
  81.      for(int i=0;i<L.length-1;i++)
  82.      {int k=i;
  83.      for(int j=i+1;j<L.length;j++)
  84.      if(L.elem[j]<L.elem[k]) k=j;
  85.      if(k!=i) {
  86.               ElemType t=L.elem[i];
  87.               L.elem[i]=L.elem[k];
  88.               L.elem[k]=t;}
  89.      }
  90. }
  91. void OutputList_sq(SqList L)     //输出 
  92. {
  93. for(int i=0;i<L.length;i++)
  94.     cout<<L.elem[i]<<" ";
  95. cout<<endl;
  96. }
  97. Status Equal(ElemType a,ElemType b)             //判断是否相等 
  98. {
  99.        if(a==b) return true;
  100.        else return false;}
  101. compare com=Equal;
  102. void minus(SqList &La,SqList &Lb)             //La-Lb
  103. {
  104.    ElemType *pb=Lb.elem;
  105.    int i=0;
  106.    ElemType e;
  107.    while(i<Lb.length)
  108.    {if(int j=LocateElem_sq(La,*pb++,*com))
  109.        DeleteList_sq(La,j,e);
  110.     i++; }
  111. }
  112. int main()
  113. {
  114. void minus(SqList &La,SqList &Lb);
  115. SqList La,Lb,Lc;
  116. int num_a,num_b,pos_in,pos_del;
  117. ElemType insert_elem,delete_elem,search;
  118. InitList_sq(La);
  119. while(cin>>num_a&&num_a>La.listsize);
  120. for(int i=0;i<num_a;i++)
  121. cin>>La.elem[i];
  122. La.length=num_a;
  123. sort(La);
  124. OutputList_sq(La);
  125. InitList_sq(Lb);
  126. while(cin>>num_b&&num_b>Lb.listsize);
  127. for(int i=0;i<num_b;i++)
  128. cin>>Lb.elem[i];
  129. Lb.length=num_b;
  130. sort(Lb);
  131. OutputList_sq(Lb);
  132. minus(La,Lb);
  133. OutputList_sq(La);
  134. MergeList_sq(La,Lb,Lc);
  135. OutputList_sq(Lc);
  136. cout<<"please input the position and the element you want to insert:";
  137. cin>>pos_in>>insert_elem;
  138. if(InsertList_sq(Lc,pos_in,insert_elem)) cout<<"insert succeed"<<endl;
  139. OutputList_sq(Lc);
  140. cout<<"please input the position you want to delete:";
  141. cin>>pos_del;
  142. if(DeleteList_sq(Lc,pos_del,delete_elem)) 
  143. cout<<delete_elem<<"is deleted"<<endl;
  144. OutputList_sq(Lc);
  145. return 0;}
  146. 输入输出:
  147. 4
  148. 3 5 2 1
  149. 1 2 3 5
  150. 5
  151. 2 5 7 6 9
  152. 2 5 6 7 9
  153. 1 3
  154. 1 2 3 5 6 7 9
  155. please input the position and the element you want to insert:3
  156. 8
  157. insert succeed
  158. 1 2 8 3 5 6 7 9
  159. please input the position you want to delete:5
  160. 5is deleted
  161. 1 2 8 3 6 7 9
  162. 请按任意键继续. . .
原创粉丝点击