qq_34324657 于 2016.04.24 16:48 提问

#include
#include

#define ERROR 0
#define OK 1

typedef int Status;
typedef int ElemType;//顺序表测试用

const int MaxSize=100; //100只是示例性的数据，可以根据实际问题具体定义
const int Increasement=10;

typedef struct{
ElemType *elem;
int length;
int listsize;
}SeqList;

Status InitList(SeqList &a,int n=MaxSize)
{//初始化线性表
if(n<=0)
n=MaxSize;
a.elem =(ElemType *)malloc(n*sizeof(ElemType));
if(a.elem ==NULL)
exit(ERROR);
a.length =0;
a.listsize =n;
return OK;
}
Status Deletek(SeqList &a,int i,int k) {//从顺序存储结构的线性表a中删除第i个元素起的k个元素??//注意i的编号从0开始??
int j;
if(ia.length-1||ka.length-i) return ERROR; for(j=0;j<=k;j++)
a.elem[j+i]=a.elem[j+i+k]; a.length=a.length-k; return OK;

}

void main()
{
SeqList La;
ElemType data;

``````printf("输入顺序表La的元素值：\n");
for(int i=1;i<=10;i++)
{
scanf("%d",&data);

}

InitList(La,10);
Deletek(La,3,5);
``````

}好像main函数有问题，但我不会改

2个回答

qq423399099      2016.04.24 17:30

`````` #include<iostream>
using namespace std;
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;//顺序表测试用
const int MaxSize=100; //100只是示例性的数据，可以根据实际问题具体定义
const int Increasement=10;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SeqList;
Status InitList(SeqList &a,int n=MaxSize)
{//初始化线性表
if(n<=0)
n=MaxSize;
a.elem =(ElemType *)malloc(n*sizeof(ElemType));
if(a.elem ==NULL)
exit(ERROR);
a.length =0;
a.listsize =n;
return OK;
}
Status Deletek(SeqList &a,int i,int k) {//从顺序存储结构的线性表a中删除第i个元素起的k个元素??//注意i的编号从0开始??
int j;
if(i>=a.length-1||k>=a.length-i)
return ERROR;
for(j=i+k;j<=a.length;j++)
{
a.elem[i++] = a.elem[j];
}
a.length=a.length-k;
return OK;
}
void main()
{
SeqList La;
ElemType data;
InitList(La,10);
printf("输入顺序表La的元素值：\n");
for(int i=1;i<=10;i++)
{
scanf("%d",&data);
La.elem[i - 1] = data;
La.length++;
}
Deletek(La,3,5);
printf("删除后元素值：\n");
for(int i=0;i<La.length;i++)
{
printf("%d ", La.elem[i]);
}
printf("\n");
}
``````

qq_34324657   2016.04.24 21:25