C + +, data structure implementation of two linked lists (tail interpolation method to establish a single-linked list, the chain table length, direct insertion sort)

Source: Internet
Author: User


1 Topics

Implement merging of two linked lists

2 basic function requirements:

1. set up two lists A and B, the number of linked list elements are M and n each .

2. assume that the elements are (x1,x2,... xm), and (Y1,y2, ... yn). Combine them into a linear table C, which makes:

    &NBSP, m>=n when c=x1,y1,x2, Y2,... xn,yn,..., xm                                      

when n>m, c=y1,x1,y2,x2,... ym,xm,..., yn

3. Output Linear table C:

The c is sorted in ascending order by the direct insertion sort method, and the list D is generated and the list D is output.

3 test data:

(1) Table A (30,41,15,12,56,80)                                  

           b table (23,56,78,23,12,33,79,90,55)                            

( 2 ) Table A (30,41,15,12,56,80,23,12,34)                        

Table B (23,56,78,23,12)

From the problem analysis, the task can first be divided into modules:

1. Linked list structure

2. output function

3. number of list elements

4. Merging

5. Direct Insertion sorting method

Among them, the merging function realizes the idea concretely:

Take the test data (1) For example: A table data element m=6,b table data element n=9, at this time m < n, analyze the merge result, should first insert a data element of table B, insert the data element of table A, and then insert a table of ..., so you can get the list, odd bit is a table data element, The even digits are the B-table data elements.

c=23,30,56,41,78,15,23,12,12,56,33,80,79,90,55

Sort Result: 12,12,15,23,23,30,33,41,55,56,56,78,79,80,90

Similarly, the test data (2)

C=30,23,41,56,15,78,12,23,56,12,80,23,12,34

Sort Result: 12,12,12,15,23,23,23,30,34,40,56,56,78,80

Module partitioning

1 Linked header files

It mainly includes the storage structure of the linked list and the declaration of the main functions, including the pre-processing instructions, and putting up the compiler header files.

2 Linked list function implementation file

is mainly the implementation of the various functions of the list, as well as the implementation of the sub-functions required by these functions.

3 Testing The main function file

Mainly include data manipulation, data import, test function.



Note: The value of this list is passed ( The next list function takes C + + The reference method in

node.h****** function definition ********* #pragma  once  #ifndef  __node_h__#define __node_h__  //--------Add header files-----------#include <stdio.h> #include <stdlib.h> #include <assert.h> / /--------Linked list structure------------ typedef int datetype;  //defined data type is  int typedef  struct node//node type definition {datetype data;struct node *next;} node, *linklist; //--------function-------------void initlist (linklist *l);////initialization void  Createfromtail (linklist l);//tail interpolation method to establish a single-linked list Void printlink (linklist l);//Print List int listlength ( LINKLIST&NBSP;L);//Find the length of the list linklist merge (linklist l, linklist m);//merge two linked lists void  Insertsort (Node *head);//Direct Insertion sort #endif   //__node_h_//*********node.c**** function implementation ********   #include "Node.h"   void initlist (linklist *l)//Initialize {*l =  (linklist) malloc ( sizeof (Node));(*l)->next = null;}  void createfroMTail (LINKLIST&NBSP;L)//tail interpolation to create a single-linked list {node *r, *s;datetype c = 1;r = l;//scanf_s ("%d",  &c);//r->data = c;while  (c) {s =  (node*) malloc (sizeof (Node)); scanf_ S ("%d",  &c);if  (c == 0) {R->next = null;return;} S->data = c;r->next = s;r = s;}  } void printlink (linklist l)//Print List {linklist p = l->next;while  (P  != null) {printf ("%d ",  p->data); P = p->next;} printf ("\ n");}  int listlength (LINKLIST&NBSP;L)//chain table length {Node *p;p = l;int j = 0;while   (p != null) {p = p->next;j++;} Return j;}  void ins (linklist *l, datetype x)//tail plug element {node *s;s =  (Node *) malloc (Node);s->data = x; (*l)->next = s; } linklist merge ( Linklist l, linklist&NBSP;M)//merge two linked lists {node *pl, *pm, *pls; Linklist ls;initlist (&ls);p L = l->next;pm = m->next;pls = ls;int  count_l = listlength (PL),  count_m = listlength (PM);int sum =  count_l + count_m;if  (count_l >= count_m) {while  (Pl != NULL)   ||   (Pm != null)) {if  (pl != null) {Ins (&pls, pl->data);p l =  Pl->next;pls = pls->next;} if  (pm != null) {Ins (&pls, pm->data);p m = pm->next;pls = pls- >next;}}}  if  (Count_l <= count_m) {while  ((pl != null)  | |   (Pm != null)) {if  (pm != null) {Ins (&pls, pm->data);p m =  Pm->next;pls = pls->next;} if  (pl != null) {Ins (&pls, pl->data);p L&NBSP;=&NBSP;PL-&GT;NEXT;PLS&NBSP;=&NBSP;PLS->next;}}} Pls->next = null;return ls;}  void insertsort (node *head)   //Direct Insert Sort {node *p, *pre, *q, *r;p  = head->next;head->next = NULL;while  (p) {pre = p->next;r =  head;q = head->next;while  (q&&q->data<p->data) {r = q;q  = q->next;} P->next = r->next;r->next = p;p = pre;}}  //*************test.c***** function Implementation ***********  #include "Node.h"   void test1 () {LinkList  l, m, sum;int sum = 0;initlist (&l); initlist (&M); InitList (&SUM); printf ("Please enter the value of each element of the list L (enter 0 to end) \ n"); Createfromtail (L);p rintf ("\ n");p rintf ("Please enter the value of each element of the list m (enter 0 to end) \ n"); Createfromtail (M);p rintf ("\ n");  printf ("Linked list  l ="); Printlink (L);p rintf ("l  chain table Length  %d\n",  listlength (L));p rintf ("\ n");p rintf ("linked list  m ="); Printlink (M);p rintf ("m  chain table length = %d\ n ", Listlength (M));p rintf (" \ n "),  sum = merge (l, m);p rintf (" linked list  SUM =  "); Printlink (sum); Sum = listlength (L)  + listlength (M); Insertsort (sum); Printlink (SUM);}  int main () {Test1 (); System ("pause"); return 0;}

650) this.width=650; "title=" Picture 1.png "alt=" Wkiol1ahxchgg260aabgru-qvvc950.png "src=" http://s3.51cto.com/wyfs02/M00 /79/0f/wkiol1ahxchgg260aabgru-qvvc950.png "/>

C + +, data structure implementation of two linked lists (tail interpolation method to establish a single-linked list, the chain table length, direct insertion sort)

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.