CodePudding user response:
To write about yourself, such as to create a leading the first node of the linked list, and then the output list, these two functions, first back to write function to deleteCodePudding user response:
Basic meet the requirements, if there is a bug not detailed test
# define _CRT_SECURE_NO_WARNINGS
# include & lt; stdio.h>
# include & lt; Stdlib. H>
Typedef struct node
{
int data;
Struct node * next;
} linknode, * linklist;
//create a list
Linklist createLink ()
{
Linklist pHead=(linknode *) malloc (sizeof (linknode));
If (pHead==NULL)
{
Printf (" memory allocation failure \ n ");
return NULL;
}
PHead - & gt; Data=https://bbs.csdn.net/topics/0;
PHead - & gt; Next=NULL;
Return pHead;
}
//list assignment
Void invoke the (linklist pHead)
{
int a;
Linklist pTemp=NULL, pCurr=pHead;
Printf (" input the value of the linked list node - the end of 1 \ n ");
While (1)
{
The scanf (" % d ", & amp; A);
If (a!=1)
{
PTemp=(linknode *) malloc (sizeof (linknode));
PTemp - & gt; Data=https://bbs.csdn.net/topics/a;
PTemp - & gt; Next=NULL;
PCurr - & gt; Next=pTemp;
PCurr=pTemp;
}
The else
{
break;
}
}
}
//output list
Void printLink (linklist pHead)
{
Linklist pTemp=pHead - & gt; next;
While (pTemp!=NULL)
{
Printf (" % d ", pTemp - & gt; The data);
PTemp=pTemp - & gt; next;
}
printf("\n");
}
//delete node of x is not more than y values
Void deletedata (linklist L, int x, int y)
{
Linklist pOne=L, pTwo=L - & gt; Next, pTemp=NULL;
//traverse to find qualified node
While (pTwo!=NULL)
{
If (pTwo - & gt; The data & gt; X & amp; & PTwo - & gt; The data & lt;=y)
{
If (pTwo - & gt; Next!=NULL)
{
POne - & gt; Next=pTwo - & gt; next;
PTemp=pTwo;
Free (pTemp);
PTemp=NULL;
PTwo=pOne;
}
The else
{
POne - & gt; Next=NULL;
Free (pTwo);
PTwo=pOne;
}
}
POne=pTwo;
PTwo=pTwo - & gt; next;
}
}
Int main ()
{
Linklist pHead;
PHead=createLink ();
Invoke the (pHead);
PrintLink (pHead);
The deletedata (pHead, 60, 70);
PrintLink (pHead);
system("pause");
return 0;
}
CodePudding user response:
The deletedata function can be simplified as the followingVoid deletedata (linklist L, int x, int y)
{
Linklist pOne=L, pTwo=L - & gt; Next, pTemp=NULL;
//traverse to find qualified node
While (pTwo!=NULL)
{
If (pTwo & amp; & PTwo - & gt; The data & gt; X & amp; & PTwo - & gt; The data & lt;=y)
{
POne - & gt; Next=pTwo - & gt; next;
PTemp=pTwo;
Free (pTemp);
PTemp=NULL;
PTwo=pOne;
}
POne=pTwo;
PTwo=pTwo - & gt; next;
}
}
CodePudding user response:
Amend the main to bestInt main ()
{
Int x, y;
Linklist pHead;
PHead=createLink ();
Invoke the (pHead);
PrintLink (pHead);
The scanf (" % d % d ", & amp; X, & amp; Y);//it is pointed out that the x, y the scope of the
The deletedata (pHead, x, y);
PrintLink (pHead);
system("pause");
return 0;
}
CodePudding user response:
//delete node of x is not more than y values
Void deletedata (linklist L, int x, int y)
{
Linklist pOne=L, pTwo=L - & gt; Next, pTemp=NULL;
//traverse to find qualified node
While (pTwo!=NULL)
{
If (pTwo - & gt; The data & gt; X & amp; & PTwo - & gt; The data & lt;=y)
{
PTemp=pTwo;
POne - & gt; Next=pTwo - & gt; next;
Free (pTemp);
PTwo=pOne - & gt; next;
continue;
# if 0
If (pTwo - & gt; Next!=NULL)
{
POne - & gt; Next=pTwo - & gt; next;
PTemp=pTwo;
Free (pTemp);
PTemp=NULL;
PTwo=pOne;
}
The else
{
POne - & gt; Next=NULL;
Free (pTwo);
PTwo=pOne;
}
# endif
}
POne=pTwo;
PTwo=pTwo - & gt; next;
}
}
For your reference ~
Upstairs write no problem, the revised on the basis of the above is my in his, can compare ~