现在的位置: 首页 > 综合 > 正文

hncu1323线性表

2018年02月22日 ⁄ 综合 ⁄ 共 2299字 ⁄ 字号 评论关闭

题目描述

假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B。这就要求对线性表做如下操作:扩大线性表LA,将存在于线性表LB中而不存在于线性表LA中的数据元素插入到线性表LA中去。只要从线性表LB中依次取得每个元素,并依值在线性表LA中进行查访,若不存在,则插入之。上述操作过程可用下列算法描述之。
图2:线性表的动态分配顺序存储结构以及初始化
图3:线性表的插入算法
图4:线性表的删除算法
图5:线性表的查找算法

图:将两个列表合并的算法(C/C++描述)

上图算法中,在第8行取得集合B中的元素,然后再在第10行插入到集合A中。你的任务是先输出集合A和集合B中的元素,每个集合在一行中输出。然后每次在将集合B中的元素取出插入到集合A尾部后输出集合A中的元素。当然你的代码可以和上面的代码不一样,只要有相同的输出即可。

输入格式

有多组测试数据,每组测试数据占两行。第一行是集合A,第一个整数m0<m<=100)代表集合A起始有m个元素,后面有m个整数,代表A中的元素。第二行是集合B,第一个整数n(0<n<=100)代表集合B起始有n个元素,后面有n个整数,代表B中的元素。每行中整数之间用一个空格隔开。

输出

每组测试数据输出n+2行:前两行分别输出集合A、集合B中的数据,后面n行是每次从B中取出元素插入到A尾部后的集合A。每行整数之间用一个空格隔开,每组测试数据之间用一行空行隔开。

样例输入

5 1 5 2 6 3

3 1 7 9

1 3

2 2 7

4 2 5 1 4

4 1 2 4 5

样例输出

1 5 2 6 3

1 7 9

1 5 2 6 3

1 5 2 6 3 7

1 5 2 6 3 7 9


3

2 7

3 2

3 2 7


2 5 1 4

1 2 4 5

2 5 1 4

2 5 1 4

2 5 1 4

2 5 1 4

#include<iostream>
#include<malloc.h>
#include<stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 10 /* 线性表存储空间的初始分配量 */
#define LISTINCREMENT 2 /* 线性表存储空间的分配增量 */
typedef int ElemType;
typedef int Status;

typedef struct LIST
{
    ElemType *elem;
    int length;
    int listsize;
}Sqlist;

Status SetList (Sqlist *L)
{
    (*L).elem= (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!(*L).elem)
    return ERROR ;
    (*L).length=0;
    (*L).listsize=LIST_INIT_SIZE;
    return OK;
}

int ListLength (Sqlist L)
{
    return L.length;
}

void PrintList(Sqlist L)
{
    ElemType *q;
    int i;
    q=L.elem;
    //cout<<'e'<<L.length;
    for(i=0;i<L.length;i++,q++)
    if(i!=0)
    cout<<' '<<*q;
    else
    cout<<*q;
    cout<<endl;
}

int FindInList(Sqlist L,ElemType e)
{
    int i;
    for(i=0;i<L.length;i++)
    if(L.elem[i]==e)
    return OK;

    return ERROR;
}

Status InList(Sqlist *L,int i,ElemType e)
{
    if(i<1||i>(*L).length+1)
    return ERROR;
    if(FindInList(*L,e))
    return ERROR;

    if((*L).length>=(*L).listsize)
    {
        ElemType *newbase;
        newbase=(ElemType *)realloc((*L).elem,((*L).listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase)
        return ERROR ;
        (*L).listsize+=LISTINCREMENT;
        (*L).elem=newbase;
    }
    ElemType *q,*p;
    q=(*L).elem+i-1;
    p=(*L).elem+(*L).length-1;
    for(;p>=q;p--)
    *(p+1)=*p;

    *q=e;
    (*L).length++;
    return OK;
}

int main()
{
    int i,m,n,a[110],b[110],t=0;

    while(cin>>n)
    {
        if(t!=0)
        cout<<endl;
        t=1;
        for(i=0;i<n;i++)
        cin>>a[i];

        cin>>m;
        for(i=0;i<m;i++)
        cin>>b[i];

        for(i=0;i<n;i++)
        if(i==0)
        cout<<a[i];
        else
        cout<<' '<<a[i];
        cout<<endl;
            for(i=0;i<m;i++)
            if(i==0)
            cout<<b[i];
            else
            cout<<' '<<b[i];
            cout<<endl;

             struct LIST La;
            SetList(&La);
        for(i=0;i<n;i++)
        InList(&La,i+1,a[i]);
        for(i=0;i<m;i++)
        {
            InList(&La,La.length+1,b[i]);
            PrintList(La);
        }
    }
}

抱歉!评论已关闭.