3, delete
Deleting a node is actually a reverse operation that inserts a node. Or for a two-way cyclic list, to delete the P node in a contiguous three-node s,p,q, simply point S to the left chain field pointer to the Q,Q, and then the P node is completed.
Here is an example of using a bidirectional cyclic list deletion algorithm:
#include
#include
#include
#define N 10
typedef struct NODE
{
Char name[20];
struct node *llink,*rlink;
}stud;
Stud * creat (int n)
{
Stud *p,*h,*s;
int i;
if ((h= (Stud *) malloc (sizeof (stud))) ==null)
{
printf ("Cannot allocate memory space!");
Exit (0);
}
H->name[0]= ' ";
h->llink=null;
h->rlink=null;
P=h;
for (i=0;i〈n;i++)
{
if ((s= (Stud *) malloc (sizeof (stud))) ==null)
{
printf ("Cannot allocate memory space!");
Exit (0);
}
P-〉rlink=s;
printf ("Please enter the name of%d person", i+1);
scanf ("%s", s->name);
s->llink=p;
s->rlink=null;
P=s;
}
h->llink=s;
p->rlink=h;
return (h);
}
Stud * Search (stud *h,char *x)
{
Stud *p;
Char *y;
p=h->rlink;
while (P!=H)
{
y=p->name;
if (strcmp (y,x) ==0)
return (p);
else p=p->rlink;
}
printf ("No find this data!");
}
void print (Stud *h)
{
int n;
Stud *p;
p=h->rlink;
printf ("Data information is: \ n");
while (P!=H)
{
printf ("%s", &* (P->name));
p=p->rlink;
}
printf ("\ n");
}
void del (stud *p)
{
(P->rlink)->llink=p->llink;
(P->llink)->rlink=p->rlink;
Free (p);
}
Main ()
{
int number;
Char studname[20];
Stud *head,*searchpoint;
Number=n;
CLRSCR ();
Head=creat (number);
Print (head);
printf ("Please enter the name of the person you are looking for:");
scanf ("%s", studname);
Searchpoint=search (Head,studname);
printf ("The name of the person you are looking for is:%s\n", *&searchpoint->name);
Del (searchpoint);
Print (head);
}