是我插入方法不对吗?还是不可以这样打印啊,打印长度一直是0…
刚学还没入门就入土了😭
供参考:
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 128
typedef struct Sql {
int data[MaxSize];
int Length;
}SeqList;
void InitList(SeqList* L)
{
(*L).Length = 0;
}
bool ListInsert(SeqList* L, int i, int e)
{
if (i < 1 || i > (*L).Length + 1)
return false;
if ((*L).Length > MaxSize)
return false;
for (int j = (*L).Length; j > i - 1; j--)
(*L).data[j] = (*L).data[j - 1];
(*L).data[i - 1] = e;
(*L).Length++;
return true;
}
void PrintList(SeqList L)
{
for (int i = 0; i < L.Length; i++)
printf("%d ", L.data[i]);
printf("\n");
}
int main()
{
SeqList L;
InitList(&L);
ListInsert(&L, 1, 1);
ListInsert(&L, 2, 3);
ListInsert(&L, 3, 5);
ListInsert(&L, 2, 7);
ListInsert(&L, 1, 9);
printf("%d\n", L.Length);
PrintList(L);
return 0;
}
你只调用了InitList函数(应该是初始化函数,Length的初始值应该设为了0),并没有插入数据,所以L.Length是0
你写的是赋值啊,没有调用insert函数,length不加的
#include<stdio.h>
#define Max 255
struct Sql
{
int data[Max];
int len;
};
struct Sql insert(struct Sql L,int n,int i)
{
if(i<0||i>L.len||L.len>Max)
return L;
else
{
for(int j=L.len; j>i; j--)
{
L.data[j]=L.data[j-1];
}
L.data[i]=n;
L.len++;
return L;
}
}
void disply(struct Sql L)
{
for(int i=0; i<L.len; i++)
printf("%d ",L.data[i]);
}
int main()
{
struct Sql L;
L.len=0;
L=insert(L,1,0);
L=insert(L,2,1);
L=insert(L,3,2);
L=insert(L,4,3);
L=insert(L,5,4);
L=insert(L,6,5);
disply(L);
printf("\n在下标2位置插入9后:\n");
L=insert(L,9,2);
disply(L);
return 0;
}
你调用这个方法了吗,如果没有调用,长度是不会增加的