English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1Introduction to doubly linked list.
The doubly linked list, also known as the double linked list, is a type of linked list. Each data node in it has two pointers, pointing to the direct successor and direct predecessor. Therefore, starting from any node in the doubly linked list, it is very convenient to access its predecessor node and successor node. Generally, we construct a doubly circular linked list.
2Example requirements:
Complete the insertion, deletion, and search of the doubly linked list, and implement the array used by the student management system in the form of a doubly linked list, which can support the addition, deletion, modification, and search of an unlimited number of students as well as save.
3The code implementation.
#include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> typedef struct Student{ char name[20]; int score; char phoneNum[14]; } str_student; typedef struct Node{ str_student data; struct Node *prior; //points to the previous node struct Node *next; //points to the subsequent node })Node, *DLinkList; // Initialize a student linked list 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 insertDouLinkListStudent(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; } // Auxiliary 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 Initialize student two-way linked list data insertDouLinkListStuent(L,1,name2,99,phone2); printfInfo(L); // 2.2 Find student zhangsan findDouLinkListStudent(L,'zhangsan'); printfInfo(L); // 2.3 Delete student zhangsan removeDouLinkListStudent(L,'zhangsan'); printfInfo(L); // 2.4 Add student zengteng insertDouLinkListStuent(L,9,zengteng89,13643345667'); printfInfo(L); }
This is the data compilation of C language two-way linked list. We will continue to supplement relevant information in the future, thank you all for your support to this site!
Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.