Looking back, data structure--a common algorithm on linear table and linked list

Source: Internet
Author: User


Recently in reviewing data structure, by the way look at the time of the freshman write code, after reading more than at the beginning of a deeper experience.


I hope these can provide some reference for beginners.


1. Write algorithm to implement linear table in-place inverse operation void Inverselist (Seqlist l) {for (i = 0; I <= (l.length-1)/2; i++) {l.elem[i] <-> L.elem[l.len Gth-1-i];}} 2. Delete the k elements from the sequence table starting with the first element void DeleteList (seqlist l, int i, int k) {if (I < 0 | | i > L.LENGTH-1 | | k < 0) {printf ("E Rror! "); return;} if (i+k <= l.length) {for (j = i+k; J < L.length; J + +) l.elem[j-k] = L.elem[j];l.length = k;} Elsel.length = i;} 3 If a single linked list with a lead node has been established, H is the pointer to the head node//and the data stored in the linked list is arranged in small to large order. Write the function implementation algorithm, insert the value of X into the list, insert the post-linked list to remain ordered void Insertlist (linklist h, ElementType x) {if (NULL = = h->next) {p = (linklist) malloc ( sizeof (Linknode));p->data = X;p->next = Null;h->next = P;} Pre = H->next;p = H;while (pre->next! = NULL && Pre->data < x) {p = Pre;pre = Pre->next;}  if (NULL = = Pre->next && Pre->data < x) {q = (linklist) malloc (sizeof (Linknode)); q->data = X;q->next = Null;pre->next = q;} else//null! = Pre->next && pre->data > x | | NULL = = Pre->next && pre->data >= x{q = (linklist) malloc (sizeof (Linknode)); q->data = X;q->next = Pre;p->next = q;}} 4. Assume that in a single loop with a length greater than 1, there is no head node, no head pointer,//p is a pointer to a node in the linked list,//write algorithm, implement delete the node's precursor node void deletelinklist (Linklist p) {pre = P-> Next;while (Pre->next! = p) Pre = PRE-&GT;NEXT;PP = Pre->next;pre->next = Pre->next->next;free (PP);} 5. Set h as a pointer to a single-linked header node, the list of values in the order//design algorithm, to delete the same value in the list of nodes, so that only one void Deleteselectlinklist (linklist h) {p = h->next;if (!p) Return;x = P->data;while (P->next) if (x! = p->next->data) {x = P->next->data;p = P->next;} Else{q = P->next;p->next = P->next->next;free (q);}} /*6. Delete the predecessor node of the node with the first node H (pointing to the head node) with the value X in the lead junction */void Deletelinklist (linklist H, ElementType x) {if (!) ( H->next)) Return;if (! ( H->next->next)) Return;pre = H->next->next;p = H;while (Pre->next && Pre->data! = x) {pre = pre-& Gt;next;p = P->next;} if (x = = pre->data) {q = P->next;p->next = Pre;free (q);}} Note that the/*7.la,lb is the head pointer of a single-linked list representing the lead node of the set, a, and a-B (the difference of two sets) returned by the LA list */linklist subtractioN (linklist la, linklist lb) {linklist pre = La->next;while (pre) {while (Lb->next) {if (Pre->data = = LB-&GT;NEXT-&G T;data) {p = Pre;pre = Pre->next;free (p);} LB = lb->next;} Pre = Pre->next;} Return LA;} /*8_1. Set a,b,c corresponds to the order Increment table for LA,LB,LC,C=A∩B represented by LC */seqlist intersection (seqlist la, seqlist lb) {lc.length = 0;for (i = 0; i < La.length; i++) {for (j = 0; J < lb.length && La.elem[i]! = Lb.elem[j]; j + +); if (J < lb.length) {lc.elem[length++] = LA.E Lem[i];}} return LC;} /*8_2.LA,LB,LC is the head pointer of an ascending ordered single-linked list representing the lead node of the set A,b,c, C=a∩b returned by the LC linked list *///to a modified LC-linked list return linklist intersection (linklist la, linklist lb) {pre = La->next;while (pre) {while (Lb->next && pre->data! = lb->data) lb = lb->next;if ( Lb->next) {la->next = pre;} Pre = Pre->next;} Return LA;} /*9 Delete the out-of-order table of the lead node, the node with the element value greater than min and less than Max */void Delete (linklist h, ElementType min, ElementType max) {h = h->next;while (h) {if (H->data > Min && h->data < max) {q = h;h = H->next;free (q);} H = H-> next;}} /*10.h is the head pointer of a single-linked list of leading nodes, which contains two types of character data elements: letters and numbers, split h as two leading nodes of a single circular link list *ph1, *PH2, where the *PH1 chain holds the letters, the *PH2 chain holds the numbers */void separation ( Linklist h, linklist H1, linklist h2) {if (! ( H->next)) Return;p = H;h = H->next;free (p), H1 = (linklist) malloc (sizeof (Linknode)), H2 = (linklist) malloc (sizeof ( Linknode));}


Looking back, data structure--a common algorithm on linear table and 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.