顺序表程序有问题
如题:顺序表的类实现,插入操作看似没有问题,删除有问题,最后的查找程序直接崩掉了,自己也不是很清楚其中的缘故,求大神讲解,不胜感激!!!程序代码:
#include<iostream> using namespace std; #define ok 0 #define error -1 class SeqList { private: int *list; int Maxsize; int last; public: SeqList(); ~SeqList() { delete []list; } void initinal(int *mylist,int n); int list_insert(int i,int item); int list_del(int i); int list_find(int value); int list_get(int i); void list_display(); }; SeqList::SeqList() { Maxsize = 1000; list = new int[Maxsize]; last = -1; }; void SeqList::initinal(int *mylist,int n) { Maxsize = n; list = new int[Maxsize]; for(int i = 0; i < n;i++) list[i] = mylist[i]; last = Maxsize - 1; }; int SeqList::list_insert(int i,int item) { if(i < 0||i > last+1)//||last ==Maxsize-1) { return error; } else { last++; for(int j = last;j > i;j--) list[j] = list[j-1]; list[i] = item; return ok; } }; int SeqList::list_del(int i) { if(i >= 0) { last--; for(int j = i;j <= last;j++) list[j] = list[j+1]; return ok; } else return error; }; int SeqList::list_get(int i) { if(i < 0||i >last) return error; else return list[i]; } void SeqList::list_display() { for(int i = 0;i < last+1;i++) cout<<list[i]<<" "; cout<<endl; }; int main() { int n; int mylist[1000]; cin>>n; for(int i= 0;i < n;i++) { cin>>mylist[i]; } SeqList myseq; myseq.initinal(mylist,n); myseq.list_display(); int j; for(j = 0;j < 2;j++)//在k位置插入item { int k,item; cin>>k>>item; myseq.list_insert(k,item); myseq.list_display(); } for(j = 0;j < 2;j++)//删除位置为k的元素 { int k; cin>>k; myseq.list_del(k); myseq.list_display(); } for(j = 0;j < 2;j++)//查找位置为k的元素 { int k; cin>>k; cout<<myseq.list_get(k)<<endl; } return (0); }