【PTA】线性表练习

目录

一、R7-2 顺序表(删除)

1、偷懒方法

2、C++代码

3、Python代码

二、R7-3 单链表的创建及遍历

1、偷懒方法

2、C++代码

3、Python代码

三、R7-4 在有序链表中插入数据

1、C++代码

2、Python代码


一、R7-2 顺序表(删除)

已知一组数据,采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素

输入格式:

输入包含三行数据,第一行是表中元素个数,第二行是顺序表的各个元素,第三行是区间x和y。

输出格式:

删除元素值在[x,y]之间的所有元素后,输出新的顺序表。(最后无空格)

输入样例:

在这里给出一组输入。例如:

10
55 11 9 15 67 12 18 33 6 22
10 20

输出样例:

在这里给出相应的输出。例如:

55 9 67 33 6 22

1、偷懒方法

#include <stdio.h>
int main()
{
    int x, y, n, a[99], i;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d %d", &x, &y);
    for (i = 0; i < n; i++)
    {
        if (a[i] >= x && a[i] <= y)
            continue;
        else
        {
            if (i == 0)
                printf("%d", a[i]);
            else
                printf(" %d", a[i]);
        }
    }
    return 0;
}

2、C++代码

#include <iostream>
#include <vector>

using namespace std;

vector<int> deleteElements(vector<int>& seqList, int x, int y) {
    vector<int> newList;
    for (int num : seqList) {
        if (num < x || num > y) {
            newList.push_back(num);
        }
    }
    return newList;
}

int main()
{
    int n;
    cin >> n;  // 元素个数

    vector<int> seqList(n);
    for (int i = 0; i < n; i++)
    {
        cin >> seqList[i];  // 顺序表元素
    }

    int x, y;
    cin >> x >> y;  // 区间x和y

    // 删除元素
    vector<int> newList = deleteElements(seqList, x, y);

    // 输出新顺序表
    for (int i = 0; i < newList.size(); i++)
    {
        cout << newList[i];
        if (i != newList.size() - 1)
        {
            cout << " ";
        }
    }

    return 0;
}

3、Python代码

def delete_function(seq_list, x, y):
    new_list = []
    for num in seq_list:
        if num < x or num > y:
            new_list.append(num)
    return new_list

# 读取输入
n = int(input())  # 元素个数
seq_list = list(map(int, input().split()))  # 顺序表元素
x, y = map(int, input().split())  # 区间x和y

# 删除元素
new_list = delete_function(seq_list, x, y)

# 输出新顺序表
print(' '.join(map(str, new_list)), end='')

二、R7-3 单链表的创建及遍历

读入n值及n个整数,建立单链表并遍历输出。

输入格式:

读入n及n个整数。

输出格式:

输出n个整数,以空格分隔(最后一个数的后面没有空格)。

输入样例:

在这里给出一组输入。例如:

2
10 5

输出样例:

在这里给出相应的输出。例如:

10 5

1、偷懒方法

#include <stdio.h>
int main()
{
    int n, a[99];
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++)
    {
        if (i == 0)
            printf("%d", a[i]);
        else
            printf(" %d", a[i]);
    }
    return 0;
}

2、C++代码

#include <iostream>
using namespace std;


// 定义单链表节点结构
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

// 建立单链表
ListNode* buildLinkedList(int n) {
    ListNode* head = nullptr;
    ListNode* tail = nullptr;

    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;

        ListNode* newNode = new ListNode(num);
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    return head;
}

// 遍历输出单链表
void traverseLinkedList(ListNode* head) {
    ListNode* curr = head;
    while (curr != nullptr) {
        cout << curr->val;
        if (curr->next != nullptr) {
            cout << " ";
        }
        curr = curr->next;
    }
}

int main() {
    int n;
    cin >> n;  // 读入n值

    // 建立单链表
    ListNode* head = buildLinkedList(n);

    // 遍历输出单链表
    traverseLinkedList(head);

    return 0;
}

3、Python代码

n = int(input())

nums = list(map(int, input().split()))
print(*nums)

三、R7-4 在有序链表中插入数据

给定一批严格递增排列的整型数据,给定一个x,若x不存在,则插入x,要求插入后保持有序。存在则无需任何操作。

输入格式:

输入有两行:
第一个数是n值,表示链表中有n个数据。后面有n个数,分别代表n个数据。
第二行是要插入的数。

输出格式:

输出插入后的链表数据,以空格分开。行末不能有多余的空格。

输入样例1:

在这里给出一组输入。例如:

5 1 3 6 9 11
4

输出样例1:

在这里给出相应的输出。例如:

1 3 4 6 9 11

输入样例2:

在这里给出一组输入。例如:

5 1 3 6 9 11
3

输出样例2: 

在这里给出相应的输出。例如:

1 3 6 9 11

1、C++代码

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}LNode, *LinkList;

void InsertList(LinkList L, int x)
{
    LinkList p, pp, q;
    pp = (LinkList) malloc (sizeof(LNode));
    pp -> data = x;
    for (q = L -> next; q && q -> data != x; q = q -> next);
    if (q != NULL)    return;
    for (p = L; p -> next && p -> next -> data < x; p = p -> next);
    pp -> next = p -> next;
    p -> next = pp;
}

void PrintList(LinkList L)
{
    LinkList p = L -> next;
    while (p)
    {
        if (p -> next != NULL)
        {
            printf("%d ", p -> data);
            p = p -> next;
        }
        else
        {
            printf("%d", p -> data);
            p = p -> next;
        }
    }
}

int main()
{
    int n, t, i;
    LinkList L = (LinkList) malloc (sizeof(LNode));
    L -> next = NULL;
    if (scanf("%d", &n) == 1)
    {
        for (i = 0; i < n + 1; i++)
        {
            if (scanf("%d", &t) == 1)
                InsertList(L, t);
        }
        PrintList(L);
    }
    return 0;
}

2、Python代码

n, *data = map(int, input().split())
x = int(input())

inserted = False

for i in range(n):
    if x in data:  # 如果x已经存在于数据列表中,无需插入,直接跳出循环
        break
    if x < data[0]:     # 如果比一号位还小,则放在第一位
        data.insert(0, x)
        inserted = True
        break
    if data[i] > x:  # 找到第一个大于x的位置,将x插入在该位置之前
        data.insert(i, x)
        inserted = True
        break

if not inserted and x not in data:  # 比所有元素都大的话,放在最后
    data.append(x)

print(*data)  # 输出插入后的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸢想睡觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值