Hsue66 / Algo

0 stars 0 forks source link

linkedlist #13

Open Hsue66 opened 4 years ago

Hsue66 commented 4 years ago

/*#include using namespace std;

define MAX 100

struct Node { int val; Node prev, next; };

struct List { int size; Node HEAD, TAIL; Node node[MAX];

Node* alloc() {
    return &node[size++];
}

void init() {
    size = 0;
    HEAD = alloc();
    TAIL = alloc();
    HEAD->next = TAIL;
    TAIL->prev = HEAD;
}

void push(int data) {
    Node *nuw = alloc();
    nuw->val = data;

    Node *Prev = TAIL->prev;
    nuw->next = TAIL;
    nuw->prev = Prev;
    Prev->next = nuw;
    TAIL->prev = nuw;
}

void insert(int x, int data) {
    Node *nuw = alloc();
    nuw->val = data;

    Node *it = HEAD->next;
    int c = 0;
    for (; it != TAIL && c != x; it = it->next, c++);
    Node *Prev = it->prev;
    Prev->next = nuw;
    nuw->prev = Prev;
    nuw->next = it;
    it->prev = nuw;
}

void erase(int x) {
    Node *it = HEAD->next;
    int c = 0;
    for (; it != TAIL && c != x; it = it->next, c++);
    Node *Prev = it->prev;
    Prev->next = it->next;
    it->next->prev = Prev;
}

void show() {
    for (Node *it = HEAD->next; it != TAIL; it = it->next)
        cout << it->val << " ";
    cout << endl;
}

};

int main() { List list; list.init(); for(int i=3; i<7; i++) list.push(i); list.show(); list.insert(3, 10); list.show(); list.erase(1); list.show(); }*/