C-language implementation of linked list (III.)

Source: Internet
Author: User
Tags exit header printf

The basic operation of single linked list
After creating a single linked list, what if you want to do something like insert, delete, and so on? So it is necessary to master some of the basic algorithms of single linked list to achieve these operations. The basic operations of a single linked list include: Find, insert, and delete. Below we will introduce these three basic arithmetic algorithms, and combine with us to create a single linked list of examples to write the corresponding program.
1, find
The idea of finding the single linked list is as follows: The node of the single linked list is scanned in order to detect whether the data field is the value we want to check, if the pointer returns the node, return NULL.
Because in the chain of single linked list contains the storage address of subsequent nodes, so when we realize, as long as we know the head pointer of the single linked list, we can check the data field of each node sequentially.
Here is an example of an application lookup algorithm:
#include <stdio.h>
#include <malloc.h>
#include <string.h>/* header file containing some string processing functions.
#define N 10

typedef struct NODE
{
Char name[20];
struct node *link;
}stud;

Stud * creat (int n)/* Create a function of a list
{
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 list function where h pointer is the table header pointer of the list, x pointer is the name of the person to look for.
{
Stud *p; /* Current pointer to the node you want to compare to the name you are looking for * *
Char *y; /* Hold the pointer to the name in the data field of the node.
p=h->link;
while (P!=null)
{
y=p->name;
if (strcmp (y,x) ==0)/* Compare the name in the data field with the name you want to find, and if the same, return 0, that is, the condition is set up * *
return (p); /* Return to the address to find the node/*
else p=p->link;
}
if (p==null)
printf ("No find this data!");
}

Main ()
{
int number;
Char fullname[20];
Stud *head,*searchpoint; /*head is a pointer to a table, Searchpoint is a pointer to save the node address that matches the condition.
Number=n;
Head=creat (number);
printf ("Please enter the name of the person you are looking for:");
scanf ("%s", FullName);
Searchpoint=search (Head,fullname); /* Call the LOOKUP function and assign the result to the Searchpoint pointer.
}

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.