Home > Back-end >  Delete in the list
Delete in the list

Time:11-27

List of create and output function is no problem, after is to perform the delete function can't normal output, on the debug, I found that is if you remove the second number, the data of the third number becomes 0 x... Such as, when I think of is to remove the first and second digits didn't connect well, but don't see problems
Void Creat (Node * h)
{
Int I, n;
The Node * p, * Node;
H - & gt; Next=NULL;
P=h;
Srand (time (0));
Printf (" please input node number: ");
scanf("%d",& N);
for (i=0; i {
The node=(*) malloc (sizeof (node));
Node - & gt; Data=https://bbs.csdn.net/topics/rand () % n;
P - & gt; Next=node;
P=node;
}
P - & gt; Next=NULL;
}

Void the Delete (Node * h)//n for the deleted data location
{
Int I, n;
Node * p=h;
Node * q;
Printf (" please input what want to delete the data: ");
scanf("%d",& N);
For (I=1; i {
P=p - & gt; Next;
}
Q=p;
P - & gt; Next=(q - & gt; Next) - & gt; Next;
Free (q - & gt; Next);
}


Void Print (Node * h)
{
Node * p;
P=h;
Printf (" data is as follows: ");
While (p - & gt; Next!=NULL)
{
P=p - & gt; Next;
Printf (" % d ", p - & gt; The data);
}
printf("\n");
}

CodePudding user response:

'd better draw a diagram to more clearly some
Modify the following
 
For (I=1; i {
P=p - & gt; Next;
}
Q=p - & gt; Next;
P - & gt; Next=q - & gt; Next;
Free (q)

CodePudding user response:

 # include & lt; stdio.h> 
#include
#include

Typedef struct node {
int data;
Struct node * next;
}Node;

//void Creat (Node * h)
Void Creat (Node * * h)
{
Int I, n;
The Node * p, * Node;
//h - & gt; Next=NULL;
//p=h;
Srand (time (0));
Printf (" please input node number: ");
scanf("%d",& N);
for (i=0; i {
The node=(*) malloc (sizeof (node));
Node - & gt; Data=https://bbs.csdn.net/topics/rand () % n;
If (* h==NULL) {
(* h)=node;
P=(* h);
} else {
P - & gt; Next=node;
P=node;
}
}
P - & gt; Next=NULL;
}

///void the Delete (Node * h)/n to be deleted data location
Void the Delete * * h (Node)//n for the deleted data location
{
Int I, n;
Node * p=* h;
Node * q;
Printf (" please input what want to delete the data: ");
scanf("%d",& N);

If (n==1) {
* * h=(h) - & gt; Next;
Free (p);
return;
}
For (I=1; i {
if (! P)
break;
P=p - & gt; Next;
}
if (! P) {
Printf (" Can 't find it! \n");
return;
}

//q=p;
Q=p - & gt; Next;
if (! Q) {
Printf (" Can 't find it! \n");
return;
}
//p - & gt; Next=(q - & gt; Next) - & gt; Next;
P - & gt; Next=q - & gt; Next;
//free (q - & gt; Next);
Free (q);
}


Void Print (Node * h)
{
Node * p;
P=h;
Printf (" data is as follows: ");
//while (p - & gt; Next!=NULL)
While (p!=NULL)
{
Printf (" % d ", p - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}

Int main (void)
{
The head Node *=NULL;

Creat (& amp; The head);
Print (the head);
Delete (& amp; The head);
Print (the head);
return 0;
}

For your reference ~

Create a list of the building Lord, delete list and output have problem ~
  • Related