3, delete
If we already know the location of the node p to be deleted, the P node should be deleted as long as the link field of the precursor node of the p node is stored as the address of the secondary node of the store P node, and the P node can be recycled.
The following are examples of application deletion algorithms:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define N 10
typedef struct NODE
{
Char name[20];
struct node *link;
}stud;
Stud * creat (int n)/* Create a new list of functions * *
{
Stud *p,*h,*s;
int i;
if ((h= (Stud *) malloc (sizeof (stud))) ==null)
{
printf ("Cannot allocate memory space!");
Exit (0);
}
H->name[0]= ' ";
h->link=null;
P=h;
for (i=0;i<n;i++)
{
if ((s= (Stud *) malloc (sizeof (stud))) ==null)
{
printf ("Cannot allocate memory space!");
Exit (0);
}
p->link=s;
printf ("Please enter the name of%d person", i+1);
scanf ("%s", s->name);
s->link=null;
P=s;
}
return (h);
}
Stud * Search (stud *h,char *x)/* Lookup function * *
{
Stud *p;
Char *y;
p=h->link;
while (P!=null)
{
y=p->name;
if (strcmp (y,x) ==0)
return (p);
else p=p->link;
}
if (p==null)
printf ("No find this data!");
}
Stud * SEARCH2 (stud *h,char *x)//Another lookup function that returns a pointer to the direct predecessor node of the previous lookup function.
/*h is the table head pointer, x is the pointer to the name you want to find.
/* In fact, the algorithm of this function is the same as the search algorithm above, just one more pointer s, and s always point to the direct precursor of the node pointed to by the pointer p.
/* Result returns s that is the previous node of the node to find.
{
Stud *p,*s;
Char *y;
p=h->link;
S=h;
while (P!=null)
{
y=p->name;
if (strcmp (y,x) ==0)
return (s);
Else
{
p=p->link;
s=s->link;
}
}
if (p==null)
printf ("No find this data!");
}
void del (stud *x,stud *y)/* Delete function, where y is the pointer to the node to be deleted, X is the pointer to the previous node of the node to be deleted.
{
Stud *s;
S=y;
x->link=y->link;
Free (s);
}
Main ()
{
int number;
Char fullname[20];
Stud *head,*searchpoint,*forepoint;
Number=n;
Head=creat (number);
printf ("Please enter the name of the person you want to delete:");
scanf ("%s", FullName);
Searchpoint=search (Head,fullname);
FOREPOINT=SEARCH2 (Head,fullname);
Del (forepoint,searchpoint);