Auxiliary tutorial on data structure of piglet--2.2 single-linked list in linear table

Source: Internet
Author: User

Auxiliary tutorial on data structure of piglet--2.2 single-linked list in linear table

tags (space delimited): Data structure

Learning Roadmap and Learning Essentials in this section

Learning Essentials :

  • 1. Understand the order table and the single-linked list of their own little and shortcomings!
  • 2. Familiar with the form of single-linked lists, for head pointers, head nodes, tail nodes, data fields and pointer fields these nouns need to know what is!
  • 3. Familiar with single-linked list node structure
  • 4. Distinguish the head pointer from the head node !
  • 5. Familiar with the two ways to create a single-linked list: head-interpolation and tail-insertion method
  • 6. Understanding the logic of the 12 basic operations of a single-link list
  • 7. Interesting algorithmic questions: Find the middle node of a single linked list ~
1. Introduction of single-link list (sequential table and single-linked list PK)

2. A single-linked list of structural drawings and some nouns

3. Storage structure of single-linked table nodes

From the above structure we know that the node structure of the single-linked list is divided into data fields and pointer fields, the corresponding storage structure code is as follows:

typedefstruct LNode{    ElemType data;         //数据域    struct LNode *next;   //指针域 }LNode,*LinkList;         //定义两个只是方便使用而已,Linklist是一个结点指针哦~ 
4. Comparison of head pointer and head node

2 We see the head pointer and head node, you may be a little puzzled about these two nouns, let us explain below ~

5. The comparison between the head interpolation method and the tail interpolation method to create the table

The difference between the two methods is simply the location of the insertion:
Head interpolation: The new insertion node is never the first node at the current point
Tail Interpolation: The new insertion node is always the last node at the current point

1) Head interpolation method to build a table

Implementation code:

//Head-insert method to build a linked listvoidHeadcreatelist (linklist l,int N) {linklist p;    int i; Srand (Time (0));//Initialize random number seedL=(linklist) malloc (sizeof (Lnode)); L -Next= NULL;//Use loops to generate nodes and add them to a single linked list.for (i= 0; I<N;i++) {P=(linklist) malloc (sizeof (Lnode));//production of new nodesP -Data =RAND ()% - + 1;//Production two random number 100, three bits isP -Next=L -Next L -Next=P//Insert to table header}   }
2) tail interpolation method to build a table

Implementation code:

voidTailcraetelist (linklist L,intN) {linklist p,r;intI Srand (Time (0));//Initialize random number seedL = (linklist)malloc(sizeof(Lnode)); R = L; for(i =0; i < n;i++) {p = (linklist)malloc(sizeof(Lnode));//production of new nodesP->data = rand ()% -+1;//Production two random number 100, three bits isR->next = p;//The pointer to the end of the footer node points to the new noder = P;//define the current new node as the tail node at the end of the table} r->next = NULL;//Current linked list ends}
6. Interesting algorithm: Find the middle node of a single linked list

is to give you a single-linked list, to get you a single-linked table in the middle of the node? What would you do?
In general, we may use a pointer to the beginning and end of the record, while recording the length of the single-linked list, and then divided by 2 to get the first few
The key is the middle node, then the LENGTH/2 to get the intermediate nodes, repeated traversal is cumbersome, there is no other way?
Yes, sure there is, here's an easy way to do it:
With two different pointers, move in different moving order, here we take them as a fast and slow pointer !
Each cycle,
* * The quick pointer moves backwards by two nodes: P = P-Next next;
The slow pointer moves backwards one node: q = q-next;**
When the quick pointer reaches the tail, the slow pointer does not point to the middle node, you say yes ~
The principle is very simple, below we write the code implementation:

Status Getmidlnode (linklist*L,elemtype*e) {linklist p,q; P=Q= *L while(p -Next -Next!= NULL)    {if(p -Next -Next!= NULL) {P=P -Next -Next Q=Q -Next }Else{p=P -Next }} E=Q -Data;returnOK;}
7.12 Basic Operating code implementations

From the beginning of this section is not as a step by step to explain, directly on the code, the difficult part will write down comments!

1) Construct empty table
void InitList(LinkList L){    L = (LinkList)malloc(sizeof(LNode));    if(!L)exit(ERROR);    L ->nextNULL;}
2) Set the linked list to an empty table
void ClearList(LinkList L){    =->next;    ->=NULL;    //接着就是释放头结点以外的结点了    while(p)    {        = L->next;        free(L);  //释放首元结点        = p;    //移动到当前的首元结点     
3) Determine if the table is empty

Here are two things to differentiate:

* * with head node: L--next = null; This table is empty table!
Headless node: L = null, empty table at this time! **

Status ListEmpty(LinkList L){    //有头节点的情况,只需判断头结点的指针域是否为空即可    if->next)returnFALSE;    elsereturnTRUE;   }
4) Destroy single linked list
void DestoryList(LinkList L){    LinkList q;    //删除头结点外的结点     while(L)    {        //指向首元结点,而不是头结点             =->next;        free(L);        = q;      //删除后指向首元     
5) Get the table length
int ListLength(LinkList L){    int0;    LinkList p = L ->next;    while(p)    {        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;//point to the first element, then move back, if the end or the value of J is greater than I    //I haven't found a change element. I'm not legal .Linklist p=L -Next while(p&&J<i) {j++; P=P -Next }if(!P||J>IreturnERROR; 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)){    int0;      next;      while(p)      {          i++;          if(compare(p->data,e))return i;          next;      }      return0;  
8) obtain a direct precursor to a node.
Status BeforeElem(LinkList L,ElemType choose,ElemType *before){ LinkList q,p = L ->next; while(p ->next) { q = p ->next; //判断p的后继是否为choose,是的话返回OK,否则继续后移  if(q ->data == choose) { before = p ->data; return OK; } p = q; } return ERROR; }
9. Obtaining a direct successor to a node
*behind){    =->next;    while->next)    {        if->data== choose)        {            =->->data;            return OK;        }        =->next;    }    return ERROR;   }
10. Insert an element into the table I position
Status Listinsert (linklist l,int i,elemtype e) {int J= 0; Linklist p,q=L//Ask Q to point to the head node     while(p&&J<I- 1) {J++; P=P -Next//p refers to a node down}if(!P||J>I- 1)returnERROR; P=(linklist) malloc (sizeof (Lnode));//To have the pointer field of the inserted node point to the subsequent node of the insertion position first    //Let the pointer field of the precursor of the insertion node point to the insertion junction    //!!! The order can not be messy oh 1P -Data =E P -Next=Q -Next Q -Next=PreturnOK;}

11. Delete the first element of the table
Status Listdelete (linklist L,intI,elemtype*e){intj =0; Linklist p,Q= L; while(Q-Next&& J < i-1) {j + +;Q=Q-Next; }if(!Q | |J >i-1)returnERROR; p =Q-Next;//Point to the node you want to deleteQ-Next= P-Next;//The pointer field of the precursor that deletes the node points to the subsequent E = P->data; of the Delete node Free (p);//Release the node you want to deletereturnOK;}

12. Traverse all elements in a single linked list
void ListTraverser(LinkList L,void(*visit)(ElemType)){    =->next;    while(p)    {        ->data);        =->next;    }    printf("\n");}
8. This section of the code download:

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

Auxiliary tutorial on data structure of piglet--2.2 single-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.