This paper is aimed at the data structure Basic Series Network course (2): The application of the 14th class linear table in linear table.
Problem: There are table A,m1 rows, N1 columns, table b,m2 rows, N2 columns, and the natural connection result for a and B C
Cases:
Answer:
#include <stdio.h>#include <malloc.h>#define MAXCOL //maximum number of columns typedef intElemtype;typedef structNode1//Define Data node types{Elemtype Data[maxcol];structNode1 *next;//point to subsequent data nodes} dlist;typedef structNode2//Define head node type{intRow,col;//number of rows and columnsDlist *next;//point to First Data node} hlist;voidCreateTable (Hlist *&h) {intI,j; Dlist *r,*s; H= (Hlist *)malloc(sizeof(hlist));//Create head nodeh->next=null;printf("Number of rows in table, number of columns:");scanf("%d%d", &h->row,&h->col); for(i=0; iprintf("line%d:", i+1); s= (Dlist *)malloc(sizeof(dlist));//Create Data Nodes for(j=0; j//input a row of data preliminary statistics scanf("%d", &s->data[j]);if(H->next==null)//Insert First Data nodeh->next=s;Else //Insert additional data nodesr->next=s;after inserting the *s into the *r node.R=s;//r always points to the last data node} r->next=null;//Footer node next field empty}voidDisptable (Hlist *h) {intJ Dlist *p=h->next; while(P!=null) { for(j=0; jprintf("%4D", P->data[j]);printf("\ n"); p=p->next; }}voidLinkTable (hlist *h1,hlist *h2,hlist *&h) {intF1,f2,i; Dlist *p=h1->next,*q,*s,*r;printf(the connection fields are: 1th, the 2nd, and the order of the two columns:);scanf("%d%d", &F1,&F2); H= (Hlist *)malloc(sizeof(hlist)); h->row=0; h->col=h1->col+h2->col; h->next=null; while(P!=null) {q=h2->next; while(Q!=null) {if(p->data[f1-1]==q->data[f2-1])//corresponding field values are equal{s= (Dlist *)malloc(sizeof(dlist));//Create a data node for(i=0; i//Copy the current row of table 1s->data[i]=p->data[i]; for(i=0; i//Copy the current row of table 2 if(H->next==null)//Insert First Data nodeh->next=s;Else //Insert additional data nodesr->next=s; R=s;//r always points to the last data nodeh->row++;//number of table rows increased by 1} q=q->next;//Table 2 Move down one record} p=p->next;//Table 1 Move down one record} r->next=null;//Footer node next field empty}intMain () {hlist *h1,*h2,*h;printf("Table 1:\n"); CreateTable (H1);//CREATE TABLE 1 printf("Table 2:\n"); CreateTable (H2);//CREATE TABLE 2LinkTable (H1,H2,H);//Connect two tables printf("join result table: \ n"); Disptable (h);//Output connection result return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure routines--Application of linear tables: natural connection of tables