zoukankan      html  css  js  c++  java
  • 线性表的应用

    通过计算任意两个表的简单自然连接过程讨论线性表的应用。书本2.4

    #include<iostream>
    using namespace std;
    #define MaxCol 10
    
    typedef struct Node1
    {
    	int data[MaxCol];
    	struct Node1 *next;
    }DList;
    typedef struct Node2
    {
    	int Row,Col;
    	DList *next;
    }HList;
    
    void CreateTable(HList *&h)
    {
    	int i,j;
    	DList *r,*s;
    	h=(HList *)malloc (sizeof(HList));
    	h->next=NULL;
    	printf("表的行数,列数:");
    	scanf("%d%d",&h->Row,&h->Col);
    	for(i=0;i<h->Row;i++)
    	{
    		printf("第%d行",i+1);
    		s=(DList *)malloc (sizeof(DList));
    		for(j=0;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 DispTable (HList *h)
    {
    	int j;
    	DList *p=h->next;
    	while(p!=NULL)
    	{
    		for(j=0;j<h->Col;j++)
    			printf("%4d",p->data[j]);
    		printf("
    ");
    		p=p->next;
    	}
    }
    
    void LinkTable(HList *h1,HList *h2,HList *&h)
    {
    	int i,j,k;
    	DList *p=h1->next,*q,*s,*r;
    	printf("连接字段是:第一个表位序,第二个表位序:");
    	scanf("%d%d",&i,&j);
    	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[i-1]==q->data[j-1])
    			{
    				s=(DList *)malloc (sizeof(DList));
    				for(k=0;k<h1->Col;k++)
    					s->data[k]=p->data[k];
    				for(k=0;k<h2->Col;k++)
    					s->data [h1->Col+k]=q->data[k];
    				if(h->next==NULL)
    					h->next=s;
    				else
    					r->next=s;
    				r=s;
    				h->Row++;
    			}
    			q=q->next;
    		}
    		p=p->next;
    	}
    	r->next=NULL;
    }
    
    void main()
    {
    	HList *h1,*h2,*h;
    	printf("表1:
    ");
    	CreateTable(h1);
    	printf("表2:
    ");
    	CreateTable(h2);
    	LinkTable(h1,h2,h);
    	printf("连接结果表:
    ");
    	DispTable(h);
    }
    


  • 相关阅读:
    Day4 0708
    Day2 0706
    两道递推公式题的解题报告
    博客还需优化
    飞行路线Luogu4568
    堆优化Dijkstra(Luogu 4779)
    2019四等奖的清明节征文
    2019四等奖的叶圣陶初稿
    Luogu P1072 Hankson的趣味题
    Loj10022 埃及分数(迭代加深搜索IDDFS)
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3225994.html
Copyright © 2011-2022 走看看