C-language implementation of linked list (vii)

Source: Internet
Author: User
Tags exit printf

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);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.