#include<iostream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; typedef int ElemType; #define MAXSIZE 100 typedef struct { ElemType *elem; int lenth; }Sqlist; typedef struct Lnode { ElemType data; Lnode *next; }*Linklist; Status InitList_Sq(Sqlist &L)//有序表的初始化 { L.elem=new ElemType[MAXSIZE]; if(!L.elem) exit(OVERFLOW); L.lenth=0; return OK; } void InputList_Sq(Sqlist &L)//顺序表输入数据 { int n; cout<<"请输入要输入的元素个数:"; cin>>n; cout<<"请输入每个元素的值:"; for(int i=0;i<n;i++) cin>>L.elem[i]; L.lenth=n; cout<<"输入成功!"<<endl<<endl; } Status Erfen(Sqlist L,ElemType e)//二分查找要输入或要删除的元素下标时间复杂度为O(logn); { int mid,l=0,r=L.lenth-1; while(l<r) { mid=(l+r)/2; if(L.elem[mid]>e) r=mid-1; else l=mid+1; } return l; } bool InsertList_Sq(Sqlist &L,ElemType e)//顺序表的插入 { int i=Erfen(L,e); if(L.elem[i]<e) i++; for(int j=L.lenth;j>=i+1;j--) L.elem[j]=L.elem[j-1]; L.elem[i]=e; L.lenth++; return OK; } bool DeleteList_Sq(Sqlist &L,ElemType e)//顺序表的删除 { int i=Erfen(L,e); if(L.elem[i-1]==e) i--; else if(L.elem[i+1]==e) i++; if(L.elem[i]==e) { for(int j=i;j<L.lenth;j++) L.elem[j]=L.elem[j+1]; L.lenth--; return OK; } return ERROR; } Status Init(Linklist &l)//链表的初始化 { l=new Lnode; l->next=NULL; return OK; } void InputLink(Linklist &l)//链表的输入数据 { Lnode *p,*r; int n; cout<<"请输入要输入的元素个数:"; cin>>n; cout<<"请输入每个元素的值:"; p=l->next; r=l; for(int i=0;i<n;i++) { p=new Lnode; cin>>p->data; p->next=NULL; r->next=p; r=p; } } Status Insertlink(Linklist &l,ElemType n)//链表的插入 { Lnode *p; p=l->next; while(p&&p->data<n) p=p->next; if(!p) { p=new Lnode; p->data=n; p->next=NULL; } else { Lnode *r; r=new Lnode; r->data=p->data; r->next=p->next; p->next=r; p->data=n; } return OK; } Status Delete(Linklist &l,ElemType e)//链表的删除 { Lnode *p=l,*r; p=l->next; while(p->data<e&&p) p=p->next; if(!p||p->data>e) return ERROR; else { r=p->next; p->data=r->data; p->next=r->next; delete r; return OK; } } void output(Linklist &l)//链表的输出 { Lnode *p; p=l->next; while(p) { cout<<p->data<<" "; p=p->next; } cout<<endl<<endl; } void output1(Sqlist L)//顺序表的输出 { for(int i=0;i<L.lenth;i++) cout<<L.elem[i]<<" "; cout<<endl<<endl; } void convert(Linklist &l)//链表的原地逆置 { Lnode *p,*r; r=l->next; l->next=NULL;//重点易忽略处 while(r) { p=r->next; r->next=l->next; l->next=r; r=p; } } int main() { Sqlist L; Lnode *l; ElemType n,i,e,temp; int choose; cout<<"******************线性有序表的基本操作与实现******************"<<endl; cout<<" 1.创建有序顺序表"<<endl; cout<<" 2.有序顺序表输入数据"<<endl; cout<<" 3.有序顺序表插入"<<endl; cout<<" 4.有序顺序表删除"<<endl; cout<<" 5.有序顺序表输出"<<endl; cout<<" 6.创建有序链表"<<endl; cout<<" 7.有序链表输入数据"<<endl; cout<<" 8.有序链表插入"<<endl; cout<<" 9.有序链表删除"<<endl; cout<<" 10.有序链表的输出"<<endl; cout<<" 0.退出"<<endl; cout<<"************************我是分界线************************"<<endl<<endl; choose=-1; while(choose) { cout<<"请输入你的选择:"; cin>>choose; switch(choose) { case 0:break; case 1: if(InitList_Sq(L)) cout<<"创建有序顺序表成功!"<<endl<<endl; else cout<<"创建有序顺序表失败!"<<endl<<endl; break; case 2: InputList_Sq(L); break; case 3: cout<<"请输入要插入的数据:"; cin>>e; if(InsertList_Sq(L,e)) cout<<"插入成功!"<<endl<<endl; else cout<<"插入失败!"<<endl<<endl; break; case 4: cout<<"请输入要删除的数:"; cin>>e; if(DeleteList_Sq(L,e)) cout<<"删除成功,你删除的数是"<<e<<endl<<endl; else cout<<"删除失败!"<<endl<<endl; break; case 5: cout<<"此时的顺序表中的数据为:"; output1(L); break; case 6: if(Init(l)) cout<<"创建链表成功!"<<endl<<endl; break; case 7: InputLink(l); cout<<"输入数据成功!"<<endl<<endl; break; case 8: cout<<"请输入要插入的元素:"; cin>>n; if(Insertlink(l,n)) cout<<"插入成功!"<<endl<<endl; else cout<<"插入失败!"<<endl<<endl; break; case 9: cout<<"请输入要删除的元素:"; cin>>e; if(Delete(l,e)) cout<<"删除成功,删除的元素是"<<e<<endl<<endl; else cout<<"删除失败!"<<endl<<endl; break; case 10: cout<<"此时的链表中的数据为:"<<endl; output(l); break; default: cout<<"无效输入!"<<endl<<endl; break; } } return 0; }