A supplementary tutorial on the data structure of Piglet--2.4 circular linked list in linear table

Source: Internet
Author: User

A supplementary tutorial on the data structure of Piglet--2.4 circular linked list in linear table

tags (space delimited): Data structure

Learning Roadmap and Learning Essentials in this section

Learning Essentials :

  • 1. Understand the shortcomings of the single-linked list, exposed problems
  • 2. Know what is a circular single-link list, master the characteristics of single-linked list and storage structure
  • 3. Master the implementation logic of some basic operation of the circular link list, the best expert tear code
1. Introduction of circular single chain list

2. The characteristics of the circular list and the storage structure

Features of the circular chain list:

It also says that a little bit more Gaubig than a single-linked list is:
The last node of the linked list points to the head node, so that the formation of the so-called ring, is a circular single-linked list, hehe!
Characteristics of the words are:
We have previously judged whether the single-linked list is empty: whether head-to-next is null
A single linked list only needs to be: Head and Next are head (self)
Related operations and single-linked lists are still more similar!

storage structure: (As with a single linked list):

typedefint ElemType;typedefint Status;  typedefstruct LNode{    ElemType data;         //数据域    struct LNode *next;   //指针域 }LNode;  typedefstruct LNode *LinkList;

a single-linked list of the structure diagram :

For example, if joining our list is long, it is time-consuming to find the footer from the table header, so the loop list is often set to the tail pointer !
Instead of setting the head pointer! Tail Hands ! Tail Hands ! Tail Hands !

3. Code implementation of related basic operations 1) Constructing empty tables
Status InitList(LinkList L){    L = (LinkList)malloc(sizeof(LNode));    if(!L)exit(ERROR);    L ->next = L;   //自己指自己~(头节点指针域指向头结点)     return OK;}
2) Place the table empty
void ClearList(LinkList L){    LinkList p,q;    =->next;     //指向头结点     =->next;    //指向第一个结点     while(p!=L)    {        =->next;        free(p);        = q;    }    ->=//自己指自己 
3) Determine if the table is empty

There's a head node, OH ~

Status ListEmpty(LinkList L){    return L!=L ->next?FALSE:TRUE; }
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 -Next//point to first node    if(I<= 0||I>Listlength)returnERROR;//Determine if the insertion position is valid     while(j<i) {j++; P=P -Next } E=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 q,p=L -Next -Next//point to first nodeQ=P -Next while(q!=L -Next) {if(q -Data ==Choose) {Before=P -Data;returnOK; } p=Q//Continue to move backQ=Q -Next }returnERROR; }
9) obtain a direct successor to a node
*behind){    =->->next;  //指向第一个结点    while!= L)    {        if->data== choose)        {            =->->data;            return OK;        }    }}
10) Insert the element into the first position
Status Listinsert (linklist l,int i,elemtype e) {linklist s,p=L -Next Int J= 0;if(I<= 0 ||I>Listlength (L)+ 1)returnERROR;//Determine if the insertion position is valid    //Find the previous node of the insertion node .     while(j<I- 1) {J++; P=P -Next }//Generate new NodesS=(linklist) malloc (sizeof (Lnode)); S -Data =E//Assign E to new nodeS -Next=P -Next//new node points to the original first node .P -Next=S//The original I-1 node points to the new node.    //If the insertion position is at the end of the table, give the new footer address to the tail pointer    if(p==L) {L=S }returnOK;}

Step parsing :

Common inserts and single-linked lists are the same, common node insertion process:

The special case is that if the insertion position is a tail node , then you need to have the tail pointer point to the newly inserted tail node.
Is the above:L = s;

11) Delete the first element of the table
Status listdelete (linklist l,int I,elemtype*e) {linklist s,p=L -Next Int J= 0;if(I<= 0||I>Listlength (L))returnERROR;//Determine if delete location is legal    //Find the previous node of the insertion node .     while(j<I- 1) {J++; P=P -Next } s=P -Next P -Next=S -Next E=S -Data;if(s==L) L=P Free (q);//Release node    returnOK; }

Step parsing :

As with insertions, when you're done deleting, consider where the tail pointer points

If the deletion is the tail knot point, then also let L = p; Point to the previous node of the delete node ~

12) Traverse all elements of the loop list
void ListTraverser(LinkList L,void(*visit)(ElemType)){    =->->next;   //指向第一个结点     while!=->next)    {        ->data);        =->next;    }    printf("\n");}
4. This section of the code download:

Https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/List/list4.c

A supplementary tutorial on the data structure of Piglet--2.4 circular linked list in linear table

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.