数据结构 2-线性表

题目

用链表完成以下内容:
(1)根据提示输入5个整数,依次输出这5个整数,并将其存入线性表R1中,输出线性表R1的长度;
(2)根据提示删除线性表中的第3个元素,并将线性表中的元素依次输出,然后输出线性表R1的长度;
(3)移除线性表中最大数,将线性表中的元素依次输出,然后输出线性表R1的长度;
(4)重新根据提示输入4个整数,依次输出这4个整数,并将其按值递增方式存入线性表R2中(同时删除值相同元素),依次输出线性表R2的元素;
(5)分析第(3)和(4)部分的时间复杂度T(n),思考是否有效率更高的算法?分别写出至少一个其它算法(用C或者C++)用以解决(3)和(4)。

代码

#include<bits/stdc++.h>
using namespace std;
const int MaxSize = 1e5;
typedef int ElemType;
typedef struct SqList* PtrToList;
typedef struct SqList
{
    ElemType Data[MaxSize];
    int Length;
} SqList;
typedef PtrToList List;


void InitList(List& L);    //初始化线性表
void DestroyList(List& L);   //销毁线性表
void DispList(List L, string ListName);   //依次输出线性表中元素,并输出线性表长度
void ListInsert(List& L, int NumCount);   //根据提示输入NumCount个整数,依次输出这些整数,并将其存入线性表中,输出线性表的长度; 
void ListDelete(List& L, int Position);   //提示删除线性表中的第3个元素,并将线性表中的元素依次输出,然后输出线性表R1的长度; 
void MaxDelete(List& L);   //移除线性表中最大数,将线性表中的元素依次输出,然后输出线性表R1的长度;
void IncreasingInsert(List& L, int NumCount);   //重新根据提示输入4个整数,依次输出这4个整数,并将其按值递增方式存入线性表R2中(同时删除值相同元素),依次输出线性表R2的元素;


int main()
{
    List L;
    InitList(L);
    ListInsert(L, 5);
    DispList(L, "L");
    ListDelete(L, 3-1);
    DispList(L, "L");
    MaxDelete(L);
    DispList(L, "L");
    DestroyList(L);

    List L2;
    InitList(L2);
    IncreasingInsert(L2, 4);
    DispList(L2, "L2");
    DestroyList(L2);
}


//------------初始化线性表------------
void InitList(List& L)
{
    L = new SqList;
    L->Length = -1;
}
//------------销毁线性表------------
void DestroyList(List& L)
{
    delete L;
}
//------------输出线性表------------
//依次输出线性表中元素,并输出线性表长度
void DispList(List L, string ListName)
{
    printf("L:  ");
    for (int i = 0; i <= L->Length; i++)
        printf("%d ", L->Data[i]);
    printf("\nLength of %s:  %d\n", ListName.c_str(), L->Length + 1);
    printf("------------\n");
}
//------------为线性表插入元素------------
//根据提示输入NumCount个整数,依次输出这些整数,并将其存入线性表中,输出线性表的长度; 
void ListInsert(List& L, int NumCount)
{
    printf("Please input %d numbers: ", NumCount);
    int i;
    for (i = 0; i < NumCount; i++)
    {
        scanf("%d", &L->Data[++L->Length]);
    }
}
//------------删除线性表中的元素------------
//提示删除线性表中的第3个元素,并将线性表中的元素依次输出,然后输出线性表R1的长度; 
void ListDelete(List& L, int Position)
{
    printf("Delete the num.%d elem of L : \n", Position + 1);
    for (int i = Position; i < L->Length; i++)
    {
        L->Data[i] = L->Data[i + 1];
    }
    L->Length--;
}
//------------移除线性表最大数------------
//移除线性表中最大数,将线性表中的元素依次输出,然后输出线性表R1的长度;
void MaxDelete(List& L)
{
    printf("Delete the max elem of L : \n");
    int MaxNum = L->Data[0];
    for (int i = 1; i <= L->Length; i++)
    {
        MaxNum = max(MaxNum, L->Data[i]);
    }
    int index = -1;
    for (int i = 0; i <= L->Length; i++)
    {
        if (L->Data[i] != MaxNum)
        {
            L->Data[++index] = L->Data[i];
        }
    }
    L->Length = index;
}
//------------递增插入线性表------------
//重新根据提示输入4个整数,依次输出这4个整数,并将其按值递增方式存入线性表R2中(同时删除值相同元素),依次输出线性表R2的元素;
void IncreasingInsert(List& L, int NumCount)
{
    printf("Please input %d numbers: ", NumCount);
    set<int>num;
    for (int i = 0; i < NumCount; i++)
    {
        int t;
        scanf("%d", &t);
        num.insert(t);
    }
    for (auto x : num)
    {
        L->Data[++L->Length] = x;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值