顺序表实现和输入 输出问题


#include <stdio.h>
#include<stdlib.h>
#define MaxSize 5 
//1 线性表的顺序存储---顺序表 
//定义结点
typedef struct LNode* List;
struct LNode {
int data[MaxSize];
int length;
};
List Make(){
    int i;
    List P;
    P = (List)malloc(sizeof(struct LNode));
    if(P){
        for(i=0;i<MaxSize;i++){
            scanf("%d",&P.data[i]);
            P.length++;
        }
    }
    return P;
}
void PrintList(List &p){
    int i;
    for(i=0;i<p.length;i++){
        printf("%d ",p.data[i]);
    }
}
int main(){
    List s;
    s = Make();

} 

修改如下,供参考:

#include <stdio.h>
#include<stdlib.h>
#define MaxSize 5
//1 线性表的顺序存储---顺序表
//定义结点
typedef struct LNode* List;
struct LNode {
       int data[MaxSize];
       int length;
};
List Make(){
    int i;
    List P;
    P = (List)malloc(sizeof(struct LNode));
    P->length = 0;
    if(P){
        for(i=0;i<MaxSize;i++){
            scanf("%d",&P->data[i]);//scanf("%d",&P.data[i]);
            P->length++;  //P.length++;
        }
    }
    return P;
}
void PrintList(List &p){
    int i;
    for(i=0;i<p->length;i++){  //i<p.length;
        printf("%d ",p->data[i]);//printf("%d ",p.data[i]);
    }
}
int main(){
    List s;
    s = Make();
    PrintList(s);
    
    return 0;
}

你的代码我拿下来跑了跑,发现问题
你的length并没有赋初值,length++容易导致length野数据(可能是负值),所以无法输出
下边是用.的方法

#include <stdio.h>
#include<stdlib.h>
#define MaxSize 5 
//1 线性表的顺序存储---顺序表 
//定义结点
//typedef struct LNode *List;
struct LNode {
    int data[MaxSize];
    int length=0;
};
LNode Make() {
    int i;
    LNode P;
    //P = (List)malloc(sizeof(struct LNode));
        for (i = 0; i<MaxSize; i++) {
            scanf("%d", &P.data[i]);
            P.length++;
        }
    return P;
}
void PrintList(LNode &p) {
    int i;
    //printf("%d ", p.length);
    for (i = 0; i<p.length; i++) 
    {
        printf("%d  ", p.data[i]);
    }
}
int main() {
    LNode s;
    s = Make();
    PrintList(s);

    return 0;
}