Home > Back-end >  A question about the malloc function location
A question about the malloc function location

Time:11-01

The first way a run my collapse would collapse
The second is my metaphysics operation got it right
Can have a great god to help me solve the genuflect is begged
This is the pta
6-2 delete singly linked lists even-numbered node (15 points) topic
Struct ListNode * createlist ()
{
Struct ListNode * head, * p, * q; int m;
The head=NULL;
P=q=(struct ListNode *) malloc (sizeof (struct ListNode *));
While (the scanf (" % d ", & amp; M) & amp; & M!=1)
{
P - & gt; data=https://bbs.csdn.net/topics/m;
If (head==NULL) head=p;
The else q - & gt; Next=p;

Q=p;
P=(struct ListNode *) malloc (sizeof (struct ListNode *));


}
P=NULL;
return head;


}
Struct ListNode * createlist ()
{
Struct ListNode * head, * p, * q; Int j, oo;
The head=NULL;



While (the scanf (" % d ", & amp; M) & amp; & M!=1)
{
P=(struct ListNode *) malloc (sizeof (struct ListNode *));

P - & gt; data=https://bbs.csdn.net/topics/m;
If (head==NULL) head=p;
The else q - & gt; Next=p;

Q=p;



}
P - & gt; Next=NULL;
return head;


}



The complete code





#include
#include

Struct ListNode {
int data;
Struct ListNode * next;
};

Struct ListNode * createlist ();
Struct ListNode * deleteeven (struct ListNode * head);
Void printlist (struct ListNode * head)
{
Struct ListNode * p=head;
While (p) {
Printf (" % d ", p - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}

Int main ()
{
Struct ListNode * head;

The head=createlist ();
The head=deleteeven (head);
Printlist (head);

return 0;
}
Typedef struct ListNode * List;
Struct ListNode * createlist ()
{
Struct ListNode * head, * p, * q; int m;
The head=NULL;
P=q=(struct ListNode *) malloc (sizeof (struct ListNode *));
While (the scanf (" % d ", & amp; M) & amp; & M!=1)
{
P - & gt; data=https://bbs.csdn.net/topics/m;
If (head==NULL) head=p;
The else q - & gt; Next=p;

Q=p;
P=(struct ListNode *) malloc (sizeof (struct ListNode *));


}
P=NULL;
return head;


}

Struct ListNode * deleteeven (struct ListNode * head)
{

Struct ListNode * p, * q;
P=the head;
While (head & amp; & The head - & gt; Data % 2==0) {
P=the head;
The head=head - & gt; Next;
Free (p);
}
P=the head;

While (p& & P - & gt; Next)
{
While (p - & gt; Next& & P - & gt; Next - & gt; Data % 2==0)
{
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
}

P=p - & gt; Next;

}
return head;
}

CodePudding user response:

P=q=(struct ListNode *) malloc (sizeof (struct ListNode *)); There is something wrong with the memory allocation,
P=q=(struct ListNode *) malloc (sizeof (struct ListNode));
  • Related