Home > Back-end >  Bosses, look at my first time to write the data structure, input, output, don't know where is w
Bosses, look at my first time to write the data structure, input, output, don't know where is w

Time:10-22

#include
#include
Typedef struct list
{
int data;
Struct list * next;
} the link;
Void initlist (link * & amp; L, int a [], int n)
{
The link * p=l;
L=(link *) malloc (sizeof (link));
for(int i=0; i{
p-> data=https://bbs.csdn.net/topics/a [I];
P=p - & gt; Next;
}
}
Int listempty (link * l)
{
Return (l - & gt; Next==NULL);
}//judge whether empty table
Int listlength (link * l)
{
int n=0;
The link * p=l;
While (p - & gt; Next!=NULL)
{
N++;
P=p - & gt; Next;
}
Return (n);
}//the length of the linear table
Void displist (link * l)
{
The link * p=l - & gt; Next;
while(p!=NULL)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}//output linear table
Int main ()
{
int n;
The scanf (" % d ", & amp; N);
The link * l;
Int a [n].
for(int i=0; i{
The scanf (" % d ", & amp; A [I]);
}
Initlist (l, a, n);
Listempty (l);
Printf (" % d \ n ", listlength (l));
Displist (l);
return 0;
}

CodePudding user response:

Learn to step through,

CodePudding user response:

 # include 
#include
Typedef struct list
{
int data;
Struct list * next;
} the link;

Void initlist (link * & amp; L, int a [], int n)
{
The link * p, * tail;//=l;
L=(link *) malloc (sizeof (link));
Tail=p=l;
for(int i=0; i{
P=(link *) malloc (sizeof (link));
if (! P)
The exit (0);
p-> Data=https://bbs.csdn.net/topics/a [I];
Tail - & gt; Next=p;
Tail=p;
}
Tail - & gt; Next=NULL;
}

Int listempty (link * l)
{
//return (l - & gt; Next==NULL);
Return (l - & gt; Next==NULL);
}//judge whether empty table
Int listlength (link * l)
{
int n=0;
The link * p=l;
While (p - & gt; Next!=NULL)
{
N++;
P=p - & gt; Next;
}
Return (n);
}//the length of the linear table
Void displist (link * l)
{
The link * p=l - & gt; Next;
while(p!=NULL)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}//output linear table
Int main ()
{
int n;
The scanf (" % d ", & amp; N);
The link * l;
//int a [n].
Int * a=(int *) malloc (sizeof (int) * n);
for(int i=0; i{
The scanf (" % d ", & amp; A [I]);
}
Initlist (l, a, n);
Listempty (l);
Printf (" % d \ n ", listlength (l));
Displist (l);
return 0;
}

For your reference ~
Chain table creation has a problem, it is suggested that compare the code to find your own problem