Linked list of data structures and algorithms

Source: Internet
Author: User

In software design, the two most commonly used data storage structure is sequential storage structure and chain storage structure, the most used in sequential storage structure is the array, and the chain storage structure used more should be single-linked list and their deformation.

There is only one pointer to the next node in the single-linked list, and the next pointer of the last element is null; the difference between a circular linked list and a single linked list is that the last pointer points to the head node; In a doubly linked list, each node has not only a next pointer to the next node, There is also a prior pointer to the previous node, and the next of the prior and tail nodes of the head node is null. Because all are single-linked list deformation, so here the main analysis of single-linked list.

Unlike sequential storage spaces, chained storage spaces are not contiguous, and the elements are accessed directly through an index in an array. In a single-linked list, it finds the next node through the next pointer in each node.

1, single linked list of the structure of the body

The structure of a single-linked list contains two elements, one is the data element, and the other refers to the address of a node that is down. Data types in the following structures can be either basic data types or complex data types.

struct singlelistnode{    int  data;     *next;} ListNode;

2, single-linked list creation

If the return value is NULL, the creation fails or the creation succeeds.

listnode* allocnode (int  data) {    *pnode;     = (listnode*)malloc(sizeof(ListNode));     if (NULL! = pnode) {        memset (pnode,0,sizeof(ListNode));        Pnode->data = data;    }     return Pnode;}

3. Single-linked list lookup

Returns a node address if the corresponding element is found, otherwise null is returned.

listnode* Finddatainsinglelist (ConstListNode *plistnode,intdata) {ListNode*Pnode; if(NULL = =Plistnode)returnNULL; if(Data = = Plistnode->data)return(listnode*) Plistnode; Pnode= plistnode->Next;  while(pnode) {if(Data = = Pnode->data)returnPnode; Pnode= pnode->Next; }    returnNULL;}

4, single-linked list insertion

Locate the last node of the list, which is the next null node, and then create a new node insert.

BOOLInsertnodeintosinglelist (ListNode **plistnode,intdata) {ListNode*pnode,*Pplistnode; if(NULL = =Plistnode)return false; Pnode=Createsinglelistnode (data); if(NULL = =Pnode)return false; if(NULL = = *Plistnode)*plistnode =Pnode; Else{Pplistnode= *Plistnode;  while(pplistnode->next) Pplistnode= pplistnode->Next; Pplistnode->next =Pnode; }    return true;}

5, single linked list of the deletion

Find the node pnode that the element equals data, use the pointer ppre to represent the previous node of Pnode, point Ppre next to the next node of Pnode, and then free Pnode.

BOOLDeletefromsinglelist (ListNode **plistnode,intdata) {    if(NULL = = Plistnode | | NULL = = *Plistnode)return false; ListNode*pnode = *plistnode->Next; if(*plistnode->data = =data) {         Free(*plistnode);*plistnode =Pnode; return true; } ListNode*ppre = *Plistnode;  while(pnode) {if(Pnode->data = =data) {Ppre= pnode->Next;  Free(Pnode); return true; } ppre=Pnode; Pnode= pnode->Next; }    return false;}

6. Release the linked list

The linked list is created dynamically by Mallic, so you must free it after each run to avoid memory leaks.

void freesinglelist (ListNode *plistnode) {    if(NULL = = plistnode        )  return  ;     *pnode = NULL;      while (plistnode) {        = plistnode->next;          Free (Plistnode);         = pnode;    }}

About the algorithm of the linked list:

The most commonly used is the speed of the pointer, 7th, 8, 9 is the idea.

There is also the concept of a third pointer. In fact, the operation of the linked list is the operation of the pointer, understand the list first to understand the pointer. Here are a few common list algorithms.

7. Find the middle node of the linked list.

With two quick and slow hands, fast hands each walk two steps, slow hands each step, when the fast pointer to the tail of the list, the slow pointer to the node is the middle node.

listnode* Findmidpositionoflist (ConstListNode *Plistnode) {ListNode*pquick = Plistnode,*pslow =Plistnode:if(NULL = =Plistnode)returnNULL;  while(Pquick) {Pslow= pslow->Next; Pquick= pquick->Next; if(NULL = =Pquick) Break; Pquick= pquick->Next; }    returnPslow;}

8. Find the bottom of the K-node

The quick pointer first Walk K step, then the speed pointer goes together, knows the quick pointer to go to the list end, the slow pointer is the reciprocal K node.

listnode* Findnodecountbackwards (ConstListNode *plistnode,intk) {    if(NULL = =Plistnode)returnNULL; ListNode*pquick = Plistnode,*pslow =Plistnode: while(k--){        if(NULL = =Pquick)returnNULL; Pquick= pquick->Next; }     while(Pquick) {Pquick= pquick->Next; Pslow= pslow->Next; }    returnPslow;}

9. Determine if the chain list has a ring

Fast hands each walk two steps, slow hands each step, when the fast pointer to NULL, indicating that the list is definitely not a circular link list, otherwise, when the fast pointer = = Slow pointer, that is, fast pointer chasing a slow pointer, indicating that the chain list has a ring.

BOOLJudgecircleinlist (ConstListNode *Plistnode) {    if(NULL = =Plistnode)return false; ListNode*pquick = Plistnode,*pslow =Plistnode: while(Pquick) {Pquick= pquick->Next; if(NULL = =Pquick)return false; Pquick= pquick->Next; Pslow= pslow->Next; if(Pquick = =Pslow)return true; }}

10. Determine if the two linked list has a common node

There are many ways to judge whether there are public nodes in the two lists, the simplest and most brutal is the exhaustion search, but the time complexity is O (N2). There is a linked list of the tail node and the first node of another node to form a linked list, and then determine whether the list has a ring. Of course, there are other methods, here only a kind of personal think that the relatively simple method, since there are public nodes, then they are from a node after the beginning of the node are equal, so their last node must be equal. So it is only necessary to determine whether the last node of the two list is equal.

bool judgecommonnode (const ListNode *plist1,const ListNode *pList2) {    if (NULL = = PList1 | | NULL = = pList2        )returnfalse;      while (plist1->next)         = Plist1->Next;      while (plist2->next)         = Plist2->Next;         return plist1==pList2;}

11. Find out the first common node of two lists

Two lists have common nodes, then they are equal from the last node to the first common node, and the same length, we just need to start the search from the two linked lists at the same distance from the tail node, we can find the first common node.

intCountsizeoflist (ConstListNode *Plistnode) {    if(NULL = =Plistnode)return 0; intCount =0;  while(plistnode) {count++; Plistnode= plistnode->Next; }    returncount;} ListNode* Findfirstcommonnode (ConstListNode *plist1,ConstListNode *pList2) {    if(NULL = = PList1 | | NULL = =pList2)returnNULL; intSize1 =countsizeoflist (PLIST1); intSize2 =countsizeoflist (PLIST2); if(Size1 >size2) {        intindex = size1-Size2;  while(index--) PList1= plist1->Next; }    Else if(Size1 <size2) {        intindex = size2-size1;  while(index--) PList2= plist2->Next; }     while(pList1) {if(PList1 = =pList2)returnPList1; PList1= plist1->Next; PList2= plist2->Next; }}

12, the chain list reversal

The reversal of the

list can be handled with a stack, which is recursion, but the recursion itself is too inefficient, so here we use loops to deal with it. Use one node to save the previous node, save the next node with another node, and then process the node.

 listnode* reverselist (listnode *plistnode) {listnode  *pguard = Null,*pnode    = Plistnode;  if  (NULL == Plistnode)  return   NULL;    Pguard  = Plistnode->next;    Plistnode ->next = NULL;    Plistnode  = Pguard;  while  (Plistnode->next) {Pguard 
    = plistnode->next;        Plistnode ->next = Pnode;    Pnode  = Plistnode;  return   Plistnode;}  

Full code See: Https://github.com/whc2uestc/DataStructure-Algorithm/tree/master/list

Linked list of data structures and algorithms

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.