#2
azzbcc2016-03-09 09:16
|
程序代码:
#include<iostream>
using namespace std;
const int SIZE = 10;
const int MAX = 100;
typedef int ElemType;
typedef struct SL
{
ElemType * pElem;
int length;
int max;
int add;
}SqList;
void create_SL(SqList &);//建立线性表
void init_SL(SqList &);//初始化线性表
bool is_emept(SqList &);//判断线性表是否为空
void output_SL(SqList &);//输出线性表
bool is_implement(SqList &);//是否执行语句
bool insert_SL(SqList &, int, ElemType);//插入元素
bool delete_SL(SqList &, int);//删除元素
int main()
{
SqList p;
int pos;
ElemType e;
create_SL(p);
init_SL(p);
cout << "原线性表:" << endl;
output_SL(p);
if (0 == p.length)
return 0;
cout << "需要插入第几个元素后 : ";
cin >> pos;
if (pos != 0)
{
cout << "插入的元素是 : ";
cin >> e;
}
insert_SL(p, pos, e);//插入元素
cout << "插入后的线性表:" << endl;
output_SL(p);
cout << "需要删除第几个元素 : ";
cin >> pos;
delete_SL(p, pos);
cout << "删除后的线性表:" << endl;
output_SL(p);
return 0;
}
void create_SL(SqList &p)//建立线性表
{
p.max = MAX;
p.pElem = new ElemType[SIZE];
if (NULL == p.pElem)
{
cout << "分配失败,程序终止!\n";
exit(-1);
}
p.length = 0;
}
void init_SL(SqList &p)
{
int i;
cout << "需要输入多少个数:";
cin >> p.length;
cout << "请输入整数:\n";
for (i = 0; i<p.length; ++i)
{
cin >> p.pElem[i];
}
}
bool is_emept(SqList &p)
{
if (0 == p.length)
return true;
else
return false;
}
void output_SL(SqList &p)
{
if (is_emept(p))
{
cout << "线性表为空!\n";
}
int i;
for (i = 0; i < p.length; i++)
cout << p.pElem[i] << " ";
cout << endl;
}
bool insert_SL(SqList &p, int pos, ElemType e)//e储存所插入的元素
{
if (pos<0 || pos >p.length)
return false;
++p.length;
int i;
for (i = p.length; i != pos; --i)
p.pElem[pos+1] = p.pElem[pos];
p.pElem[pos] = e;
return true;
}
bool delete_SL(SqList &p, int pos)
{
if (pos<1 || pos >p.length)
{
cout << "删除失败!" << endl;
return false;
}
int i;
for (i = pos; i <p.length; ++i)
p.pElem[pos-1] = p.pElem[pos];
--p.length;
return true;
}
using namespace std;
const int SIZE = 10;
const int MAX = 100;
typedef int ElemType;
typedef struct SL
{
ElemType * pElem;
int length;
int max;
int add;
}SqList;
void create_SL(SqList &);//建立线性表
void init_SL(SqList &);//初始化线性表
bool is_emept(SqList &);//判断线性表是否为空
void output_SL(SqList &);//输出线性表
bool is_implement(SqList &);//是否执行语句
bool insert_SL(SqList &, int, ElemType);//插入元素
bool delete_SL(SqList &, int);//删除元素
int main()
{
SqList p;
int pos;
ElemType e;
create_SL(p);
init_SL(p);
cout << "原线性表:" << endl;
output_SL(p);
if (0 == p.length)
return 0;
cout << "需要插入第几个元素后 : ";
cin >> pos;
if (pos != 0)
{
cout << "插入的元素是 : ";
cin >> e;
}
insert_SL(p, pos, e);//插入元素
cout << "插入后的线性表:" << endl;
output_SL(p);
cout << "需要删除第几个元素 : ";
cin >> pos;
delete_SL(p, pos);
cout << "删除后的线性表:" << endl;
output_SL(p);
return 0;
}
void create_SL(SqList &p)//建立线性表
{
p.max = MAX;
p.pElem = new ElemType[SIZE];
if (NULL == p.pElem)
{
cout << "分配失败,程序终止!\n";
exit(-1);
}
p.length = 0;
}
void init_SL(SqList &p)
{
int i;
cout << "需要输入多少个数:";
cin >> p.length;
cout << "请输入整数:\n";
for (i = 0; i<p.length; ++i)
{
cin >> p.pElem[i];
}
}
bool is_emept(SqList &p)
{
if (0 == p.length)
return true;
else
return false;
}
void output_SL(SqList &p)
{
if (is_emept(p))
{
cout << "线性表为空!\n";
}
int i;
for (i = 0; i < p.length; i++)
cout << p.pElem[i] << " ";
cout << endl;
}
bool insert_SL(SqList &p, int pos, ElemType e)//e储存所插入的元素
{
if (pos<0 || pos >p.length)
return false;
++p.length;
int i;
for (i = p.length; i != pos; --i)
p.pElem[pos+1] = p.pElem[pos];
p.pElem[pos] = e;
return true;
}
bool delete_SL(SqList &p, int pos)
{
if (pos<1 || pos >p.length)
{
cout << "删除失败!" << endl;
return false;
}
int i;
for (i = pos; i <p.length; ++i)
p.pElem[pos-1] = p.pElem[pos];
--p.length;
return true;
}
当插入和删除时, 在 取 pos != 1 时, 源代码没问题
而取 pos == 1 时, 源代码就出现问题了
请问代码那个地方有问题