Home > Back-end >  Delete data structure for the node of x
Delete data structure for the node of x

Time:10-07

# include
# include & lt; Stdlib. H>
Typedef struct LNode
{int data;
Struct LNode * next;
} LinkNode;
int a[50];
Void CreateList (LinkNode * L, int a [], 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;
}
Int ListDelete (LinkNode * L, int x)
{
Int j=0;
LinkNode * p=L, * q;
If (L - & gt; Next==NULL)
return 0;
The else {
While (p - & gt; Next!=NULL) {
If ((p - & gt; Next) - & gt; data=https://bbs.csdn.net/topics/=x) {
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Free (q);
j++;
}
The else
P=p - & gt; Next;
}
Return j; }
}

Int main ()
{
LinkNode L;
Int x, I, n, k;
The scanf (" % d % d ", & amp; X, & amp; n);
for(i=0; iscanf("%d",& a[i]);
CreateList (& amp; L, a, n);
K=ListDelete (& amp; L, x);
Printf (" % d ", a [0]).
If (k!=0) {
for(i=0; iPrintf (" % d ", a [I]);
}
The else
Printf (" NULL ");
return 0;
}

CodePudding user response:

# include
# include & lt; Stdlib. H>
Typedef struct LNode
{int data;
Struct LNode * next;
} LinkNode;
int a[50];

LinkNode * CreateList (int a [], int n)
{
LinkNode * s * r * L;
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;
Return the L;
}

Int ListDelete (LinkNode * L, int x)
{
Int j=0;
LinkNode * p=L, * q;
If (L - & gt; Next==NULL)
return 0;
The else {
While (p - & gt; Next!=NULL) {
If ((p - & gt; Next) - & gt; data=https://bbs.csdn.net/topics/=x) {
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Free (q);
j++; What do you mean//j? Are the list of multiple x? If it is still need a p=p - & gt; Next; Here
}
The else
P=p - & gt; Next;
}
Return j;
}
}

void ListPrint (LinkNode * L) {
If (L & amp; & L - & gt; Next) {
Printf (" List datas: \ n ");
LinkNode * p=L;
While (p - & gt; Next) {
Printf (" % d ", p - & gt; Next - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
} else {
Printf (" \ n -- -- -- -- -- -- -- -- the Empty List \ n ");
}
}

Int main ()
{
LinkNode * L;
int x, I, n, k;
The scanf (" % d % d ", & amp; X, & amp; n);
for(i=0; iscanf("%d",& a[i]);
L=CreateList (a, n);
ListPrint (L);
K=ListDelete (L, x);
ListPrint (L);
printf (" % d ", a [0]).
If (k!=0) {
for(i=0; iPrintf (" % d ", a [I]);
}
The else
Printf (" NULL ");
return 0;
}

CodePudding user response:

I don't know the meaning of your ListDelete return value, did not move it
Change yourself

The main problem is CreateList function, L cannot return through parameters, so the list did not create success
Added a ListPrint function, easily create and delete after check the queue of
ListDelete return what to do, not clear, very strange

CodePudding user response:

Is through ListDelete function returns the removed node of value for x after the rest of the nodes, use j to count; Excuse me, how to do can returned by each function can let the L parameters, there is no output of the program,
  • Related