Blog: Http://blog.csdn.net/muyang_ren
About the two-way linked list principle Many have explained, I this is just with the front of the kernel linked list to make a comparison, the same implementation of data deletion and change.
:
1, MAIN.C
#include "doublelist.h" int main (void) {int num, I; Double_plist list; Doublelist_init (&list); Initialize the bidirectional list printf ("\ n Please enter the list length:"); scanf ("%d", &num); Added printf ("----------------①----------------\ n"); printf ("----------------add----------------\ n"); for (i=1;i<=num;i++) {printf (">>> add%d\n", I); Doublelist_addnum (List,i); Add data to the doubly linked list}//Check printf ("----------------②----------------\ n"); printf ("-----------------display----------------\ n"); Doublelist_show (list); Display data in doubly linked list//delete printf ("----------------③----------------\ n"); printf ("----------------Delete----------------\ n"); printf ("Please enter the data to be deleted \ n"); scanf ("%d", &num); Doublelist_del (List,num); Re-check printf ("----------------display----------------\ n"); Doublelist_show (list); Display data in the doubly linked list//modify printf ("\ n----------------④----------------\ n"); printf ("----------------added----------------\ n "); printf (">>>: N m, then N is modified to m\n"); scanf ("%d%d", &i, &num); Doublelist_revise (list, I, num); Display the data in the doubly linked list//and then check printf ("----------------display----------------\ n"); Doublelist_show (list); Show the data in the doubly linked list return 0;}
2. header file
Doublelist.h
#ifndef __doublelist_h__#define __doublelist_h__#include <stdio.h> #include <stdlib.h> #include < stdbool.h>typedef int datatype;typedef struct doublelist{ datatype data; struct doublelist *next, *prior;} Double_list,*double_plist;extern void Doublelist_new (Double_plist * L); add extern void Doublelist_init (Double_plist * l);//Initialize header extern void Doublelist_show (Double_plist L);//check extern void Doublelist_addnum (double_plist, int), extern void Doublelist_del (double_plist, int);//delete extern void Doublelist_revise ( double_plist, int, int);//Change #endif
3. Bidirectional linked list implementation function
Doublelist.c
#include "doublelist.h"//Open a new node space void Doublelist_new (Double_plist *list) {*list= (double_plist) malloc (sizeof (double _list)); if (NULL = = *list) {perror ("malloc\n"); Exit (-1); }}//doubly linked list initializes void Doublelist_init (Double_plist *list) {*list= (double_plist) malloc (sizeof (double_list)); if (NULL = = *list) {perror ("malloc\n"); Exit (-1); } (*list)->next= (*list)->prior= (*list);} Add a node to the doubly linked list header and assign a value of void Doublelist_addnum (double_plist list,int num) {double_plist new; Doublelist_new (&new); Open up new space for new->data = num; new->prior=list->prior; new->prior->next=new; new->next=list; List->prior=new;} Check//Display data in the doubly linked list void Doublelist_show (double_plist list) {int i=1; Double_plist p; For (p=list->next; p!=list, P=p->next) {while (i++%6==0) {printf ("\ n"); } printf ("%d\t", p->data); } printf ("\ n");} Delete void Doublelist_del (double_plist list,int num) {int flag=0;Flag Locate the deletion point of the marker bit double_plist p; for (p=list->next; p!=list; p=p->next) {//if the value corresponding to the linked list is found if (num = = p->data) {P->prior-> ;next=p->next; p->next->prior=p->next; flag=1; Flag position 1; free (p); }}//If the value of the linked list is not found if (flag==0) {printf ("* * * does not find the corresponding value in the list ***\n"); }}//modify void Doublelist_revise (Double_plist list,int old_num, int new_num) {int flag=0; Flag Locate the deletion point of the marker bit double_plist p; for (p=list->next; p!=list; p=p->next) {//if the value corresponding to the linked list is found if (old_num = = p->data) {P->data = New_num; flag=1; Flag position 1;}}//If the value of the linked list is not modified if (flag==0) {printf ("* * * does not find a corresponding value in the list ***\n"); }}
Two-way list of data structure of Hei Yang Sheep series