首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

顺序表 程序为啥最后会强行终止

2014-01-05 
顺序表 程序为什么最后会强行终止?#includeiostreamusing namespace std#define MaxLen 1000class SeqL

顺序表 程序为什么最后会强行终止?

#include<iostream>
using namespace std;
#define MaxLen 1000


class SeqList
{
private:
int len;
int data;
SeqList *Ndoe;
public:
SeqList(){Ndoe=NULL;}
void Initlize(int,int []);
void Insert(int,int);
void Delete(int);
void Search(int);
void display();
~SeqList(){delete Ndoe;}
};
void SeqList::Initlize(int n,int array[])
{
int i;
len=n;
Ndoe=new SeqList[MaxLen];
for(i=1;i<=len;i++)
Ndoe[i].data=array[i];
display();
}

void SeqList::Insert(int pos,int key)
{
int i;
if(pos<1 || pos>len+1)
cout<<"error"<<endl;
else
{
for(i=len;i>=pos;i--)
Ndoe[i+1].data=Ndoe[i].data;
    Ndoe[i+1].data=key;
len++;
display();
}
}

void SeqList::Delete(int pos)
{
int i;
if(pos<1 || pos>len)
cout<<"error"<<endl;
else
{
for(i=pos;i<len;i++)
Ndoe[i].data=Ndoe[i+1].data;
    len--;
display();
}
}

void SeqList::Search(int pos)
{
if(pos<1 || pos>len)
cout<<"error"<<endl;
else
cout<<Ndoe[pos].data<<endl;
}


void SeqList::display()
{
int i;
cout<<len<<" ";
for(i=1;i<=len;i++)
cout<<Ndoe[i].data<<" ";
cout<<endl;
}



int main()
{
SeqList c;
int i,n,array[MaxLen],Ipose1,Ipose2,Idata1,Idata2,Dpose1,Dpose2,Spose1,Spose2;
cin>>n;
for(i=1;i<=n;i++)
cin>>array[i];
cin>>Ipose1>>Idata1;
cin>>Ipose2>>Idata2;
cin>>Dpose1;
cin>>Dpose2;
cin>>Spose1;
cin>>Spose2;

c.Initlize(n,array);

c.Insert(Ipose1,Idata1);
c.Insert(Ipose2,Idata2);

c.Delete(Dpose1);
c.Delete(Dpose2);

c.Search(Spose1);
c.Search(Spose2);

return 0;
}
/*
样例输入

6 11 22 33 44 55 66
3 777
1 888
1
9
0
5

样例输出

6 11 22 33 44 55 66 
7 11 22 777 33 44 55 66 
8 888 11 22 777 33 44 55 66 
7 11 22 777 33 44 55 66 
error
error
44

*/







[解决办法]
引用:
什么时候不用  []呢?

new和delete要互相匹配
new对应delete
new []对应 delete []

class a;
a aa=new aa();
delete aa;

是这样吗?
注意细节问题,是一个指针

class a;
a *aa=new a();//调用a的构造函数,只有1个a
delete aa;

a *aa=new a[10];//返回的是包含10个a的数组
delete []aa;

[解决办法]
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。

判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;


}

热点排行