Linked list and array simulation chain list

Source: Internet
Author: User

Reprint please indicate the source, part of the content quoted from Baidu Encyclopedia, rectification "C program design", Snail King's struggle history of the Great God's Blog


Pre-Knowledge: C Language Primer


array of the Gospel of the party (this Konjac konjac is not a pointer to the list, but it seems as if everyone is holding the pointer)
first, we need to know what a linked list
Baidu Encyclopedia
do not understand do not spray (after all, Baidu Encyclopedia is not used to let people understand)
we can derive the characteristics of the linked list:
A linked list is a non-sequential, non-sequential storage structure on a physical storage unit
extracting the main predicate: A linked list is a storage structure. I think that's the nature of the list--a data structure.
So what does non-continuity and nonlinearity mean? This indicates that the memory of the linked list is not contiguous, and the next element stored in the last address of the previous element is not necessarily the next. The linked list strings The elements in the list through a reference to the address of the next element.
The list is divided into three categories: one-way linked list, two-way linked list, a circular link list
1, unidirectional linked list
a one-way list is the simplest form of a list. We refer to the most basic data in the list as node, each node contains a data block and a pointer to the next node, the list has a head pointer variable, the figure is represented by head. As you can see, head points to the first element, and the first element points to the second element ... Until the last element, the element no longer points to another element, which is called the footer, its address part is empty, and the list ends.

As you can see, to find an element in a linked list, you must first find the previous element, based on the address of the next element it provides to find the next element. If you do not provide a head pointer, the entire list is inaccessible. The list is like a chain, tied up, and cannot be broken in the middle.
in order to understand what is linked list, play a popular analogy: kindergarten teachers lead the children out for a walk, the teacher holding the first child's hand, the first child's other hand holding the second child ... This is a chain, the last child has a hand empty, he is the tail of the chain. To find this team, you must find the teacher and then order to find each child.
Variable declaration:

Const int maxn=1010; The struct node{    //pointis the pointer, and data is an int point,data that needs to be maintained      ;} A[MAXN]; int head,cnt;    // head pointer, CNT is memory pool counter

Establish:

head=++cnt;        // set head to no practical Sentinel a[head].data=0;

Insert (insert data now to K-element):

Add (++k,now);//Enter, because the calculation takes the sentry into account, so enter when ++kvoidAddintKintNow ) {     for(intI=head;i;i=a[i].point)//go through the list from the beginning of the pointer        if(! (--k))//reach insertion Position{a[++cnt].point=a[i].point;//points The pointer of the newly inserted node to the successor of the insertion positiona[i].point=cnt;//point the pointer of the precursor node to the new insertion nodeA[cnt].data=Now ;  Break; }}


Delete (delete element K):

 del (++k); //  enter  void  del (int   K) { for  (int  i=head;i;i=a[i].point"  if  ((--k) ==1< /span>) //  find precursor          {a[i].point  =a[a[i].point].point; //             break         ; }}


Traversing a linked list:

 for (int i=a[head].point;i;i=a[i].point)    cout<<a[i].data<<endl;

2, doubly linked list
As the name implies, a doubly linked list is a linked list with two directions. Unlike a one-way list, each node in a doubly linked list stores not only a pointer to the next node, but a pointer to the previous node. Its advantage is that access, insert, delete more convenient. But "space for Time".

Variable declaration:

struct node{    int pre,nxt,data;    // precursors and Successors }A[MAXN]; int head,cnt;

Insert:

voidAddintKintNow ) {     for(intI=head;i;i=a[i].nxt)if(! (--k)) {a[++CNT].NXT=A[I].NXT;//These two are the same as the single linked list .a[i].nxt=CNT; A[a[cnt].nxt].pre=cnt;//update the successor predecessor to the new insertion nodeA[cnt].pre=i;//set the predecessor of the new Insert node as its predecessorA[cnt].data=Now ;  Break; }}


Delete:

void del (int  k) {    for (int i=head;i;i=a[i].nxt)          if (! (--k))        {            a[a[i].pre].nxt=a[i].nxt;            A[a[i].nxt].pre=a[i].pre;              Break ;        }}

3, circular link list
(1), one-way circular chain list
The pointer to the last node points to the head node
(2), bidirectional loop linked list
The pointer to the last node points to the head node, and the predecessor of the head point points to the last junction.
Code implementation is no longer given, I believe you can achieve.

Linked list and array simulation chain 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.