Home > Back-end >  Linear table data structure, inserted into the problem
Linear table data structure, inserted into the problem

Time:10-25

Data structure, linear table insert

I want to use structure inserted the 5 into the 1234678 array, what is wrong? Who help me ah, don't passed the
#include
# define maxsize 100
Typedef struct
{
The int data [maxsize];
int length;
} Sqlist;
,2,3,4,6,7,8 Sqlist L={{1}, 8};

Int findelem (Sqlist L, int x);
Void insertelem (Sqlist & amp; L, int x);


Int main (void)
{
Insertelem (L, 5);
Int j=0;
While (j & lt; L.l ength - 1) {
Printf (" % d ", L.d ata [maxsize]);
J++;
}
return 0;
}


Int findelem (Sqlist L, int x)//find position
{
int i;
For (I=0; I & lt; L.l ength; + + I)
{
If (x & lt; L.d ata [I])
{
return i;
}
}
return i;
}

Void insertelem (Sqlist & amp; L, int x)//insert
{
Int I, p;
P=findelem (L, x);
For (I=L.l ength - 1; I & gt;=p; - I)
L.d ata [I + 1)=L.d ata [I];
L.d ata [p]=x;
+ + L.l ength;
}

CodePudding user response:

Modify the following
While (jPrintf (" % d ", L.d ata [j]);//don't print the L.d ata [maxsize], because first of all, L.d ata [maxsize] will be an array, if want to print the last element is also L.d ata [maxsize - 1), second, it should be print L.d ata [j]. J loop otherwise meaningless, L.d ata maxsize print is the same element,
J++;
}
  • Related