Analysis of whether a linked list is the lead node in different data structures

Source: Internet
Author: User

I. Linked list

When learning the list of data structures, there are linked lists that differentiate between the leading nodes and the nodes that do not take the lead .

At that time, after writing the basic operation algorithm of the link list of the lead node, I wrote the basic operation of the linked list without the lead node.

The difference between finding a lead node is mainly in the 2 algorithms: Insert and delete

Insertion and deletion of a linked list without a lead node because it involves inserting a delete on the first node, it changes the value of the head pointer, requires a separate analysis of the first node, and therefore joins the reference or pointer on the link header pointer parameter.

So a linked list that does not take the lead on a node is more troublesome.

Differences in the insertion algorithm:

1 //linked list of leading nodes2 intInsert (LINK H,intPos,stu x)/*Insert*/3 {4LINK P,s;intK =0;5p =H;6    while(P! = NULL && k < pos-1)7   {8p = p->Next;9k++;Ten   } One   if(P==null | | k > pos-1) A   { -printf"The position is Illegal...insert fained\n"); -       return 0; the   } -s =NewNODE; -S->data =x; -S->next = p->Next; +P->next =s; -printf"Insert finished...\n"); +   return 1; A } at //link list without a lead node - intInsert (LINK &h,intPos,stu x)//Insert - { -LINK p =h,s; -     intk=1; -     if(pos = =1)//the case of inserting POS is 1 in     { -s =NewNODE; toS->data =x; +S->next =H; -H =s; the         return 1; *     } $      while(P! = NULL && k < pos-1)Panax Notoginseng     { -p = p->Next; thek++; +     } A     if(p = = NULL | | k > pos-1) the     { +printf"The position is Illegal,insert failed....\n"); -         return 0; $     } $s =NewNODE; -S->data =x; -S->next = p->Next; theP->next =s; -     return 1;Wuyi}

From the above code you can clearly see the difference between the insertion algorithm of the linked list without the lead node:

1. The top node of the formal parameter uses a reference, and the insertion position is the first position that requires the value of the head pointer to be modified.

2. Insertion requires a separate analysis of the first node, which is more of the code than the algorithm that takes the lead node:

if (pos = = 1)//Insert POS is 1 case     {         s = new node;31         s->data = x;32         s->next = h;33         H = s;34         r Eturn 1;35     }

Remove the differences on the algorithm:

1 //the deletion algorithm of the link list of the leading node2 intDeletenode (LINK H,intPos/*Delete*/3 {4LINK p,q;intK =0;5p =H;6     if(H = = NULL | | H->next = =NULL)7     {8printf"The linked list is empty, please create the list first ... \ n");9       return 0;Ten     } One      while(P->next! = NULL && k < pos-1) A     { -p = p->Next; -k++; the     } -     if(P->next = = NULL | | k > pos-1)  -     { -printf"invalid location, delete failed ... \ n"); +         return 0; -     } +Q = p->Next; AP->next = q->Next; at      Free(q); -printf"Delete Complete .... \ n"); -     return 1; - } - //deletion algorithm of not leading node - intDeletenode (LINK &h,intPos//Delete in { -LINK p = h,q;intK =1; to     if(H = = NULL)//empty list does not perform a delete operation +     { -printf"This is a empty link List,can not execute the delete function\n"); the         return 0; *     } $     if(pos = =1)Panax Notoginseng     { -H = h->Next; the delete p; +         return 1; A     } the      while(P->next!=null && K < pos-1) +     { -p = p->Next; $k++; $     } -     if(P->next = = NULL | | k > pos-1) -     { theprintf"The position is Illegal,delete failed...\n"); -         return 0;Wuyi     } theQ = p->Next; -p = q->Next; Wu Delete q; -     return 1; About } $ //the difference between the two algorithms and the insertion algorithm is similar, because the first node is a special consideration .

The difference reason is similar to the insertion algorithm, which is not mentioned here.

Two: Stacks and queues

Chain stack: In fact, a special form of the list, the insertion and deletion of nodes are constrained and explicit, advanced, both insert and delete can only be completed at the top of the stack, the bottom of the stack is not allowed to operate.

Chain queue: is also a special form of the list, and the link stack is different, the queue is required to be queued only at the end of the team, the team only in the team head, but also to insert and delete constraints.

Remember Kanbayashi teacher had data structure this class, the teacher just said the link stack does not take the lead node, chain queue to lead node, at that time some confusion, not quite understand, just step by step code codes when required to do.

Baidu later why, found that there is no relevant things on Baidu, just say that the chain is not the lead node, the chain queue to take the lead node.

Here are some of their own thinking, the correctness of the research, but estimated sorta it.

The first link stack, because the link stack only involves the operation of the top of the stack, both insert and delete are at the top of the stack, so it doesn't matter if you take the lead node, the algorithm only needs to consider the situation of the first node.

But because of the space considerations, so the link stack does not take the lead node, and think that the link table head is the top of the stack, into the stack is the head of the list, the stack is to delete the head node.

The picture is in the stack:

Well, it's just for convenience, so the inserts inside the chain are all head plugs .

Then chain queues, chain queues typically have two storage structures:

1. Double-headed non-cyclic chain queue

2. Loop queue with tail pointer

Regardless of the chain queue is the kind of storage structure, each insertion and deletion of a node requires the correct pointer to modify, and all need to bring the top node.

The chain queue with the top node is for ease of operation, otherwise when the first node is not taken, you can imagine that the operation becomes quite complex and the head pointer has to be assigned a value of NULL.

For example: The first two-headed non-circular queue, when out of the queue, when there is only one node in the queue, Q.front,q.rear will be different from the previous outbound queue operation.

So is the cyclic queue of the same tail pointer.

In general, it is easy to program by taking the lead node to avoid the special situations that result from the operation of the first node.

Well, so in different real demand situations, the data structure needs to be flexible, sometimes it is a combination of several data structures, the specific situation to be specific analysis, to experience whether the lead node differences.

In order to construct a good structure and write a good program.

Analysis of whether a linked list is the lead node in different data structures

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.