一个c语言数据结构线性表的问题,编译没错,运行结果有问题
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE];
int length;
}SqList;
void Reverse_SqList(SqList *L){
int n = L->length;
if(n == 0||n ==1) return;
int i = 0,j = n-1;
ElemType temp;
while(i<j){
temp = L->data[i];
L->data[i] = L->data[j];
L->data[j] = temp;
i++;
j--;
}
}
void creat_SqList(SqList *L,int n){
int i;
L->length = n;
for(i = 0;i<n;i++ ){
printf("input a int num\n");
scanf("%d",&L->data[i]);
}
}
void printf_SqList(SqList *L){
int n,i;
n = L->length;
for(i = 0;i<n;i++){
if(i = 9){
printf("\n");
}
printf("%d",L->data[i]);
}
}
void main(){
SqList L;
int n;
printf("intput n:\n");
scanf("%d",&n);
creat_SqList(&L,n);
Reverse_SqList(&L);
printf_SqList(&L);
}
理想的结果应该是 5 4 3 2 1 把输入到线性表中的数据倒序输出。 展开
printf_SqList(SqList *L)里面,if (i = 9)错了,改成i==9
#include <stdio.h>
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE];
int length;
}SqList;
void Reverse_SqList(SqList *L){
int n = L->length;
if (n == 0 || n == 1) return;
int i = 0, j = n - 1;
ElemType temp;
while (i<j){
temp = L->data[i];
L->data[i] = L->data[j];
L->data[j] = temp;
i++;
j--;
}
}
void creat_SqList(SqList *L, int n){
int i;
L->length = n;
for (i = 0; i<n; i++){
printf("input a int num\n");
scanf("%d", &L->data[i]);
}
}
void printf_SqList(SqList *L){
int n, i;
n = L->length;
for (i = 0; i<n; i++){
if (i == 9){
printf("\n");
}
printf("%d", L->data[i]);
}
}
void main(){
SqList L;
int n;
printf("intput n:\n");
scanf("%d", &n);
printf_SqList(&L);
Reverse_SqList(&L);
printf_SqList(&L);
}
原来是这样,我昨天看了俩小时没看明白到底哪错了。这个有什么规律么,根据错误的输出结果
广告 您可能关注的内容 |