The basic operation of two-way linked lists:
1, find
If we were to find a node in a bidirectional cyclic list with a header that has a specific value for the data field, we also compare the values of each node data field from the header node, and if it is that particular value, return the pointer to the node, otherwise continue looking back until the end of the table.
The following example is a program that uses a bidirectional cyclic list lookup algorithm.
#include <stdio.h>
#include <malloc.h>
#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");
}
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", *&searchpoint->name);
}