结构体代码如下:
typedef struct{
char num[];
char name[];
int sex;
double price;
}stu;
线性表的建立和插入操作代码如下:
typedef struct {
ElemType data[MAX];
int Length;
}SqList;
int InitList(SqList &L) //初始化
{
L=(SqList)malloc(sizeof(SqList));
L->Length=0;
return OK;
}
bool ListInsert(SqList *&L,ElemType e,int i) //插入
{
int j;
if(L->Length==MAX) return false;
if(i<1||i>L->Length+1) return false;
i--;
for(j=L->Length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->Length++;
return true;
}
现在的问题是如何将结构体里面的内容插入到顺序表中,主函数应该如何调用,因为我试了很多方法都不能将结构体插入顺序表中,希望能够得到帮助!
如果有帮助,一定会积极采纳!谢谢!已经想了一个晚上和早上也想不出来😭

在线性表中如何调用结构体
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qzjhjxj 2022-04-15 16:15关注
供参考:
#include <stdio.h> #include <stdlib.h> #define MAX 50 #define OK 1 #define False 0 typedef struct { char num[16]; char name[32]; int sex; double price; }stu; typedef stu ElemType; typedef struct { //线性表的建立和插入操作代码如下: ElemType data[MAX]; int Length; }SqList; int InitList(SqList*& L) //初始化 { L = (SqList*)malloc(sizeof(SqList)); L->Length = 0; return OK; } bool ListInsert(SqList* L, ElemType e, int i) //插入 { int j; if (L->Length == MAX) return False; if (i<1 || i>L->Length + 1) return False; i--; for (j = L->Length; j > i; j--) L->data[j] = L->data[j - 1]; L->data[i] = e; L->Length++; return OK; } void ListPrint(SqList* L) { int i; for (i = 0; i < L->Length; i++) printf("%s %s %d %f\n", L->data[i].num, L->data[i].name, L->data[i].sex, L->data[i].price); } int main() { SqList* L; ElemType e = { "123456","张三",1,25.36 }; ElemType f = { "123457","李四",0,36.33 }; InitList(L); ListInsert(L, e, 1); ListInsert(L, f, 2); ListPrint(L); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用