要求: 1.编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
2.采用顺序表结构编程实现:两个集合的运算:交集/并集/差集。
操作实例: 1.插入数据(位置, 数据),要测插入位置不合法的情况(0,1)、(2,1),正确插入4个数据(1,2)、(1,1)、(3,3); 2.显示顺序表中的数据,屏幕输出1, 2, 3; 3.判空,屏幕输出顺便表非空; 4. 顺便表长度,屏幕输出3; 5.获取指定位置元素,要测指定位置在【1,3】范围之外的情况和之内的情况; 6.定位,输入:4, 输出:不存在,输入2,输出位置为2; 7.求直接前驱,要测求第一个元素的前驱、不存在顺序表中的元素的直接前驱,其他元素的直接前驱; 8. 求直接后继,要测最后一个元素的后继、不存在顺序表中的元素的直接后继,其他元素的直接后继; 9.删除,要测位置在【1,3】范围之外的情况和之内的情况; 10.清空操作后再测长度;
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; typedef int ElemType; typedef int Status; #define MAXSIZE 100 #define LISTSIZE 10 #define ERROR 0 #define OK 1 #define OVERFLOW -2 typedef struct{ ElemType *elem; int length; int listsize; }SqList; //顺序表初始化 Status InitList(SqList &L){ L.elem = new ElemType[MAXSIZE]; if(!L.elem) exit(OVERFLOW); L.length = 0; L.listsize = LISTSIZE; return OK; } //清空顺序表 Status ListClean(SqList &L){ L.length=0; return 0; } //顺序表的建立 void ListCreat(SqList &L){ int n; cout<<"请输入顺序表的元素个数:"; cin>>n; for(int i=0;i<n;i++){ cout<<"请输入第"<<(i+1)<<"个元素:"; cin>>L.elem[i]; L.length++; } } //顺序表的显示 Status ListShow(SqList L){ for(int i=0;i<L.length;i++){ cout<<L.elem[i]<<" "; } cout<<endl; return 0; } //顺序表的插入 Status ListInsert(SqList &L,int i,ElemType e){ if(i<1||i>L.length+1){ cout<<"插入位置不合法"<<endl; return ERROR; } if(L.length==MAXSIZE) return ERROR; for(int j=L.length-1;j>=i-1;j--){ L.elem[j+1]=L.elem[j]; } L.elem[i-1]=e; ++L.length; return OK; } //顺序表的删除 Status ListDelet(SqList &L,int i){ if(i<1||i>L.length+1){ cout<<"插入位置不合法"<<endl; return ERROR; } for(int j=i;j<=L.length-1;j++){ L.elem[j-1]=L.elem[j]; } --L.length; return OK; } //求线性表长度 Status ListLength(SqList L){ int length = L.length; cout<<"线性表长度为"<<length<<endl; return 0; } //获取指定位置元素 Status GetElem(SqList L,int i,ElemType e){ if(i<1||i>L.length){ cout<<"输入位置不合法"<<endl; return 0; } e=L.elem[i-1]; cout<<e<<endl; return 0; } //求前驱 Status GetPre(SqList L,ElemType e) { for (int i=0;i< L.length;i++){ if(L.elem[i]==e){ if(i==0){ cout<<"无前驱"<<endl; return 0; } else cout<<L.elem[i-1]<<endl; return 0; } else if(i==L.length-1){ cout<<"未找到数据"<<endl; return 0; } } } //求后继 Status GetNext(SqList L,ElemType e) { for (int i=0;i< L.length;i++){ if(L.elem[i]==e){ if(i==L.length-1){ cout<<"无后继"<<endl; return 0; } else cout<<L.elem[i+1]<<endl; return 0; } else if(i==L.length-1){ cout<<"未找到数据"<<endl; return 0; } } } //判断是否为空 Status EmptyList(SqList L){ if(L.length==0){ cout<<"表空"<<endl; } else cout<<"非空"<<endl; return 0; } int main() { 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 << " 退出,输入一个负数!" << endl; SqList L; InitList(L); int i; bool flag=true; ElemType e; while(flag){ cout << "请输入一个操作码:"; cin>>i; if(i==1){ ListClean(L); } else if(i==2){ EmptyList(L); } else if(i==3){ ListLength(L); } else if(i==4){ cout<<"请输入位置:"; int j; cin>>j; GetElem(L,j,e); } else if(i==5){ cout<<"请输入求前驱数据:"; int j; cin>>j; GetPre(L,j); } else if(i==6){ cout<<"请输入求后继数据:"; int j; cin>>j; GetNext(L,j); } else if(i==7){ int a,e; cout<<"请输入插入位置:"; cin>>a; cout<<"请输入插入数据:"; cin>>e; ListInsert(L,a,e); } else if(i==8){ cout<<"请输入删除位置:"; int j; cin>>j; ListDelet(L,j); } else if(i==9){ ListShow(L); } else if(i<0){ flag=false; } } return 0; }