Home > Back-end >  C language list related technical bug (because before Posting to fb, so some English comments)
C language list related technical bug (because before Posting to fb, so some English comments)

Time:11-25

#include
#include

Struct Student
{
Char cName [20].
Int iNumber.
Struct Student * Pnext;

} student;

Int iCount;

Struct Student * Creat () {
Struct Student * pHead=NULL;//initialize pHead
Struct Student * pNew;
Struct Student * pEnd;//to move pointer
ICount=0;
PNew=(struct Student *) malloc (sizeof (struct Student));
PEnd=(struct Student *) malloc (sizeof (struct Student));

Printf (" both Please enter the Name firstly, and then enter the number \ n ");
Scanf_s (" % s ", & amp; PNew - & gt; CName);
Scanf_s (" % d ", & amp; PNew - & gt; INumber);

While (pNew - & gt; INumber!=0)
{
ICount++;//global variable
If (iCount==0) {//ready to create the first node
PNew - & gt; Pnext=NULL;
PEnd=pNew;
PHead=pNew;//

}
The else
{
PNew - & gt; Pnext=NULL;
PEnd - & gt; Pnext=pNew;//move the present pointer to the next node
PEnd=pNew;//

}
PNew=(struct Student *) malloc (sizeof (struct Student));
Scanf_s (" % s ", & amp; PNew - & gt; CName);
Scanf_s (" % d ", & amp; PNew - & gt; INumber);
}

Free (pNew);
Return pHead;//can find the any node of LinkedList by pHead

}

Void Print (struct Student * pHead) {
Struct Student * pTemp;//temporary variable
Int iIndex=1;//using the index to the seek aim node

Printf (" The list from The % d members \ n ", iCount);
printf("\n");

PTemp=pHead;//you know the mean

While (pTemp!=NULL)
{
Printf (" the is No % d: \ n ", iIndex);
Name: printf (" % s \ n ", pTemp - & gt; CName);
Printf (" \ n Number: % d ", pTemp - & gt; INumber);
printf("\n");

IIndex++;

}


}
Int main () {
Struct Student * pHead;//define the Head node
PHead=Creat ();//create the node
Print (pHead);//print Linked List
return 0;

}



/* Hi bro, both please copy my code and run it in your IDE, it 's not have error in visual studio' s complie, but when I run and put some data, I saw the big bug in the CMD window.

I don 't know where I' m wrong, I need your help bro _
No compiler error, just cannot achieve the function of the programming operation, don't know if the computer environment problems

*/






CodePudding user response:

That's lots of problems
Question 1, pEnd=(struct Student *) malloc (sizeof (struct Student));//there is memory leak
Question 2,
ICount++;//global variable
If (iCount==0) {//ready to create the first node//here if not satisfied, because above iCount++ cause iCount is not 0, so lead to pHead has not been set
Question 3,
While (pNew - & gt; INumber!=0)//if the input iNumber is 0, here has withdrawn from circulation, lead to the following will not perform
The else
{
PNew - & gt; Pnext=NULL;
PEnd - & gt; Pnext=pNew;//move the present pointer to the next node
PEnd=pNew;//

}
So lead to pEnd not point to the last node (namely pEnd was the last but one node, not the last node)

Question 4,
While (pTemp!=NULL)
{
Printf (" the is No % d: \ n ", iIndex);
Name: printf (" % s \ n ", pTemp - & gt; CName);
Printf (" \ n Number: % d ", pTemp - & gt; INumber);
printf("\n");

IIndex++;

PTemp=pTemp - & gt; Pnext;//missed moves the pointer list here, while dead cycle

}

Problems have been pointed out that LZ to modify it


CodePudding user response:

 # include 
#include

Struct Student
{
Char cName [20].
Int iNumber.
Struct Student * Pnext;

} student;

Int iCount;

Struct Student * Creat ()
{
Struct Student * pHead=NULL;//initialize pHead
Struct Student * pNew;
Struct Student * pEnd;//to move pointer
ICount=0;
PNew=(struct Student *) malloc (sizeof (struct Student));
//pEnd=(struct Student *) malloc (sizeof (struct Student));

Printf (" both Please enter the Name firstly, and then enter the number \ n ");
Scanf_s (" % s ", pNew - & gt; CName, sizeof (pNew - & gt; CName);
Scanf_s (" % d ", & amp; PNew - & gt; INumber);

While (pNew - & gt; INumber!=0)
{
ICount++;//global variable
//if (iCount==0) {//ready to create the first node
If (iCount==1) {//ready to create the first node
//pNew - & gt; Pnext=NULL;
PEnd=pNew;
PHead=pNew;//

}
The else
{
//pNew - & gt; Pnext=NULL;
PEnd - & gt; Pnext=pNew;//move the present pointer to the next node
PEnd=pNew;//

}
PNew=(struct Student *) malloc (sizeof (struct Student));
Scanf_s (" % s ", pNew - & gt; CName, sizeof (pNew - & gt; CName);
Scanf_s (" % d ", & amp; PNew - & gt; INumber);
}
If (iCount)
PEnd - & gt; Pnext=NULL;
Free (pNew);

Return pHead;//can find the any node of LinkedList by pHead

}

Void Print (struct Student * pHead)
{
Struct Student * pTemp;//temporary variable
Int iIndex=1;//using the index to the seek aim node

Printf (" The list from The % d members \ n ", iCount);
printf("\n");

PTemp=pHead;//you know the mean

While (pTemp!=NULL)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related