[链表练习2-1]线性表ADT的实现之一:线性表ADT基于顺序存储的实现

实习题目:线性表ADT实现之一:线性表ADT基于顺序存储的实现

实习目的:帮助学生熟练掌握顺序表的建立及基本操作
问题描述:设计一个顺序表并实现对其进行基本操作。
基本要求:建立一个顺序表:
(1)输入数据;
(2)实现数据的插入、删除、搜索、输出等基本操作;
(3)实现集合的并、交和两个有序顺序表的合并。

 

 测试输入期待的输出
测试用例5
1 3 5 7 9
2 10
10
9
22
6
1 2 3 4 5 6
A is created as: 1 3 5 7 9
After inserted A is 1 10 3 5 7 9
After deleted A is 1 3 5 7 9
9 is located at index of 5
22 is not found
B is created as: 1 2 3 4 5 6
A cross B is 1 3 5
A union B is 1 3 5 7 9 2 4 6

A union B in sequence is 1 2 3 4 5 6 7 9

测试数据:

5  //线性表A的长度
1 3 5 7 9  //线性表A的数据
2 10 //表示在第2个位置插入10
10 //表示删除值=10的数据元素
9 //查找元素9
22/ 查找元素22
6 //线性表B的长度

1 2 3 4 5 6 

代码:

 

#include <stdio.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct
{
    int *elem,*m;
    int length;
    int listsize;
} SqList;

int InitList_Sq(SqList &L)
{
    L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
    if(!L.elem)
        return(-1);
    L.length=0;
    L.listsize=LIST_INIT_SIZE;
    return 1;
}

int Insert_SqList(SqList &L,int i,int x)
{
    int *p,*q;
    if(i<1||i>L.length+1)
        return(-1);
    if(L.length+1>L.listsize)
    {
        L.m=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
        if(!L.m)
            return(-1);
        L.elem=L.m;
        L.listsize +=LISTINCREMENT;
    }
    q=&(L.elem[i-1]);
    for(p=&(L.elem[L.length-1]); p>=q; --p)
        *(p+1)=*p;
    *q=x;
    ++L.length;
    return 1;
}


//Delete_SqList
int Delete_SqList(SqList &L,int x) //把x从表中删去
{
    int index=-1;
    for(int i=0; i<L.length; i++)
    {
        if(L.elem[i]==x)
        {
            index=i;
            break;
        }
    }
    if(index<0)
        return 0;
    for(; index<L.length-1; index++)
    {
        L.elem[index]=L.elem[index+1];
    }
    L.length--;
    return 1;
}



int main()
{
    int i,x,temp;
    int delete_m;
    int search_m,flag_search=0;
    SqList L;

    SqList Sum;//※
    InitList_Sq(Sum);//※


    InitList_Sq(L);

    scanf("%d",&x);

    for(i=0; i<x; i++)
    {

        scanf("%d",&temp);
        Insert_SqList(L,i+1,temp);
        Insert_SqList(Sum,i+1,temp);

    }

    for(i=0; i<L.length; i++)  //创建后输出
    {
        if(i==0)
        {
            printf("A is created as:");
        }
        printf(" %d",L.elem[i]);
        if(i==L.length-1)
        {
            printf("\n");
        }
    }

    //以下为插入操作

    scanf("%d",&i);

    scanf("%d",&x);

    Insert_SqList(L,i,x);

    for(i=0; i<L.length; i++)  //插入后输出
    {
        if(i==0)
        {
            printf("After inserted A is");
        }
        printf(" %d",L.elem[i]);
        if(i==L.length-1)
        {
            printf("\n");
        }
    }

    //以下为删除操作
    scanf("%d",&delete_m);
    Delete_SqList(L,delete_m);
    for(i=0; i<L.length; i++)
    {
        if(i==0)
        {
            printf("After deleted A is");
        }
        printf(" %d",L.elem[i]);
        if(i==L.length-1)
        {
            printf("\n");
        }
    }

    //以下为查找操作
    scanf("%d",&search_m);
    //情况1:找到了 9 is located at index of 5
    //情况2:没找到 22 is not found
    for(int i=0; i<L.length; i++)
    {
        if(search_m==L.elem[i])
        {
            printf("%d is located at index of %d\n",search_m,i+1);
            flag_search=1;
        }
        if((i==L.length-1)&&(flag_search==0))
        {
            printf("%d is not found\n",search_m);
        }
    }

    scanf("%d",&search_m);
    flag_search=0;
    for(int i=0; i<L.length; i++)
    {
        if(search_m==L.elem[i])
        {
            printf("%d is located at index of %d\n",search_m,i+1);
            flag_search=1;
        }
        if((i==L.length-1)&&(flag_search==0))
        {
            printf("%d is not found\n",search_m);
        }
    }

    //以下是关于B的操作
    SqList B;
    InitList_Sq(B);
    scanf("%d",&x);
    int sum_flag=0;//*
    for(i=0; i<x; i++)
    {
        //printf("Please input nums(%d):\n",i+1);
        scanf("%d",&temp);
        Insert_SqList(B,i+1,temp);
        for(int j=0;j<L.length;j++) //*
        {
            if(temp!=Sum.elem[j])
                sum_flag++;
        }
        if(sum_flag==L.length)
            Insert_SqList(Sum,L.length+1,temp);//*
        sum_flag=0;
    }


    for(i=0; i<B.length; i++)  //创建后输出
    {
        if(i==0)
        {
            printf("B is created as:");
        }
        printf(" %d",B.elem[i]);
        if(i==B.length-1)
        {
            printf("\n");
        }
    }

    /*B is created as: 1 2 3 4 5 6
      A cross B is 1 3 5
      A union B is 1 3 5 7 9 2 4 6
      A union B in sequence is 1 2 3 4 5 6 7 9 */
    //A corss B:
    printf("A cross B is");
    for(int i=0; i<L.length; i++)
    {
        for(int j=0; j<B.length; j++)
        {
            if(L.elem[i]==B.elem[j])
            {
                printf(" %d",L.elem[i]);
            }
        }

    }
    printf("\n");

    //A union B:
    printf("A union B is");
    int flag=0;
    for(int i=0; i<L.length; i++)
    {
        printf(" %d",L.elem[i]);
    }
    for(int i=0; i<B.length; i++)
    {
        for(int j=0; j<L.length; j++)
        {
            if(L.elem[j]==B.elem[i])
            {
                //printf(" %d",B.elem[i]);
                break;
            }
            if(L.elem[j]!=B.elem[i])
            {
                flag++;
            }

        }
        if(flag==L.length)
            printf(" %d",B.elem[i]);
        flag=0;
    }
    printf("\n");

    //A union B in sequence:

    //A union B in sequence is 1 2 3 4 5 6 7 9 */
    temp=0;
    for(int i=0;i<Sum.length;i++)
    {
        for(int j=0;j<Sum.length-1-i;j++)
        {
            if(Sum.elem[j]>Sum.elem[j+1])
            {
                temp=Sum.elem[j+1];
                Sum.elem[j+1]=Sum.elem[j];
                Sum.elem[j]=temp;
            }
        }
    }
    printf("A union B in sequence is");
    for(int i=0;i<Sum.length;i++)
    {
        printf(" %d",Sum.elem[i]);
    }
    printf("\n");

    //printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值