1. 程式人生 > >線性表基本操作的實現(合並)

線性表基本操作的實現(合並)

typedef real 不變 names base ++ 元素 一個 修改元素

//實現順序表的建立、初始化、插入、刪除、修改、普通合並、有序合並 
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
ElemType *newbase; 
//順序表結構描述
typedef struct{ 
    ElemType 
*elem; //基地址指針 int length; //順序表長度,數據元素個數 int ListSize; //存儲空間 }SqList; //順序表初始化 int InitSqList(SqList &L){ L.elem=(ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);//分配存儲空間 if(!L.elem) exit(OVERFLOW);//分配存儲空間失敗 L.length=0; //初始長度為0 L.ListSize=LIST_INIT_SIZE; //
初始空間 return OK; } //創建順序表 void CreatSqList(SqList &L){ int i,n; cout<<"請輸入順序表的元素個數:"; cin>>n; for(i=0;i<n;i++){ cout<<"請輸入第 "<<(i+1)<<" 個元素:"; cin>>L.elem[i]; L.length++; } } //順序表的顯示 void ShowSqList(SqList &L){ cout
<<endl; for(int i=0;i<L.length;i++) cout<<L.elem[i]<<" "; cout<<endl; } //順序表的插入 int InsertSqList(SqList &L,int pos,ElemType elem){//在順序表中的pos位置插入elem元素 if((pos-1)<0&&pos>L.length+1){//判斷位置是否合法 cout<<"您所插入的位置不合法!"<<endl; return ERROR; } if(L.length>=L.ListSize){/* realloc可以對給定的指針所指的空間進行擴大或者縮小,無論是擴張或是縮小,原有內存的中內容將保持不變。當然,對於縮小,則被縮小的那一部分的內容會丟失。realloc 並不保證調整後的內存空間和原來的內存空間保持同一內存地址。相反,realloc 返回的指針很可能指向一個新的地址。所以在代碼中,我們必須將realloc返回的值,重新賦值給newbase*/ newbase=(ElemType *)realloc(L.elem,(L.ListSize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW);//內存分配失敗 L.elem=newbase; L.ListSize+=LISTINCREMENT; } for(int i=L.length-1;i>=pos-1;i--){ L.elem[i+1]=L.elem[i]; } L.elem[pos-1]=elem; L.length++;//表長加1 return OK; } //順序表的刪除 int DeleteSqList(SqList &L,int pos){//順序表中刪除pos位置的元素 if(pos<1||pos>L.length){ cout<<"刪除的位置不合法!"<<endl; return ERROR; } for(int i=pos-1;i<L.length;i++){ L.elem[i]=L.elem[i+1]; } L.length--;//表長減1 return OK; } //修改順序表 int UpdateSqList(SqList &L,int pos,ElemType elem){//在順序表pos位置修改元素 if(pos<1&&pos>L.length){ cout<<"修改的位置不合法!"<<endl; return ERROR; } L.elem[pos-1]=elem; return 0; } //順序表的合並 void CombineSqList(SqList &La,SqList &Lb){ int i,j; for(i=0;i<Lb.length;i++){ int cout=0; for(j=0;j<La.length;j++){ if(La.elem[j]==Lb.elem[j]) cout++; } if(cout==0) La.elem[La.length++]=Lb.elem[i]; } } //順序表的有序合並,有序合並的前提,兩個順序表已經排序好的 void CombineSq(SqList &LA,SqList &LB,SqList &LC){//LA,LB是遞增排序的 ElemType *pa,*pb,*pc,*pa_last,*pb_last; LC.length=LA.length+LB.length;//新表的長度為兩個表的長度之和 LC.elem=new ElemType[LC.length];//分配空間 //LC.elem=(ElemType *)malloc(LC.length*sizeof(ElemType)); pc=LC.elem;//分別指向第一個元素 pa=LA.elem; pb=LB.elem; 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++; } int main(){ SqList L; InitSqList(L); CreatSqList(L); ShowSqList(L); int num=0; cout<<endl<<"1、插入"<<endl; cout<<"2、刪除"<<endl; cout<<"3、修改"<<endl; cout<<"4、順序表普通合並"<<endl; cout<<"5、順序表有序合並"<<endl<<endl; cout<<"請選擇需要進行的操作:"; cin>>num; if(num<1||num>5){ cout<<"你選擇的操作不存在,請重新輸入:"; cin>>num; } switch(num){ case 1:{ int m,n; cout<<"請輸入你要插入的位置:"; cin>>m; cout<<"請輸入你要插入的元素:"; cin>>n; InsertSqList(L,m,n); ShowSqList(L); break; } case 2:{ int m; cout<<"請輸入你要刪除的位置:"; cin>>m; DeleteSqList(L,m); ShowSqList(L); break; } case 3:{ int m,n; cout<<"請輸入你要修改的位置:"; cin>>m; cout<<"請輸入你要修改的元素:"; cin>>n; UpdateSqList(L,m,n); ShowSqList(L); break; } case 4:{ SqList Lb; InitSqList(Lb); cout<<"請創建你要合並的順序表Lb:"<<endl; int n; cout<<"請輸入Lb的元素個數:"; cin>>n; cout<<"你所輸入的"<<n<<"個元素分別為:"; for(int i=0;i<n;i++){ cin>>Lb.elem[i]; Lb.length++; } CombineSqList(L,Lb); cout<<"合並後:"; ShowSqList(L); break; } case 5:{ SqList Lb,Lc; InitSqList(Lb); InitSqList(Lc); cout<<"請創建你要合並的順序表Lb:"<<endl; int n; cout<<"請輸入Lb的元素個數:"; cin>>n; cout<<"你所輸入的"<<n<<"個元素分別為:"; for(int i=0;i<n;i++){ cin>>Lb.elem[i]; Lb.length++; } CombineSq(L,Lb,Lc); cout<<"合並後:"; ShowSqList(Lc); break; } } return 0; }

線性表基本操作的實現(合並)