Review knowledge--the realization of single linked list

Source: Internet
Author: User

  A linked list is a non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the order of the pointers in the linked list. A linked list consists of a series of nodes (each element in the list is called a node) that can be dynamically generated at run time. Each node consists of two parts: one is the data field that stores the data element, and the other is the pointer field that stores the next node address. the operation is complex compared to the linear table order structure. Since they do not have to be stored sequentially, the list can achieve an O (1) complexity at the time of insertion, much faster than another linear table-order table, but the time to find a node or to access a particular numbered node requires O (n), while the corresponding time complexity of the linear and sequential tables is O (Logn) and O (1) respectively.

1 int Elemtype; 2 struct Lnode {3    elemtype data; 4     struct Lnode *Next; 5 }lnode,*linklist;
single-linked list storage structure
1 //single-linked list initialization2 linklist initlist () {3 linklist list;4List =NewLnode;5List->next =NULL;6List->data =0;7     returnlist;8 }9 //Node InitializationTenlnode*Initnode (elemtype value) { OneLnode *node; Anode =NewLnode; -Node->data =value; -Node->next =NULL; the     returnnode; -}
single-linked list initialization
1 //inserting nodes into a single-linked list head2 voidPush_front (linklist &list, lnode*node) {3 linklist temp;4temp = list->Next;5List->next =node;6List->next->next =temp;7 }8 //insert nodes at the end of a single linked list9 voidPush_back (linklist &list, lnode*node) {Ten linklist temp; Onetemp =list; A      while(Temp->next! = NULL) temp = temp->Next; -Temp->next =node; - } the //inserting nodes based on location - voidInsert (linklist &list,lnode* node,intindex) { -     intK =1, ans =1; - linklist temp; +temp =list; -      while(Temp->next! =NULL) {  +temp = temp->Next; Aans++;  at     } -         //inserts a node into the list header if the insertion position is less than or equal to the 1th number bit -     if(Index <=1) { -temp = list->Next; -List->next =node; -List->next->next =temp; in     } -         //if the inserted position is greater than or equal to the maximum node position, insert the node into the tail of the list to     Else if(Index >=ans) { +temp =list; -          while(Temp->next! = NULL) temp = temp->Next; theTemp->next =node; *     } $         //inserts a node into the list according to the insertion position indexPanax Notoginseng     Else { - linklist p; thetemp =list; +          while(K <index) { Ak++; thetemp = temp->Next; +         } -p = temp->Next; $Temp->next =node; $Temp->next->next =p; -     } -}
node insertion for single-linked lists
1 //Delete First node2 voidPop_front (Linklist &list) {3linklist temp =list;4temp = temp->next->Next;5     DeleteList->Next;6List->next =temp;7 }8 //Delete last node9 voidPop_back (Linklist &list) {Tenlinklist temp =list; One      while(Temp->next->next! =NULL) Atemp = temp->Next; -     DeleteTemp->Next; -Temp->next =NULL; the } - //Delete based on node location - voidRemove (linklist &list,intindex) { -     intK =1, ans =1; + linklist temp; -temp =list; +      while(Temp->next! =NULL) { Atemp = temp->Next; atans++; -     } -     if(Index <=1) - Pop_front (list); -     Else if(Index >=ans) - pop_back (list); in     Else { - linklist p; totemp =list; +          while(K <index) { -k++; thetemp = temp->Next; *         } $p = temp->next->Next;Panax Notoginseng         DeleteTemp->Next; -Temp->next =p; the     } + } A //Delete all nodes the voidClear (Linklist &list) { +linklist temp =list; -     if(Temp->next = = NULL)return; $ Pop_front (list); $ Clear (list); -}
node deletion for single linked list
1 voidReverse (linklist &list) {2Linklist temp, p=initlist ();3p = list->Next;4List->next =NULL;5      while(p) {6temp = p->Next;7P->next = list->Next;8List->next =p;9p =temp;Ten     } One}
single-linked list rollover
1 BOOLisloop (linklist list) {2linklist p = list, q =list;3      while(P! = NULL && P->next! =NULL)4     {5p = p->next->Next;6Q = q->Next;7         //If there is a ring, then p will exceed the Q lap8         if(p = =q)9              Break;Ten     } One     if(p = = NULL | | p->next = =NULL) A         return false; -     Else -         return true; the}
whether a single linked list has a ring

Linked list is the basis of data structure, master the operation of the list of the data structure behind the understanding is very helpful.

Forget the previous knowledge is also similar, the knowledge point is also very vague, now review the previous knowledge and code, by the way, record knowledge review distance, too long did not knock code, inevitably some unfamiliar, may be some not quite correct place, also hope that spectators correct. For the flip and to determine if there is a ring these two can be debugged with the IDE, a single step to try to go a few more laps to understand why this is written.

Review knowledge--the realization of single linked list

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.