编辑代码

#include <stdio.h>
#include <stdlib.h>
typedef struct line {
    struct line* prior;//指向前结点
    int data;
    struct line* next;//指向后结点
}line;
//双链表的创建函数
line* initLine(line* head);
//输出双链表的函数
void display(line* head);
//添加数据
line* insertLine(line* head, int data, int add);
//删除结点
line* delLine(line* head, int data);
//查找元素
int selectElem(line* head, int elem);
//更改元素
line* updateElem(line* p, int add, int newElem);
int main() {
    //创建一个头指针
    line* head = NULL;
    head = initLine(head);
    display(head);
    //从表中第3的位置插入元素7
    head = insertLine(head, 7, 3);
    display(head);
    //表中删除元素2
    head = delLine(head, 2);
    display(head);
    //修改表中第3个结点中的数据改为存储6
    head = updateElem(head, 3, 6);
    display(head);
    return 0;
}
line* initLine(line* head) {
    head = (line*)malloc(sizeof(line));//创建链表第一个结点
    head->prior = NULL;
    head->next = NULL;
    head->data = 1;
    line* list = head;
    for (int i = 2; i <= 5; i++) {
        //创建并初始化一个新结点
        line* body = (line*)malloc(sizeof(line));
        body->prior = NULL;
        body->next = NULL;
        body->data = i;

        list->next = body;//直接前驱结点的next指针指向新节点
        body->prior = list;//新节点指向直接前驱结点
        list = list->next;
    }
    return head;
}

void display(line* head) {
    line* temp = head;
    while (temp) {
        //如果该结点没有后继节点,说明此节点是链表的最后一个结点
        if (temp->next == NULL) {
            printf("%d\n", temp->data);
        }
        else {
            printf("%d<->", temp->data);
        }
        temp = temp->next;
    }
}
line* insertLine(line* head, int data, int add) {
    //新建数据域为data的结点
    line* temp = (line*)malloc(sizeof(line));
    temp->data = data;
    temp->prior = NULL;
    temp->next = NULL;
    //插入到链表头,要特殊考虑
    if (add == 1) {
        temp->next = head;
        head->prior = temp;
        head = temp;
    }
    else {
        line* body = head;
        //找到要插入位置的前一个结点
        for (int i = 1; i < add - 1; i++) {
            body = body->next;
        }
        //判断条件为真,说明插入位置为链表尾
        if (body->next == NULL) {
            body->next = temp;
            temp->prior = body;
        }
        else {
            body->next->prior = temp;
            temp->next = body->next;
            body->next = temp;
            temp->prior = body;
        }
    }
    return head;
}
line* delLine(line* head, int data) {
    line* temp = head;
    //遍历链表
    while (temp) {
        //判断当前结点中数据域和data是否相等,若相等,摘除该结点
        if (temp->data == data) {
            temp->prior->next = temp->next;
            temp->next->prior = temp->prior;
            free(temp);
            return head;
        }
        temp = temp->next;
    }
    printf("链表中无该数据元素");
    return head;
}
int selectElem(line* head, int elem) {
    //新建一个指针t,初始化为头指针head
    line* t = head;
    int i = 1;
    while (t) {
        if (t->data == elem) {
            return i;
        }
        i++;
        t = t->next;
    }
    return -1;
}
line* updateElem(line* p, int add, int newElem) {
    line* temp = p;
    //遍历到被删除结点
    for (int i = 1; i < add; i++) {
        temp = temp->next;
    }
    temp->data = newElem;
    return p;
}