双向循环链表-头插法-尾插法

#include
#include

typedef struct roundlink {
    int data;
    struct roundlink* piror;
    struct roundlink* next;
} roundlink, * rlink;

void Initlist(rlink& L) {
    L = (rlink)malloc(sizeof(roundlink));
    if (L == NULL) {
        return;
    }
    L->data = 0;
    L->next = L;
    L->piror = L;
}

void insert(rlink& L, int data) { //尾插法
    roundlink* insert;
    roundlink* temp;
    insert = (roundlink*)malloc(sizeof(roundlink));
    insert->data = data;
    temp = L;
    while (temp->next != L) {
        temp = temp->next;
    }
    insert->next = temp->next;
    temp->next = insert;
    insert->piror = temp;
    L->piror = insert;
}

void print(rlink& L) {
    roundlink* temp;
    temp = L->next;
    while (temp != L) {
        printf("%d\n", temp->data);
        temp = temp->next;
    }
    printf("-----------------------------\n");
}

void tail_delete(rlink& L) {
    roundlink* temp;
    temp = L->piror;
    L->piror = temp->piror;
    temp->piror->next = L;
    free(temp);
}

void head_insert(rlink& L, int data) {//头插法
    roundlink* insert;
    insert = (roundlink*)malloc(sizeof(roundlink));
    insert->data = data;
    insert->next = L->next;
    insert->piror = L;
    L->next->piror = insert;
    L->next = insert;
}
int main() {
    roundlink* L = NULL;
    Initlist(L);
    for (int i = 1; i <= 10; i++) {
        insert(L, i);
    }
    print(L);
    tail_delete(L);
    print(L);
    head_insert(L,99);
    print(L);
    return 0;
}

你可能感兴趣的:(链表,数据结构)