双向链表

#include <iostream>

using namespace std;

struct Link {
    struct Link* prior;
    int data;
    struct Link* next;
};

Link* InitLink(Link *head) {
    head = new Link;//创建首元节点
    head->data = 1;
    head->next = NULL;
    head->prior = NULL;

    Link* temp = head;
    for (int i = 2; i <= 15; ++i) {
        Link* list = new Link;//创建后面的节点
        list->data = i;
        list->next = NULL;
        list->prior = NULL;

        temp->next = list;//next往后一节点指,prior往前一节点指
        list->prior = temp;
        temp = temp->next;
    }
    
    return head;
}

Link* Delete(Link* head, int data) {
    Link* temp = head;
    while (temp) {
        if (temp->data == data) {
            temp->prior->next = temp->next;
            temp->next->prior = temp->prior;
            delete temp;

            return head;
        }
        temp = temp->next;
    }

    printf("没有这个数据");
    return head;
}

void Add(Link* head, int pos, int elem) {
    Link* temp2 = new Link;//存储数据节点(新增
    temp2->data = elem;
    temp2->next = NULL;
    temp2->prior = NULL;

    if (pos == 1) {//当在首元节点前插入
        temp2->next = head;
        head->prior = temp2;
        head = temp2;
    }
    else {
        Link* temp = head;
        for (int i = 1; i < pos - 1; ++i) {//遍历到要插入节点前一节点位置
            temp = temp->next;
        }
        if (temp->next == NULL) {//末尾节点
            temp2->prior = temp;
            temp->next = temp2;
        }
        else {//中间节点增加
            temp->next->prior = temp2;//必须在temp->next = temp2;前
            temp2->next = temp->next;//必须在temp->next = temp2;前
            temp->next = temp2;
            temp2->prior = temp;
            //temp2->next = temp->next; error
            //temp->next->prior = temp2; error顺序很重要
        }
    }
}

void Display(Link* head) {
    Link* temp = head;
    while (temp->next != NULL) {//遍历到最后一个节点,准备逆序输出
        temp = temp->next;
    }
    while (temp) {
        if (temp->prior == NULL) {
            cout << temp->data << endl;
        }
        else {
            cout << temp->data << "<->";
        }
        temp = temp->prior;
    }
}

int Select(Link* head, int num) {
    Link* temp = head;
    int pos = 1;
    while (temp->data != num) {//循环遍历,数据相等,返回一个位置
        ++pos;
        temp = temp->next;
        if (temp == NULL) {
            cerr << "未找到" << num << endl;
            exit(1);
        }
    }

    return pos;
}

void alter(Link* head, int oldvalue, int newvalue) {
    int n = Select(head, oldvalue);
    Link* temp = head;
    for (int i = 1; i < n; ++i) {
        temp = temp->next;
    }
    temp->data = newvalue;
}

int main()
{
    Link* head = NULL;

    cout << "双向链表为:" << endl;
    head = InitLink(head);
    Display(head);

    cout << endl;

    cout << "删除操作之后的链表:" << endl;
    head = Delete(head, 10);
    Display(head);

    cout << endl;

    cout << "增加之后的链表:" << endl;
    Add(head, 3, 12);
    Display(head);

    cout << endl;

    cout << "搜寻之后的链表:" << endl;
    int m = Select(head, 5);
    cout << "位置:" << m << endl;
    
    cout << endl;

    cout << "更改节点数据之后的链表:" << endl;
    alter(head, 8, 88);
    Display(head);

    system("PAUSE");
    return 0;
}

 

posted @ 2018-12-21 13:38  Hk_Mayfly  阅读(223)  评论(0编辑  收藏  举报