功 能: 设计算法,将两个单链表数组的特定位序, 相同者,链接起来
编程人: 王涛
详细博客:https://blog.csdn.net/qq_57484399/article/details/127161982
时 间: 2024.4.14
版 本: V1.0
V1.0
main.cpp
/*****************************************
功 能: 设计算法,将两个单链表数组的特定位序, 相同者,链接起来
编程人: 王涛
详细博客:https://blog.csdn.net/qq_57484399/article/details/127161982
时 间: 2024.4.14
版 本: V1.0
******************************************/
#include <stdio.h>
#include <malloc.h>#define MaxCol 10typedef int ElemType;typedef struct dataNode
{ElemType date[MaxCol]; //存储数据的节点数组struct dataNode *next; //定义节点的前驱指针
}DataList;typedef struct headNode
{int row; //头结点数据存储链表的 行数和列数int columns;DataList *next; //头结点的后继指针类型是 DataList}HeadList;void CreatTable(HeadList *&new_table)
{int i,j;DataList *tailNode; //尾指针节点DataList *dataNode; //数据新节点new_table = (HeadList*)malloc(sizeof(HeadList));new_table->next = NULL;//基本头结点创建成功//下面分配行列数printf("请输入表的行数和列数:");scanf("%d%d",&new_table->row,&new_table->columns);//头结点基本创建完成//下面遍历输入数据节点for(i = 0;i < new_table->row; i++){printf("请输入第%d行的数据:",i+1);//为每行数组分配空间dataNode = (DataList*)malloc(sizeof(DataList));for(j = 0;j < new_table->columns; j++){scanf("%d",&dataNode->date[j]);}//数据填充完毕, 开始 尾插法插入头结点之后(head和data节点类型不一致,区分插入)if(new_table->next == NULL){new_table->next = dataNode;}else{tailNode->next = dataNode;}tailNode = dataNode; //尾结点指向新节点}//尾指针置空tailNode->next = NULL;
}
/*****************************************//定义指针指向数据节点//while(数据节点不为空){//()for循环遍历数组//遍历完指针后移//}//记得换行回车
******************************************/
void DisplayTable(HeadList *showTable)
{int i;//定义指针指向数据节点dataNode *nowNode = showTable->next;//while(数据节点不为空){printf("\n");while(nowNode != NULL){//()for循环遍历数组for(i = 0; i < showTable->columns; i++){printf("%2d",nowNode->date[i]);printf(" ");}//记得换行回车printf("\n");printf("\n");//遍历完指针后移nowNode = nowNode->next;}}void LinkTable(HeadList *first_table,HeadList *second_table,HeadList *&result_table)
{DataList *firstNode;DataList *secondNode;DataList *newNode;DataList *tailNode;int connect_table1,connect_table2;int serial_number; //数组序号//result_table 头节点建立好 (列 = first_table->columns + second_table->columns),行不确定result_table = (HeadList*)malloc(sizeof(HeadList));result_table->columns = first_table->columns + second_table->columns;result_table->row = 0; //目前是0//然后头结点next置空result_table->next = NULL;//while 遍历 first_table每个节点 ,firstNode = first_table->next;secondNode = second_table->next;printf("表1连接的数组位序:");scanf("%d",&connect_table1);getchar();getchar();printf("表2连接的数组位序:");scanf("%d",&connect_table2);while(firstNode != NULL){secondNode = second_table->next;//每次都重新遍历//拿first_table一个节点 遍历 second_table的所有节点while(secondNode != NULL){//判断特定行,特定列, 相等,则链接相关数组for循环if(secondNode->date[connect_table2-1] == firstNode->date[connect_table1-1]){newNode = (DataList*)malloc(sizeof(DataList));//连接两数组(first先, second后)for(serial_number = 0; serial_number < first_table->columns;serial_number++){newNode->date[serial_number] = firstNode->date[serial_number];}for(serial_number = 0;serial_number < second_table->columns;serial_number++){newNode->date[(first_table->columns)+serial_number] = secondNode->date[serial_number];}if(result_table->next == NULL){result_table->next = newNode;}else{tailNode->next = newNode;}tailNode = newNode;result_table->row++;//尾插法插入result_table后}secondNode = secondNode->next;}firstNode = firstNode->next;}//result_table尾结点 置空tailNode->next = NULL;
}int main()
{HeadList *first_table; //代表要处理第一个表HeadList *second_table; //代表第要处理二个表HeadList *result_table; //两个表处理后,存储结果的表Cprintf("表1:\n");//用头结点建表CreatTable(first_table);printf("表2:\n");//头结点建表CreatTable(second_table);//处理两个表LinkTable(first_table,second_table,result_table);DisplayTable(result_table);return 0;
}