} //构造一个空的线性表 void InitList(SqList* L) { L->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);//申请连续的MAXSIZE长度空间 if (!L->data)//判断空间是否申请成功 exit(-1); for (int i = 0; i < MAXSIZE; i++) L->data[i] = 0; L->highPower = 0;//空表最高次项为0 }怎么输出
时间: 2023-11-27 10:53:45 浏览: 36
你可以使用以下代码输出线性表中的元素:
```
void PrintList(SqList L) {
for (int i = 0; i < L.highPower; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
```
然后在主函数中调用该函数即可输出线性表的元素,如下所示:
```
int main() {
SqList L;
InitList(&L);
// 插入数据 ...
PrintList(L);
return 0;
}
```
注意,对于线性表的输出,只需要输出 0 到 highPower - 1 这些下标对应的元素即可,因为 highPower 表示的是该线性表的最高次项,即高于 highPower 的项一定是 0,不需要输出。
相关问题
#include <stdio.h> #include <stdlib.h> # include <malloc.h> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int ElemType; typedef int Status; typedef struct { ElemType *pList; int sqSize; int sqLength; }SqList; Status InitList(SqList *L); Status CreateList(SqList *L); Status PrintList(SqList *L); Status InsertList(SqList *L,int i,ElemType e); Status DeleteSqList(SqList *L,int i,ElemType *e); //初始化一个空的线性表 Status InitList(SqList *L) { L->pList = (SqList *)malloc(sizeof(SqList)); //申请内存空间 if(L->pList == NULL) { printf("内存空间分配失败!"); } else { L->sqLength = 0; } return OK; } 请分析这段代码哪里有误
这段代码有几个问题:
1. 在结构体定义中,`pList` 应该是 `ElemType` 类型的指针,而不是 `SqList` 类型的指针;
2. 在 `InitList` 函数中,应该先将 `sqSize` 赋值为 `MAXSIZE`,再分配内存空间;
3. 在 `InitList` 函数中,应该将 `pList` 赋值为一个 `ElemType` 类型的数组,而不是一个 `SqList` 类型的指针;
4. 在 `InitList` 函数中,应该将 `sqLength` 赋值为 0,而不是在 `else` 语句块中赋值;
5. 在 `InsertList` 函数中,应该判断插入位置 `i` 是否合法,即 `i > 0 && i <= L->sqLength + 1`;
6. 在 `InsertList` 函数中,应该判断线性表是否已满,即 `L->sqLength >= MAXSIZE`;
7. 在 `DeleteSqList` 函数中,应该判断删除位置 `i` 是否合法,即 `i > 0 && i <= L->sqLength`。
修正后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct {
ElemType *pList;
int sqSize;
int sqLength;
} SqList;
// 初始化一个空的线性表
Status InitList(SqList *L) {
L->pList = (ElemType *)malloc(sizeof(ElemType) * MAXSIZE); //申请内存空间
if (L->pList == NULL) {
printf("内存空间分配失败!");
return ERROR;
} else {
L->sqSize = MAXSIZE;
L->sqLength = 0;
return OK;
}
}
// 创建线性表
Status CreateList(SqList *L) {
printf("请输入线性表的长度:");
scanf("%d", &(L->sqLength));
printf("请输入线性表的元素:");
for (int i = 0; i < L->sqLength; i++) {
scanf("%d", &(L->pList[i]));
}
return OK;
}
// 输出线性表
Status PrintList(SqList *L) {
printf("线性表中的元素为:");
for (int i = 0; i < L->sqLength; i++) {
printf("%d ", L->pList[i]);
}
printf("\n");
return OK;
}
// 在第 i 个位置插入元素 e
Status InsertList(SqList *L, int i, ElemType e) {
if (i < 1 || i > L->sqLength + 1) {
printf("插入位置不合法!");
return ERROR;
}
if (L->sqLength >= MAXSIZE) {
printf("线性表已满,无法插入!");
return ERROR;
}
for (int j = L->sqLength; j >= i; j--) {
L->pList[j] = L->pList[j-1];
}
L->pList[i-1] = e;
L->sqLength++;
return OK;
}
// 删除第 i 个元素,并将其值保存到 e 中
Status DeleteSqList(SqList *L, int i, ElemType *e) {
if (i < 1 || i > L->sqLength) {
printf("删除位置不合法!");
return ERROR;
}
*e = L->pList[i-1];
for (int j = i; j < L->sqLength; j++) {
L->pList[j-1] = L->pList[j];
}
L->sqLength--;
return OK;
}
int main() {
SqList L;
InitList(&L);
CreateList(&L);
PrintList(&L);
InsertList(&L, 3, 99);
PrintList(&L);
ElemType e;
DeleteSqList(&L, 4, &e);
printf("删除的元素为:%d\n", e);
PrintList(&L);
return 0;
}
```
改进以下代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define ar arr[]={12,21,2,11,10,8} #define ELEM_TYPE int int ar; //顺序表的创建: typedef struct Sqlist { ELEM_TYPE *data; int length; int SIZE; }Sqlist,*PSqlist; //顺序表的初始化: void Init_Sqlist(P
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAXSIZE 100 // 定义最大长度
typedef int ElemType; // 定义元素类型
typedef struct {
ElemType *data; // 动态分配数组
int length; // 当前长度
int maxSize; // 最大长度
} SqList;
// 初始化顺序表
void InitList(SqList *L) {
L->data = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE); // 动态分配数组
L->length = 0; // 初始长度为0
L->maxSize = MAXSIZE; // 最大长度为MAXSIZE
}
// 插入元素
int Insert(SqList *L, int i, ElemType e) {
// i的合法范围为 1 <= i <= L->length + 1
if (i < 1 || i > L->length + 1) {
return 0; // 插入位置不合法
}
if (L->length >= MAXSIZE) {
return 0; // 当前存储空间已满,不能插入新元素
}
for (int j = L->length; j >= i; j--) {
L->data[j] = L->data[j - 1]; // 将第i个元素及之后的元素后移
}
L->data[i - 1] = e; // 插入新元素
L->length++; // 长度加1
return 1;
}
// 删除元素
int Delete(SqList *L, int i) {
// i的合法范围为 1 <= i <= L->length
if (i < 1 || i > L->length) {
return 0; // 删除位置不合法
}
for (int j = i; j < L->length; j++) {
L->data[j - 1] = L->data[j]; // 将第i+1个元素及之后的元素前移
}
L->length--; // 长度减1
return 1;
}
// 输出顺序表
void PrintList(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
}
int main() {
SqList L;
InitList(&L);
Insert(&L, 1, 1);
Insert(&L, 2, 3);
Insert(&L, 3, 5);
PrintList(L);
Delete(&L, 2);
PrintList(L);
return 0;
}
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)