主题:顺序存储线性表结构的问题
程序有点长,不过不要紧大家复制下在VC下可以运行,问题就在插入元素的时候出错了,程序停止运行不知道为什么,请大家指教:
#include<iostream>
#include <iomanip>
using namespace std;
typedef int DataType;
struct seqlist{
int MAXNUM;
int n;
DataType *element;
};
typedef struct seqlist * PSeqlist;
//创建空顺序表
PSeqlist createNullSeqlist_seq(int m)
{
PSeqlist palist = (PSeqlist)malloc(sizeof(struct seqlist));
if(palist!=NULL) return NULL;
palist->element = (DataType*)malloc(sizeof(DataType)*m);
if(palist->element)
{
palist->MAXNUM = m;
palist->n = 0;
return palist;
}
else free(palist);
cout<<"内存不足!"<<endl;
return NULL;
}
//判断是否为空
int isNullList_seq(PSeqlist palist)
{
return (palist->n == 0);
}
//插入元素
void insert_seq(PSeqlist palist,int p,DataType x)
{
if(palist->n >= palist->MAXNUM)
{
cout<<"该顺序表已满!"<<endl;
return;
}
if(p<1||p>palist->MAXNUM)
{
cout<<"插入位置不对"<<endl;
return;
}
if(palist->n == 0)
{
palist->element[0] = x;
palist->n += 1;
}
else
{
for(int i = palist->MAXNUM-1; i > p-1;i--)
palist->element[i] = palist->element[i-1];
palist->n += 1;
palist->element[p] = x;
}
}
//删除元素
void delSeqlist_seq(PSeqlist palist,int p)
{
if(palist->n == 0)
{
cout<<"顺序表为空"<<endl;
return;
}
if(p<1||p>palist->MAXNUM)
{
cout<<"插入位置不对"<<endl;
return;
}
for(int i = p; i <= palist->MAXNUM-1; i++)
{
palist->element[i-1] = palist->element[i];
}
palist->n -= 1;
}
//遍历元素
void display_seq(PSeqlist palist)
{
if(palist->n == 0){ cout<<"顺序表是空的"<<endl; return;}
for(int i = 0; i < palist->MAXNUM; i++)
{
cout<<palist->element[i];
}
}
int main()
{
cout<<"输入顺序表内元素个数:"<<endl;
int t;
cin>>t;
PSeqlist aa = createNullSeqlist_seq(t);
if(aa = createNullSeqlist_seq(t)) cout<<"空顺序表建立成功!"<<endl;
cout<<"输入插入元素值:"<<endl;
int a;
cin>>a;
cout<<"输入插入位置:"<<endl;
int b;
cin>>b;
insert_seq(aa,b,a);
cout<<"遍历"<<endl;
display_seq(aa);
cout<<"输入删除元素位置:"<<endl;
int j;
cin>>j;
delSeqlist_seq(aa,j);
return 0;
}
#include<iostream>
#include <iomanip>
using namespace std;
typedef int DataType;
struct seqlist{
int MAXNUM;
int n;
DataType *element;
};
typedef struct seqlist * PSeqlist;
//创建空顺序表
PSeqlist createNullSeqlist_seq(int m)
{
PSeqlist palist = (PSeqlist)malloc(sizeof(struct seqlist));
if(palist!=NULL) return NULL;
palist->element = (DataType*)malloc(sizeof(DataType)*m);
if(palist->element)
{
palist->MAXNUM = m;
palist->n = 0;
return palist;
}
else free(palist);
cout<<"内存不足!"<<endl;
return NULL;
}
//判断是否为空
int isNullList_seq(PSeqlist palist)
{
return (palist->n == 0);
}
//插入元素
void insert_seq(PSeqlist palist,int p,DataType x)
{
if(palist->n >= palist->MAXNUM)
{
cout<<"该顺序表已满!"<<endl;
return;
}
if(p<1||p>palist->MAXNUM)
{
cout<<"插入位置不对"<<endl;
return;
}
if(palist->n == 0)
{
palist->element[0] = x;
palist->n += 1;
}
else
{
for(int i = palist->MAXNUM-1; i > p-1;i--)
palist->element[i] = palist->element[i-1];
palist->n += 1;
palist->element[p] = x;
}
}
//删除元素
void delSeqlist_seq(PSeqlist palist,int p)
{
if(palist->n == 0)
{
cout<<"顺序表为空"<<endl;
return;
}
if(p<1||p>palist->MAXNUM)
{
cout<<"插入位置不对"<<endl;
return;
}
for(int i = p; i <= palist->MAXNUM-1; i++)
{
palist->element[i-1] = palist->element[i];
}
palist->n -= 1;
}
//遍历元素
void display_seq(PSeqlist palist)
{
if(palist->n == 0){ cout<<"顺序表是空的"<<endl; return;}
for(int i = 0; i < palist->MAXNUM; i++)
{
cout<<palist->element[i];
}
}
int main()
{
cout<<"输入顺序表内元素个数:"<<endl;
int t;
cin>>t;
PSeqlist aa = createNullSeqlist_seq(t);
if(aa = createNullSeqlist_seq(t)) cout<<"空顺序表建立成功!"<<endl;
cout<<"输入插入元素值:"<<endl;
int a;
cin>>a;
cout<<"输入插入位置:"<<endl;
int b;
cin>>b;
insert_seq(aa,b,a);
cout<<"遍历"<<endl;
display_seq(aa);
cout<<"输入删除元素位置:"<<endl;
int j;
cin>>j;
delSeqlist_seq(aa,j);
return 0;
}