罗小白


私信TA

用户名:Timmmmy

访问量:16382

签 名:

隔一年又回来刷题了...

等  级
排  名 141
经  验 7192
参赛次数 0
文章发表 46
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

有问题可以互相交流,共同提高 欢迎私信,请多指教:)

解题思路:

注意事项:
        见注释。欢迎讨论~
参考代码:

#include<bits/stdc++.h>
#define DefaultListSize 0
using namespace std;

template <typename Elem>
class Node
{
public:
    Elem element; 
    Node *prev;
    Node *next;   

    Node() {    }
    Node(const Elem& elemval, Node* prevval = NULL, Node* nextval = NULL)
    {
        element = elemval;
        prev = prevval;
        next = nextval;
    }
};

template <typename Elem>
class DList : public Node<Elem>
{
protected:
    Node<Elem>* head; //Point to list header
    Node<Elem>* tail; //Pointer to last Elem
    Node<Elem>* fence;//Last element on left
    int leftcnt;      //Count of nodes at left
    int rightcnt;     //Count of nodes at right

public:
    DList() { init(); }

    void init()
    {
        fence = head = new Node<Elem>('\0', NULL, tail);
        tail  = new Node<Elem>('\0', head, NULL);
        leftcnt = rightcnt = 0;
    }
    ~DList() { removeAll(); }  // Destructor
    void clear() { removeAll(); init(); }
    void removeAll()
    {
        while (head != NULL)
        {
            fence = head;
            head = head->next;
            delete fence;
        }
    }
    int leftLength() const { return leftcnt; }
    int rightLength() const { return rightcnt; }
    int getCount() { return leftcnt + rightcnt; }
    void setStart()
    {
        fence = head;
        rightcnt += leftcnt;
        leftcnt = 0;
    }
    void setEnd()
    {
        fence = tail;
        leftcnt += rightcnt;
        rightcnt = 0;
    }
    void prev()
    {
        if (fence != head)
        {
            fence = fence->prev;
            rightcnt--;
            leftcnt++;
        }
    }
    void next()
    {
        // Don't move fence if right empty
        if (fence != tail)
        {
            fence = fence->next;
            rightcnt--;
            leftcnt++;
        }
    }
    // Get the value of fence
    bool getValue(Elem& it) const
    {
        if (rightLength() == 0) return false;
        it = fence->element;
        return true;
    }
    bool insert(const int pos, const Elem& item)
    {
        if (getCount() == 0) {
            Node<Elem>* tmp = new Node<Elem>(item, head, tail);
            head->next = tmp;
            tail->prev = tmp;
            setStart();
            rightcnt++;
            return true;
        }

        setPos(pos - 1);
        Node<Elem>* tmp = new Node<Elem>(item, fence, fence->next);
        fence->next->prev = tmp;
        fence->next = tmp;
        rightcnt++;

        return true;
    }
    // Remove and return first Elem in right partition
    bool remove(const int pos, Elem& it)
    {
        if (getCount() == 0) return false;

        setPos(pos - 1);
        Node<Elem>* tmp = fence->next;
        it = tmp->element;
        tmp->next->prev = fence;
        fence->next = tmp->next;
        delete tmp;
        rightcnt--;

        return true;
    }
    // Set the size of left partition to pos
    bool setPos(int pos)
    {
        if ((pos < 0) || (pos > getCount())) return false;
        fence = head;
        for (int i = 0; i < pos; i++) {
            fence = fence->next;
        }
        rightcnt = rightcnt + leftcnt - pos;
        leftcnt = pos;
        return true;
    }
    // show
    void show()
    {
        setStart();
        next();
        while (fence) {
            if (fence->next == tail) {
                cout << fence->element << endl;
                return;
            }
            cout << fence->element << " ";
            next();
        }
    }
};

int main()
{
    DList<int> list;
    int cmd, tmp;
    int i, e;
    while (cin >> cmd) {
        if (cmd == 0) {
            list.show();
        }
        else if (cmd == 1) {
            cin >> i >> e;
            list.insert(i, e);
        }
        else {
            cin >> i;
            list.remove(i, tmp);
        }
    }
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区