#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem; //指向数据元素的基地址
int length; //线性表的当前长度
} SqList;
Status InitList_Sq(SqList *L) {
//构造一个空的顺序表L
L->elem=new ElemType[MAXSIZE]; //为顺序表分配空间
if(!L->elem) exit(OVERFLOW); //存储分配失败
L->length=0; //空表长度为0
return OK;
}
Status CreateList_Sq(SqList &L,int n) {
int i;
for (i=0; i<n; n++){
scanf("%d",&L.elem[i]);
}
}
Status ListInsert_Sq(SqList &L,int i,ElemType e) {
int j;
for (j=L.length-1; j>=i-1; j--){
L.elem[j+1]=L.elem[j];}
L.elem[i-1]=e;
++L.length;
return OK;
}
void TraverseList_Sq(SqList &L) {
int j;
for (j=0; j<L.length; j++) {
printf("%d",L.elem[j]);
}
}
void DestroyList_Sq(SqList &L) {
if (L.elem) delete[]L.elem; //释放存储空间
}
int main() {
SqList L;
int i,n,e;
InitList_Sq(&L);
scanf("%d",&n) ;//提示:输入元素个数:
L.length=n;
CreateList_Sq(L,n);
TraverseList_Sq(L); //遍历输出插入前线性表数据元素
//提示:在顺序表中输入新元素插入的位置和数据:
scanf("%d",&i);
scanf("%d",&e);
ListInsert_Sq(L,i,e);
//在此处编写 ListInsert_Sq函数的调用语句
TraverseList_Sq(L);
DestroyList_Sq(L);
return 0;
}
求告诉我,我的顺序表插入哪个错了。复制的有点乱,在编辑器排整齐了
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- CSDN专家-天际的海浪 2022-03-18 15:41关注
#include <stdio.h> #include<stdlib.h> #define MAXSIZE 100 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef int ElemType; typedef struct { ElemType *elem; //指向数据元素的基地址 int length; //线性表的当前长度 } SqList; Status InitList_Sq(SqList *L) { //构造一个空的顺序表L L->elem=new ElemType[MAXSIZE]; //为顺序表分配空间 if(!L->elem) exit(OVERFLOW); //存储分配失败 L->length=0; //空表长度为0 return OK; } Status CreateList_Sq(SqList &L,int n) { int i; for (i=0; i<n; i++){ //是i++ 不是n++ scanf("%d",&L.elem[i]); } } Status ListInsert_Sq(SqList &L,int i,ElemType e) { int j; for (j=L.length-1; j>=i-1; j--){ L.elem[j+1]=L.elem[j];} L.elem[i-1]=e; ++L.length; return OK; } void TraverseList_Sq(SqList &L) { int j; for (j=0; j<L.length; j++) { printf("%d ",L.elem[j]); } printf("\n"); } void DestroyList_Sq(SqList &L) { if (L.elem) delete[]L.elem; //释放存储空间 } int main() { SqList L; int i,n,e; InitList_Sq(&L); scanf("%d",&n) ;//提示:输入元素个数: L.length=n; CreateList_Sq(L,n); TraverseList_Sq(L); //遍历输出插入前线性表数据元素 //提示:在顺序表中输入新元素插入的位置和数据: scanf("%d",&i); scanf("%d",&e); ListInsert_Sq(L,i,e); //在此处编写 ListInsert_Sq函数的调用语句 TraverseList_Sq(L); DestroyList_Sq(L); return 0; }
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用