Home > Back-end >  Why my last character array is empty, and the first value of the subscript 1
Why my last character array is empty, and the first value of the subscript 1

Time:10-07

#include
#include
Typedef struct LNode
{char data;
struct LNode *next;
} LinkNode;
Char a, [50].
Void CreateList (LinkNode * & amp; L, char [], int n)
{LinkNode * s * r;
int i;
L=(LinkNode *) malloc (sizeof (LinkNode));
L - & gt; next=NULL;
R=L;
for(i=0; i={s (LinkNode *) malloc (sizeof (LinkNode));
S - & gt; data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s;
r=s;
}
R - & gt; next=NULL;
}
Void ReadList (LinkNode * & amp; L, char x)
{
LinkNode * p=L;
Int j=0;
While (p!=NULL) {
If (p - & gt; data=https://bbs.csdn.net/topics/=x)
j++;
P=p - & gt; next;
}
Printf (" % d ", j);
}
Int main ()
{
LinkNode * L;
int i,n;
Char x;
The scanf (" % c % d ", & amp; X, & amp; N);
for(i=0; iThe scanf (" % c ", & amp; A [I]);
Printf (" % c ", a [0]).
CreateList (L, a, n);
ReadList (L, x);
return 0;
}

CodePudding user response:

Main function:
The scanf (" % c % d ", & amp; X, & amp; N);//after execution, the keyboard buffer inside still enter symbols, behind the scanf % c will return to enter
getchar ();//filter the carriage return, or use fflush (stdin); Can also be
for(i=0; iThe scanf (" % c ", & amp; A [I]);

Void ReadList (LinkNode * & amp; L, char x)
{
LinkNode * p= L - & gt; Next ;//want to change, L is short, next is the first effective

CodePudding user response:

Fun
reference 1 floor response:
inside the main function:
The scanf (" % c % d ", & amp; X, & amp; N);//after execution, the keyboard buffer inside still enter symbols, behind the scanf % c will return to enter
getchar ();//filter the carriage return, or use fflush (stdin); Can also be
for(i=0; iThe scanf (" % c ", & amp; A [I]);

Void ReadList (LinkNode * & amp; L, char x)
{
LinkNode * p= L - & gt; Next ;//want to change, L is short, next is the first effective

After joining getchar () in the third line of input is how to return a responsibility
5 c
C d e s c
C//extra line casually enter
2

CodePudding user response:

 # include 
#include

Typedef struct LNode
{char data;
struct LNode *next;
} LinkNode;

Char a, [50].

Void CreateList (LinkNode * & amp; L, char [], int n)
{
LinkNode * s * r;
int i;

L=(LinkNode *) malloc (sizeof (LinkNode));
L - & gt; next=NULL;
R=L;
for(i=0; i{
S=(LinkNode *) malloc (sizeof (LinkNode));
S - & gt; data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s;
r=s;
}
R - & gt; next=NULL;
}
Void ReadList (LinkNode * & amp; L, char x)
{
//LinkNode * p=L;
LinkNode * p=L - & gt; next;
Int j=0;
While (p!=NULL) {
If (p - & gt; data=https://bbs.csdn.net/topics/=x)
j++;
P=p - & gt; next;
}
Printf (" % d ", j);
}
Int main ()
{
LinkNode * L;
int i,n;
Char x;
The scanf (" % c % d ", & amp; X, & amp; N);
getchar();
for(i=0; iThe scanf (" % c ", & amp; A [I]);
Printf (" % c ", a [0]).
CreateList (L, a, n);
ReadList (L, x);
return 0;
}


For your reference ~
  • Related