数据结构-线性表自然连接-应用实验
作者:互联网
#define MAXCOL 10
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node1{
int data[MAXCOL];
struct Node1 *next;
}Dlist;
typedef struct Node2{
int Row , Col;
Dlist *next;
}Hlist;
//创建节点链表
void createNode(Hlist *h){
int i = 0 , j = 0;
Dlist *r , *s;
h = (Hlist *)malloc(sizeof(Hlist));
printf("请输入行列数:\n");
scanf("%d%d",&h->Row , &h->Col);
printf("\n");
for(i;i<h->Row;i++){
s = (Dlist *)malloc(sizeof(Dlist));
for(j;j<h->Col;j++){
scanf("%d",&s->data[j]);
}
if(h->next == NULL)
h->next = s;
else
r->next = s;
r = s;
}
r->next = NULL;
}
void destoryTable(Hlist *&h){
Dlist *pre = h->next , *p = pre->next;
while(p!=NULL){
free(pre);
pre = p; p = p->next;
}
free(pre);
free(p);
}
void DispTable(Hlist *h){
int i = 0;
Dlist *p = h->next;
while(p!=NULL){
for(i;i<h->Col;i++){
printf("%3d",p->data[i]);
}
printf("\n");
p = p->next;
}
}
void LinkTable(Hlist *h1 , Hlist *h2 , Hlist *h3){
int i , j , k ;
Dlist *p = h1->next , *q ,*s , *r;
scanf("%d%d",&i,&j);
h3 = (Hlist *)malloc(sizeof(Hlist));
h3->Row = 0;
h3->Col = h1->Col + h2->Col;
h3->next = NULL;
while(p!=NULL){
q = h2->next;
while(q!=NULL){
if(p->data[i-1] == q->data[j-1]){
s = (Dlist *)malloc(sizeof(Dlist));
for(k=0;k<h1->Col;k++){
s->data[k] = h1->next->data[k];
}
for(k=0;k<h2->Col;k++){
s->data[h1->Col + k] = q->data[k];
}
if(h3->next == NULL){
h3->next = s;
}else{
r -> next = s;
}
r = s;
h3->Row++;
}
q = q->next;
}
p = p->next;
}
r -> next = NULL;
}
main(){
Hlist *h1 , *h2 , *h3;
printf("表一:\n");
createNode(h1);
printf("表二:\n");
createNode(h2);
printf("表三:\n");
createNode(h3);
LinkTable(h1,h2,h3);
printf("连接结果表:\n");
DispTable(h3);
return 0;
}
标签:NULL,线性表,h3,next,Hlist,Dlist,数据结构,data,连接 来源: https://blog.csdn.net/weixin_43409994/article/details/120823204