- #include<iostream>
- #include<malloc.h>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define LIST_INIT_SIZE 10
- #define LISTINCREMENT 10
- typedef int Status;
- typedef int ElemType;
- typedef Status(*compare)(ElemType,ElemType);
- typedef struct{
- ElemType *elem;
- int length;
- int listsize;
- }SqList;
- Status InitList_sq(SqList &L) //初始化顺序表
- {
- L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
- if(!L.elem) exit(OVERFLOW);
- L.length=0;
- L.listsize=LIST_INIT_SIZE;
- return OK;
- }
- Status InsertList_sq(SqList &L,int i,ElemType e) //插入
- {
- if(i<1||i>L.length+1) return ERROR;
- if(L.length==L.listsize)
- {
- ElemType* newbase=(ElemType*)realloc(L.elem,
- (L.listsize+LISTINCREMENT)*sizeof(ElemType));
- if(!newbase) exit(OVERFLOW);
- L.elem=newbase;
- L.listsize+=LISTINCREMENT;
- }
- for(int j=L.length;j>=i;j--)
- *(L.elem+j)=*(L.elem+j-1);
- *(L.elem+i-1)=e;
- L.length++;
- return OK;
- }
- Status DeleteList_sq(SqList &L,int i,ElemType &e) //删除
- {
- if(i<1||i>L.length) return ERROR;
- e=*(L.elem+i-1);
- for(int j=i;j<L.length;j++)
- *(L.elem+j-1)=*(L.elem+j);
- --L.length;
- return OK;}
- int LocateElem_sq(SqList &L,ElemType e,
- Status(*compare)(ElemType a,ElemType b)){
- //返回顺序表中第一个和e满足compare的元素位置
- int i;
- for(i=0;i<L.length&&!((*compare)(L.elem[i],e));i++);
- if(i==L.length)
- return 0;
- else
- return i+1;
- }
- void MergeList_sq(SqList &La,SqList &Lb,SqList &Lc){
- // 已知单链线性表La和Lb的元素按值非递减排列。
- // 归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列。
- Lc.listsize=Lc.length=La.length+Lb.length;
- ElemType *pa=La.elem,*pb=Lb.elem,
- *pc=Lc.elem=(ElemType*)malloc(Lc.listsize*sizeof(ElemType));
- if(!Lc.elem) exit(OVERFLOW);
- ElemType *pa_last=La.elem+La.length-1,
- *pb_last=Lb.elem+Lb.length-1;
- while(pa<=pa_last&&pb<=pb_last)
- {
- if(*pa<=*pb) *pc++=*pa++;
- else *pc++=*pb++;
- }
- while(pa<=pa_last) *pc++=*pa++;
- while(pb<=pb_last) *pc++=*pb++;
- }
- void sort(SqList &L)
- {
- for(int i=0;i<L.length-1;i++)
- {int k=i;
- for(int j=i+1;j<L.length;j++)
- if(L.elem[j]<L.elem[k]) k=j;
- if(k!=i) {
- ElemType t=L.elem[i];
- L.elem[i]=L.elem[k];
- L.elem[k]=t;}
- }
- }
- void OutputList_sq(SqList L) //输出
- {
- for(int i=0;i<L.length;i++)
- cout<<L.elem[i]<<" ";
- cout<<endl;
- }
- Status Equal(ElemType a,ElemType b) //判断是否相等
- {
- if(a==b) return true;
- else return false;}
- compare com=Equal;
- void minus(SqList &La,SqList &Lb) //La-Lb
- {
- ElemType *pb=Lb.elem;
- int i=0;
- ElemType e;
- while(i<Lb.length)
- {if(int j=LocateElem_sq(La,*pb++,*com))
- DeleteList_sq(La,j,e);
- i++; }
- }
- int main()
- {
- void minus(SqList &La,SqList &Lb);
- SqList La,Lb,Lc;
- int num_a,num_b,pos_in,pos_del;
- ElemType insert_elem,delete_elem,search;
- InitList_sq(La);
- while(cin>>num_a&&num_a>La.listsize);
- for(int i=0;i<num_a;i++)
- cin>>La.elem[i];
- La.length=num_a;
- sort(La);
- OutputList_sq(La);
- InitList_sq(Lb);
- while(cin>>num_b&&num_b>Lb.listsize);
- for(int i=0;i<num_b;i++)
- cin>>Lb.elem[i];
- Lb.length=num_b;
- sort(Lb);
- OutputList_sq(Lb);
- minus(La,Lb);
- OutputList_sq(La);
- MergeList_sq(La,Lb,Lc);
- OutputList_sq(Lc);
- cout<<"please input the position and the element you want to insert:";
- cin>>pos_in>>insert_elem;
- if(InsertList_sq(Lc,pos_in,insert_elem)) cout<<"insert succeed"<<endl;
- OutputList_sq(Lc);
- cout<<"please input the position you want to delete:";
- cin>>pos_del;
- if(DeleteList_sq(Lc,pos_del,delete_elem))
- cout<<delete_elem<<"is deleted"<<endl;
- OutputList_sq(Lc);
- return 0;}
- 输入输出:
- 4
- 3 5 2 1
- 1 2 3 5
- 5
- 2 5 7 6 9
- 2 5 6 7 9
- 1 3
- 1 2 3 5 6 7 9
- please input the position and the element you want to insert:3
- 8
- insert succeed
- 1 2 8 3 5 6 7 9
- please input the position you want to delete:5
- 5is deleted
- 1 2 8 3 6 7 9
- 请按任意键继续. . .
顺序表
最新推荐文章于 2024-10-09 10:55:47 发布