# include & lt; Stdio. H>
# include & lt; Stdlib. H>
Typedef struct Node
{
The int data;
Struct Node * next;
} the Node;
Node * Init (int n)
{
The Node * head=NULL, * p=NULL, * q;
Int I, x;
Head=(*) malloc (sizeof (Node));
The head - & gt; Next=NULL;
P=the head;
for(i=0; i
The scanf (" % d ", & amp; x);
Q=(*) malloc (sizeof (Node));
Q - & gt; data=https://bbs.csdn.net/topics/x;
If (I==n) q - & gt; Next=head;
The else
Q - & gt; Next=NULL;
If (head==NULL)
{
The head=q;
P=q;
}
P - & gt; Next=q;
P=q;
}
P - & gt; Next=NULL;
return head;
}
Void the Order (Node * L, int n, int m)
{
The Node * p=L, * q;
int i,j;
for(i=0; i
P=p - & gt; Next;
}
for(i=0; i
for(j=1; J
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Printf (" % d ", q - & gt; The data);
Free (q);
}
}
Int main ()
{
Node * L;
Int n, m;
The scanf (" % d % d ", & amp; N, & amp; m);
L=Init (n);
The Order (L, n, m);
return 0;
}
CodePudding user response:
Common use of typedef is two different names, such as typedef _Node {... } the Node;Malloc after allocating memory, should judge whether returns NULL, otherwise the operation might be wrong
Handling of the first node in the Init function also has a problem, when n is 1, p - & gt; Next=q can lead to a linked list is not null-terminated
CodePudding user response:
Using linked list, not share with empty header implementation, because the footer to point to the first meaningful data, and the first position is also likely to be deleted,Header does not contain the actual data, it has two Pointers point to the first data, easy to operate is not convenient to implement,
The main procedure:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
Typedef struct Node
{
The int data;
Struct Node * next;
} the Node;
The static void debug_print_list (struct Node * l);
Node * Init (int n)
{
int i;
Head=NULL Node, the Node * * and * prev=NULL;
For (I=0; I & lt; n; I++) {
The node=(*) malloc (sizeof (node));
Node - & gt; Data=https://bbs.csdn.net/topics/i;
Node - & gt; Next=NULL;
If (head==NULL) {
The head=node;
}
If (prev!=NULL) {
Prev - & gt; Next=node;
}
Prev=node;
}
Prev - & gt; Next=head;
return head;
}
Node * Order (Node * L, int n, int m)
{
The Node * p=NULL, * q=L;//p points to previous node, q points to the current node
Int counter=0;
While (1) {
If (q - & gt; Next==q)
break;
Counter +=1;
Printf (" [DBEUG] : node reports % d % d \ n ", q - & gt; The data, counter);
If (counter==m) {
//assert (p!=NULL); Corner case need to be considered
Printf (" [DEBUG] : o node % d \ n ", q - & gt; The data);
P - & gt; Next=q - & gt; Next;
Free (q);
Q=p - & gt; Next;
Counter=0;
Debug_print_list (q);
continue;
}
P=q;
Q=q - & gt; Next;
}
The return of q;
}
Int main ()
{
Node * L * result;
Int n, m;
N=9;
M=3;
L=Init (n);
Debug_print_list (L);
Result=Order (L, n, m);
Printf (" n=% d, m=% d, final - node=% d \ n ", n, m, the result - & gt; The data);
return 0;
}
Void debug_print_list (Node * l) {
Node * p=l;
While (1) {
Printf (" % d ", p - & gt; The data);
If (p - & gt; Next!=l)
Printf (" -- -- & gt; ");
P=p - & gt; Next;
If (p==l)
break;
};
printf("\n");
}