C-language implementation of linked list (v)

Source: Internet
Author: User
Tags exit printf strcmp

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

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.