1803. Link-list

Andrew-Malcom

include

using namespace std;
typedef struct link{
        int data;
        struct link *next;
}linklist;
linklist* create(int n)
{
        linklist *head,*near,*middle;
        head=(linklist*)malloc(sizeof(linklist));
        near=(linklist*)malloc(sizeof(linklist));
        head=near;
        for(int i=0;i<n;i++){
                middle=(linklist*)malloc(sizeof(linklist));
                cin>>middle->data;
                near->next=middle;
                near=middle;
        }
        near->next=NULL;
        return head;
}
int main()
{
        int n;cin>>n;
        linklist *p=(linklist*)malloc(sizeof(linklist));
        p=create(n);int k;cin>>k;
        while(k--){
                int a,b;cin>>a>>b;
                linklist *pr=(linklist*)malloc(sizeof(linklist));
                linklist* god=(linklist*)malloc(sizeof(linklist));
                pr=p;
                while((pr=pr->next)!=NULL){
                        if(pr->data==a){
                                god->next=pr->next;
                                pr->next=god;
                                god->data=b;
                        }
                }
                pr=p;
                while((pr=pr->next)!=NULL){
                        cout<<pr->data<<" ";
                }
                cout<<"\n";
        }
}
Fifnmar

虽然不知道是什么,我猜可能是写一个链表。由于要对链表随机访问,所以再维护一个哈希表。

#include <cstdint>
#include <iostream>
#include <stdint.h>
#include <string>
#include <unordered_map>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    uint32_t n;
    cin >> n;
    struct Node {
        int32_t val;
        Node *prev;
        Node *next;
    };
    Node *head = new Node; // Sentinel;
    Node *tail = head;
    unordered_map<int32_t, Node *> m;
    for (uint32_t i = 0; i < n; ++i) {
        tail = tail->next = new Node{0, tail, nullptr};
        cin >> tail->val;
        m.insert({tail->val, tail});
    }
    uint32_t q;
    cin >> q;
    for (uint32_t i = 0; i < q; ++i) {
        int32_t val, new_val;
        cin >> val >> new_val;
        auto temp = m[val];
        if (temp->next == nullptr) {
            temp->next = new Node{new_val, temp, nullptr};
        } else {
            temp->next = temp->next->prev = new Node{new_val, temp, temp->next};
        }
        m.insert({new_val, temp->next});
        for (auto iter = head->next; iter != nullptr; iter = iter->next) {
            cout << iter->val << ' ';
        }
        cout.put('\n');
    }
}
Fifnmar

这题也太局限了,我这个没上这课的人完全不知道课本是什么 Orz

你当前正在回复 博客/题目
存在问题!