C-language bi-directional link list Detailed and example code _c language

Source: Internet
Author: User
Tags strcmp

1. Introduction of bidirectional linked list.

Two-way linked list is also called double linked list, which is a kind of linked list, and it has two pointers in each data node, pointing to direct successor and direct precursor respectively. Therefore, starting from any node in a two-way list, it is easy to access its predecessor nodes and subsequent nodes. In general we construct bidirectional cyclic lists.

2, example requirements:

To complete the insertion, deletion and lookup of the bidirectional linked list, the array used by student management system is realized in the way of bidirectional linked list, which can support the unlimited number of students to check and delete and save.

3, code implementation.

#include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> typedef struct S
  tudent{Char name[20];
  int score;
Char phonenum[14];


} str_student;
  typedef struct node{str_student data;     struct Node *prior;     Pointing to the Precursor node struct node *next;


Point to successor node}node, *dlinklist;
  Initializes a student list of dlinklist initdoulinklist () {Node *l,*p,*r;
  Char name[20];
  Char phone[14];
  int score;
  L = (node *) malloc (sizeof (node));
  L->next = NULL;
  R = L;


  R->next = NULL;
    while (1) {p = (node *) malloc (sizeof (node));
    printf ("Input name is out Exit,input student name:\n");
    scanf ("%s", name);
    if (strcmp (name, "Out") ==0} {break;
    } strcpy (P->data.name, name);
    printf ("Input student Score:");
    scanf ("%d", &score);
    P->data.score = score;
    printf ("Input student Phone:");
    scanf ("%s", phone);


    strcpy (P->data.phonenum, phone);
    P->next = r->next;
    R->next = p;r = P;
  } r->next = NULL;
return L;
  ///Add Student Information dlinklist insertdoulinkliststuent (dlinklist l,int i,char *name, int score,char *phonenum) {DLinkList p,s;
  p = l->next;
  int tempi;
  for (tempi = 1;tempi < i-1; tempi++) p = p->next;
  s = (node *) malloc (sizeof (node));
  S->data.score = score;
  strcpy (S->data.name,name);
  strcpy (S->data.phonenum,phonenum);
  S->next = p->next;
  P->next->prior = s;
  S->prior = p;


  P->next = s;
return L;
  //Find student Information int finddoulinkliststudent (dlinklist L,char *name) {dlinklist p;
  p = l->next;


  int i = 1;
    while (P!= NULL && (strcmp (p->data.name, name)!=0)) {++i;
  p = p->next;
  } if (P = NULL) return 0;
else return i;
  //Remove a student dlinklist removedoulinkliststudent (dlinklist L,char *name) {int tempi = 1;
  Dlinklist p;
  p = l->next;
  int i =finddoulinkliststudent (l,name); while ((tempi++)!= i && p!= NULL) {p = p->next;
  } if (p = = NULL) printf ("No list \ n");
    else if (P->next = null) {p->prior->next = null;
  Free (p);
    else {P->prior->next = p->next;
    P->next->prior = p->prior;
  Free (p);
return L;
  }//Paving aid printing information void Printfinfo (Dlinklist L) {dlinklist p;
  p = l->next;
    while (P!=null) {printf ("Student name%s\n", P->data.name);
    printf ("Student name%d\n", P->data.score);
    printf ("Student name%s\n", P->data.phonenum);
  p=p->next;
  } void Main () {char name2[20]= ' hanmeimei ';


  Char phone2[14]= "13612345678";
  Dlinklist L =initdoulinklist ();
  2.1 Initialization of student bidirectional linked list data insertdoulinkliststuent (L,1,NAME2,99,PHONE2);


  Printfinfo (L);
  2.2 Find Students Zhangsan finddoulinkliststudent (L, ' Zhangsan ');


  Printfinfo (L);
  2.3 Delete student Zhangsan removedoulinkliststudent (L, ' Zhangsan ');


  Printfinfo (L);
  2.4 Add students to Zengteng insertdoulinkliststuent (l,9, ' Zengteng ', 89, ' 13643345667 ');


Printfinfo (L);



}



 

Above is the C language two-way linked list of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.