线性表维护(67)

#include
#include
using namespace std;

struct st_node
{
    string k;
    struct st_node * next;
};
typedef struct st_node ptype;
typedef ptype* node;

node root, last, p, back, blast;
string x2, s;
int x1;

void LinearAdd(string& x)
{
    node q;
    if (back->next)
    {
        q = back->next;
        back->next = back->next->next;
    }
    else
        q = new ptype;
    q->k = x;
    q->next = NULL;
    last->next = q;
    last = q;
}
void LinearMove(int& x)
{
    if (x == 0)
        p = root;
    else
    {
        while (x > 0)
        {
            if (p->next == NULL)
                break;
            p = p->next;
            --x;
        }
    }
    
}
void LinearDel(int& x)
{
    node q = p;
    while (x > 0)
    {
        q = q->next;
        if (q == NULL)
            break;
        --x;
    }
    if (back->next == NULL)
        blast = back;
    blast->next = p->next;
    blast = q;
    p->next = q->next;
    if (q->next == NULL)
        last = p;
    q->next = NULL;
}

int main()
{
    root = new ptype;
    root->next = NULL;
    p = last = root;

    back = new ptype;
    back->next = NULL;
    blast = back;
    while (cin >> s)
    {
        if (s == "ADD")
        {
            cin >> x2;
            LinearAdd(x2);
        }
        else if (s == "MOVE")
        {
            cin >> x1;
            LinearMove(x1);
        }
        else if (s == "DEL")
        {
            cin >> x1;
            LinearDel(x1);
        }
        else if (s == "PRINT")
        {
            if (p->next)
                cout << p->next->k << endl;
        }
        else
            break;
    }
    return 0;
}

你可能感兴趣的:(算法)