Some problems with the IOS single-link list algorithm

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. Such as:

All that is dealt with is a single linked list.

The basic structure of a single linked list:

1 struct Node {2     Char *3     struct node *4 } node_t;

Set a function for printing a linked list:

1 void list_display (node_t *head)2{3for      (; head; head = Head->Next)4         printf ("", head->data); 5     printf ("\ n"); 6 }

1. Calculate the length of a list (complexity O (n))

Algorithm: Defines a P pointer pointing to the head node, with a step of 1, traversing the linked list.

1 int List_len (node_t *head)2{3     int4     for0; head; head = Head->next, i++5     return  6 }

2. Reverse linked list (complexity O (n))

Algorithm: t traverses the linked list, Q records the last node of T, and P is a temporary variable used to cache the value of T.

1 void Reverse (node_t *head)2{3     000  4for      (t = head; t; p = t, t = t->next, p->next = q, q =5 }

3. Find the penultimate element (the tail node is recorded as the No. 0 lowest) (complexity O (n))

Algorithm: 2 pointer p, q initialized to point to the Head node. P ran to the K-junction, then Q began to run, and when P ran to the tail, Q arrived at the bottom of the last K.

1node_t *_kth (node_t *head,intk)2 {3     inti =0; 4node_t *p = head, *q =head;5      for(; P && i < k; p = p->next, i++); 6     if(I < k)return 0;7      for(; p->next; p = p->next, q = q->next); 8     returnQ;9}

4. Finding intermediate nodes (complexity O (n))

Algorithm: Set two to initialize the pointer to the head node P, q.p each forward two nodes, q each forward a node, so when p reached the tail of the chain, Q reached the middle.

 1  node_t *middle (node_t *head)  Span style= "color: #008080;" >2  { 3  node_t *p, *q;  4  for  (p = q = head; p->next; p = P->next, q = Q->next) { 5  p = p-> next;  6  if  (! ( P->next)) break   7   8  return   Q;  9 } 

5. Reverse Print List (complexity O (n))

Algorithm: Use recursion (that is, let the system use the stack).

1 void r_display (node_t *t)2{3     if  (t) {4         R_display (t->5         printf ("%s", t->  data); 6     }7 }

6. Determine if a linked list has a ring (complexity O (n))

Algorithm: Set two pointer p, Q, initialize the pointing head. P runs forward at step 2, and the step of Q is 1. This way, if the list does not exist, p and Q will never meet. If there is a ring, p and Q are bound to meet.

1 intAny_ring (node_t *head)2 {3node_t *p, *Q;4      for(p = q = head;p; p = p->next, q = q->next) {5p = p->Next;6         if(!p) Break; 7         if(p = = q)return 1;//Yes8     }9     return 0;//fail FindTen}

7. Find the entry node (complexity O (n)) of the ring in the linked list

Algorithm: Initialize three pointers p,q,r all point to head, then p moves in 2 step, Q travels in 1 step. When P and Q meet, the R pointer is emitted in 1 steps, and when P and R meet the junction is the entry node of the ring.

1node_t *find_entry (node_t *head)2 {3node_t *p, *q, *R;4 5      for(p = q = head; p; p = p->next, q = q->next) {6p = p->Next;7         if(!p) Break; 8         if(p = = q) Break; 9     }Ten  One     if(!p)return 0;//no ring in list A  -      for(r = head, q = q->next; Q! = r; r = r->next, q = q->next);  -  the     returnR; -}

Analytical:

Using both pointers p and Q, the step of the P scan is 1, and the step of the Q scan is 2. Their meeting point is the meet (on the ring) in the figure.

Suppose that the distance between the head pointer to the entry point entry is K. When Q is in the ring, p is already leading Q: D = k%n (n is the circumference of the ring).

We set the distance of meet relative entry (along the direction of travel) to X, then there is

(n-d) +x = 2x (P travels twice times the distance of Q)

Solution x = n-d

So when P and Q meet at meet, a 1-step pointer R is emitted from the head, and you know that R and Q will meet at entry!

8. Determine if two linked lists intersect (Complexity O (m+n))

Algorithm: Two pointers traverse the two linked lists, and if their tail nodes are the same, they must intersect.

1 intIs_intersect (node_t *a, node_t *b)2 {3     if(!a | |!b)return-1;//A or B is NULL4      for(; a->next; a = a->next); 5      for(; b->next; B = b->next); 6     returnA = = b?1:0;//return 1 for Yes, 0 for no7}

9. Find the intersection of two intersecting linked lists

Algorithm: p,q traverse the chain list A, B, assuming that Q first arrives at NULL, at this point a pointer from the head node of a T, when P arrives at null, from the head node of B issued S, when the s==t intersect.

1node_t *intersect_point (node_t *a, node_t *b)2 {3node_t *p, *q, *k, *t, *s;4      for(p = A, q = b; p && q; p = p->next, q = q->next); 5 6K = (P = =0)? q:p;//k Record the pointer not NULL7t = (p = =0)? b:a;//if p arrive at tail first, t = b; else P = a8s = (P = =0)?a:b;9      for(; k; k = k->next, t = t->next); Ten      for(; t! = s; t = t->next, s = s->next);  One     returnT; A}

10. Delete node D (do not node)

Algorithm: Copy the data of the next node E to the data area of the D node and delete E. (Defect: Cannot delete the tail node.)

1 node_t *delete (node_t *2{3     node_t *e = d->4     d->data = e->5     d->next = e->6 }

11. Two linked lists right-aligned printing.

Algorithm: the P and q two pointers traverse the list A and b respectively, if q first arrives at Null (that is, a is longer than B), at this point a head node issued a pointer T, print the complete list A. At the same time P continues to move to NULL, and the space is printed. It also prints the complete list B from the B-head node by issuing the pointer s.

1 voidFoo (node_t *a, node_t *b)2 {3node_t *p, *q, *k, *t, *s;4      for(p = A, q = b; p && q; p = p->next, q = q->next); 5 6k = p?p:q;7t = P?a:b;8s = P?b:a;9 Ten      for(; t; printf ("%d", t->data), t = t->next);  Oneprintf"\ n"); A      for(; k; printf ("  "), k = k->next);  -      for(; s; printf ("%d", s->data), S = s->next);  -}

Transferred from: HTTPS://GITHUB.COM/HIT9/OLDBLOG/BLOB/GH-PAGES/BLOG-SRC/BLOG/C/POSTS/25.MKD

Some problems with the IOS single-link list algorithm

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.