A supplementary tutorial on the data structure of piglet--2.7 loop linked list in linear table
tags (space delimited): Data structure
Learning Roadmap and Learning Essentials in this section
Learning Essentials :
1. Understanding the reasons for introducing a two-way circular link list
2. Familiar with the characteristics of the two-way circulating linked list and storage structure
3. Mastering the implementation logic of some basic operations of the two-way circular chain list
4. Mastering reverse output bidirectional cyclic link list element logic
1. Introduction of two-way circular chain list
2. The storage structure of the bidirectional circular chain list
features of the two-way circular chain list :
It also said that space for time, compared to the circular list is just a pointer to the precursor
Characteristic words:
Judging empty table: l->next = l--prior = l;
Storage Structure :
typedefstruct LNode{ ElemType data; //数据域 struct LNode *prior; //前驱指针 struct LNode *next; //后继指针}LNode; typedefstruct
structure diagram of two-way circular linked list :
3. Code implementation of related basic operations 1) Building empty Tables
Status InitList(LinkList L){ L = (LinkList)malloc(sizeof(LNode)); if(!L)exit(ERROR); else L ->next = L ->prior = L; return
Logical parsing :
Very simple, is the head knot point oneself is just ~
2) Place the table empty
void ClearList(LinkList L){ =->next; //指向第一个结点 while!= L) { =->next; //指向下一个结点 free(p->//释放该结点的前驱结点 } ->=->=//自己指自己 }
3) Determine if the table is empty
Status ListEmpty(LinkList L){ return L->==&&->== L?TRUE:FALSE;}
4) Destroy the table
void DestoryList(LinkList L){ ClearList(L); free(L); NULL;}
5) Get the table length
int ListLength(LinkList L){ int0; LinkList p = L ->next; while(p != L) { i++; p = p ->next; } return
6) Get the value of element I in the table
Status Getelem (linklist l,int I,elemtype*e) {int J= 1; Linklist p=L -Next//point to first node while(p!=L&&J<I//The pointer moves back{J++; P=P -Next }if(p==L||J>IreturnERROR;//Cannot find the elementE=P -Data;returnOK; }
7) Find the element that satisfies the criteria in the lookup table
int locateelem (linklist l,elemtype e,status (* Compare) (Elemtype,elemtype)) {int i = 0 ; linklist p = L -> next - Next; //points to the first node while (P != L -> next) {I++ ; if (Compare (P-> data , E)) return i; P = P -> next; } return 0 ; //not Found, returns 0 }
8) obtain a direct precursor to a node
Status Beforeelem (linklist l,elemtype Choose,elemtype*Before) {linklist P=L -Next -Next//points to the second node while(p!=L//Does not point to the head node{if(p -Data ==Choose) {Before=P -Prior -Data;returnOK; } p=P -Next }returnERROR;}
9) obtain a direct successor to a node
status nextelem (linklist l,elemtype Choose,ElemType Span class= "Hljs-subst" >* behind) {linklist P = L - next -> next; //points to the second node while (P != L) {if (P -> prior - data == choose) {behind Span class= "Hljs-subst" >= P -> data ; return OK; } p = P -> next; } return ERROR;}
10) return address of element I
LinkList GetElemAdd(LinkList L,int i){ int j; LinkList p = L; if0 || i > ListLength(L))returnNULL//判断i值位置是否合法 for1;j < = i;j++) { p = p ->next; } return p;}
11) Insert the element into the first position
Status Listinsert (linklist l,int i,elemtype e) {linklist p,q;//Determine if I value is legal if(I< 1 ||I>Listlength (L)+ 1)returnERROR; P=Getelemadd (l,i- 1);//null's words indicate that the precursor of the first I node does not exist, //This assumes that the head node is the precursor of the first junction. if(!PreturnERROR; Q=(linklist) malloc (sizeof (Lnode));if(!QreturnERROR; Q -Data =E//Assign a value to a new node.Q -Prior=P//The precursor of the new node is the first i-1 node .Q -Next=P -Next//The PostScript of the new node is the first node .P -Next -Prior=Q//I node precursor points to the new node.P -Next=Qthe successor of the I-1 node points to a new node returnOK; }
To implement a logical diagram :
12) Remove elements from position I
Status listdelete (linklist l,int I,elemtype*e) {linklist p;if(I< 1)returnERROR;//Determine if delete location is legalP=Getelemadd (L,i);if(!PreturnERROR;//NULL indicates that the element I does not existE=P -Data; P -Prior -Next=P -Nextthe subsequent point of the//i-1 node points to a i+1 nodeP -Next -Prior=P -Prior//The precursor of the I+1 node points to the first i-1 node .Free (p);//Release the first node returnOK; }
To implement a logical diagram :
Hey, do you think it's less a basic operation to traverse the elements of the table, do not worry, we write an example below,
Iterate through the linked list in a positive order, and iterate through all the elements in the table ~
4. Simple example: positive and reverse traversal of elements in a table
Run :
Code Implementation :
#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef intElemtype;typedef intStatus;typedef structlnode{elemtype data;//Data fields structLnode *prior;//precursor pointer structLnode *next;//successor Pointers}lnode;typedef structLnode *linklist;//Define a method to create N nodesLinklist Listcreate (intN) {linklist p,q,head;intI,data; Q = head; Head = (linklist)malloc(sizeof(Lnode)); Head->prior = head; Head->next = head; p = head; for(i =0; i < n;i++) {printf("Please enter the value of%d nodes:", i +1);scanf("%d", &data); Q = (linklist)malloc(sizeof(Lnode)); Q->data = data; P->next = q; Q->prior = p; Q->next = head; Head->prior = q; p = q; }returnHead }//Define a method for printing node data voidPrintnode (Elemtype e) {printf("%d\t", e); }//Define a positive-order output linked list method voidListtraverse (linklist L) {linklist p = l->next;//point to the first dollar node while(p!=l) {Printnode (p->data); p = P->next; }printf("\ n"); }//Define a method for reverse output list voidListtraverseback (linklist l) {linklist p = L->prior;//point to the last node while(p!=l) {Printnode (p->data); p = P->prior; }printf("\ n"); }intMain () {linklist p;intN =0;printf("Please enter the number of nodes in the doubly linked list:");scanf("%d", &n); p = listcreate (N);printf("node in the positive sequence printing list: \ n"); Listtraverse (P);printf("Reverse list of nodes in a printed list: \ n"); Listtraverseback (P);return 0; }
Very simple, not a BB ~
5. The sample code for this section is downloaded:
Https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/List/list5.c
Https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/List/list6.c
A supplementary tutorial on the data structure of piglet--2.7 loop linked list in linear table