Home > Back-end >  The code for c language data structure
The code for c language data structure

Time:10-28

Topic is

This is my code
#include
#include
Typedef struct Node {
The int data;
Struct Node * pNext;
} NODE, * PNODE;
PNODE create_list (int n)
{
int i;
Int val.


PNODE pHead=(PNODE) malloc (sizeof (NODE));
If (NULL==pHead)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
PNODE pTail=pHead;
PTail - & gt; PNext=NULL;


For (I=0; i{
The scanf (" % d ", & amp; Val);

PNODE pNew=(PNODE) malloc (sizeof (NODE));
If (NULL==pNew)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}

PNew - & gt; Data=https://bbs.csdn.net/topics/val;
PTail - & gt; PNext=pNew;
PNew - & gt; PNext=NULL;
PTail=pNew;
}

Return pHead;
}
Void traverse_list (PNODE pHead)
{
PNODE p=pHead - & gt; PNext;

while(p!=NULL)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; PNext;

}
printf("\n");
return;
}
Void sort (PNODE pHead, int x)
{
int i=1;
PNODE p=(PNODE) malloc (sizeof (NODE));
P=pHead - & gt; PNext;
PNODE r=(PNODE) malloc (sizeof (NODE));
while(p!=NULL& & P - & gt; Data>=x)
{
i++;
P=p - & gt; PNext;
}
R - & gt; Data=https://bbs.csdn.net/topics/x;
R - & gt; PNext=p - & gt; PNext;
P - & gt; PNext=r.
If (I==1)
{
R - & gt; Data=https://bbs.csdn.net/topics/x;
R - & gt; PNext=p;
PHead - & gt; PNext=r.
}
Printf (" % d ", I);
}

Int main ()
{
Int n, x;
PNODE pHead=NULL;
While (the scanf (" % d ", & amp; N)!=(EOF)
{
PHead=create_list (n);
The scanf (" % d ", & amp; X);
If (n==0)
{
Printf (" % d ", & amp; X);
}
The else
{
Sort (pHead, x);
Traverse_list (pHead);

}
}
return 0;
}

Compile the right code to run the timeout for stop==still didn't understand why the wrong turn bosses

CodePudding user response:

 # include 
#include

Typedef struct Node {
The int data;
Struct Node * pNext;
} NODE, * PNODE;

PNODE create_list (int n)
{
int i;
Int val.


PNODE pHead=(PNODE) malloc (sizeof (NODE));
If (NULL==pHead)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}
PNODE pTail=pHead;
PTail - & gt; PNext=NULL;


For (I=0; i{
The scanf (" % d ", & amp; Val);

PNODE pNew=(PNODE) malloc (sizeof (NODE));
If (NULL==pNew)
{
Printf (" allocation failure, the program terminates. \n");
exit(-1);
}

PNew - & gt; Data=https://bbs.csdn.net/topics/val;
PNew - & gt; PNext=NULL;
PTail - & gt; PNext=pNew;
PTail=pNew;
}

Return pHead;
}

Void traverse_list (PNODE pHead)
{
PNODE p=pHead - & gt; PNext;

while(p!=NULL)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; PNext;

}
printf("\n");
return;
}

Void sort (PNODE pHead, int * x)
{
int i=1;
//PNODE p=(PNODE) malloc (sizeof (NODE));
PNODE p;
PNODE r=(PNODE) malloc (sizeof (NODE));
if (! R)
exit(0);
R - & gt; Data=https://bbs.csdn.net/topics/* x;
//r - & gt; PNext=NULL;

//p=pHead - & gt; PNext;
P=pHead;
//while (p!=NULL& & P - & gt; Data>=x)
While (p - & gt; PNext & amp; & P - & gt; PNext - & gt; The data & lt;=* x)
{
i++;
P=p - & gt; PNext;
}

R - & gt; PNext=p - & gt; PNext;
P - & gt; PNext=r.
* x=I;

# if 0

R - & gt; Data=https://bbs.csdn.net/topics/x;
R - & gt; PNext=p - & gt; PNext;
P - & gt; PNext=r.
If (I==1)
{
R - & gt; Data=https://bbs.csdn.net/topics/x;
R - & gt; PNext=p;
PHead - & gt; PNext=r.
}
Printf (" % d ", I);
# endif
}

Int main ()
{
Int n, x;
PNODE pHead=NULL;
While (the scanf (" % d ", & amp; N)!=(EOF)
{
PHead=create_list (n);
The scanf (" % d ", & amp; X);
If (n==0)
{
//printf (" % d ", & amp; X);
Printf (" % d ", x);
}
The else
{
Sort (pHead, & amp; X);
Printf (" % d \ n ", x);
Traverse_list (pHead);

}
}

return 0;
}

For your reference ~

The building Lord judge has a problem,
//while (p!=NULL& & P - & gt; Data>=x) 
While (p - & gt; PNext & amp; & P - & gt; PNext - & gt; The data & lt;=* x)

Is & gt;=x or & lt; The problem of=x,

Other Suggestions contrast code to find your own problems ~
Here I use x pointer, the purpose is to obtain location information

CodePudding user response:

Thank you finally understand the code!
Thank you!!!!!!
  • Related