current position:Home>Questions about data structure: data structure sequence table (language-c language)

Questions about data structure: data structure sequence table (language-c language)

2022-02-02 16:35:39 CSDN Q & A

#include "stdio.h"#include "stdlib.h"#define MAXSIZE 100#define OK 1#define ERRO 0typedef int ElemType;typedef int Status;typedef struct{ ElemType data[MAXSIZE]; int length;}Val;Status InitVal(Val *L){ L->length=0; return OK;}Status ListInsert(Val *L,int i,ElemType e){ int k; if(L->length==MAXSIZE) return ERRO; if(i<1||i>L->length+1) return ERRO; for(k=L->length-1;k>=i-1;k--) L->data[k+1]=L->data[k]; L->data[i-1]=e; L->length++; return OK;}int Compare(Val *A,Val *B){ int i=0; while(ilength&&ilength) { if(A->data[i]>B->data[i]) return 1; else if(A->data[i]data[i]) return -1; else i++; } if(ilength) return 1; else if(ilength) return -1; else return 0;}int main(){ Val A,B; int m,n; int a[m]; int b[n]; printf(" Please enter the sequence table A Number of elements of :"); scanf("%d",&m); printf(" Please enter the sequence table A The elements of :"); for(int j=0;j<m;j++) { scanf("%d",&a[j]); } printf(" Please enter the sequence table B Number of elements of :"); scanf("%d",&n); printf(" Please enter the sequence table B The elements of :"); for(int j=0;j<n;j++) { scanf("%d",&b[j]); } int Result=0; InitVal(&A); InitVal(&B); for(int i=1;i<m+1;i++) { ListInsert(&A,i,a[i-1]); } for(int i=1;i<n+1;i++) { ListInsert(&B,i,b[i-1]); } printf(" The two sequential tables are :\nA:"); for(int i=0;i<m;i++) { printf("%d\t",A.data[i]); } printf("\nB:"); for(int i=0;i<n;i++) { printf("%d\t",B.data[i]); } printf("\n"); Result=Compare(&A,&B); if(Result==1) { printf("A>B\n"); } else if(Result==-1) { printf("A<B\n"); } else if(Result==0) { printf("A=B\n"); }}
This program works well within two groups of four numbers , but B The fifth number in the group B The fifth number of the group covers A The first number of groups , What's the problem




Refer to the answer 1:

At least change the line , What do you think




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021635367790.html

Random recommended