于归


私信TA

用户名:dotcpp0681287

访问量:193

签 名:

等  级
排  名 9004
经  验 1128
参赛次数 0
文章发表 7
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:
该程序根据输入的操作类型进行相应的操作:

 - 当操作为0时,程序会打印双向链表中的所有元素。

 - 当操作为1时,程序会在指定位置插入一个整数。

 - 当操作为2时,程序会从指定位置删除一个整数。

使用双向链表来存储整数,每个节点包含整数值、指向前一个节点的指针和指向下一个节点的指针。


注意事项:
在 createNode函数中,需要为新节点分配内存,并将整数值赋给节点的成员变量。

在 insertNode函数中,需要根据指定的位置插入新节点,注意处理头节点和中间节点的情况。

在 deleteNode函数中,需要根据指定的位置删除节点,注意处理头节点和中间节点的情况。

在 printList函数中,需要遍历链表并打印每个节点的整数值。


参考代码:

#include <stdio.h>

#include <stdlib.h>


// 定义双向链表节点结构体

typedef struct Node {

    int data;

    struct Node* prev;

    struct Node* next;

} Node;


// 函数声明

Node* createNode(int data);

Node* insertNode(Node* head, int position, int data);

Node* deleteNode(Node* head, int position);

void printList(Node* head);


int main() {

    Node* head = NULL;

    int operation, position, data;


    while (scanf("%d", &operation) != EOF) {

        if (operation == 0) {

            // 输出双向链表中的所有元素

            printList(head);

        } else if (operation == 1) {

            // 插入一个整数到双向链表中

            scanf("%d %d", &position, &data);

            head = insertNode(head, position, data);

        } else if (operation == 2) {

            // 从双向链表中删除一个整数

            scanf("%d", &position);

            head = deleteNode(head, position);

        }

    }


    // 释放链表内存

    Node* current = head;

    while (current != NULL) {

        Node* temp = current;

        current = current->next;

        free(temp);

    }


    return 0;

}


// 创建一个双向链表节点

Node* createNode(int data) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    newNode->data = data;

    newNode->prev = NULL;

    newNode->next = NULL;

    return newNode;

}


// 在指定位置插入一个整数到双向链表中

Node* insertNode(Node* head, int position, int data) {

    Node* newNode = createNode(data);


    if (head == NULL) {

        // 空链表,直接将新节点作为头节点

        head = newNode;

    } else if (position == 1) {

        // 在头部插入新节点

        newNode->next = head;

        head->prev = newNode;

        head = newNode;

    } else {

        // 在指定位置插入新节点

        Node* current = head;

        int count = 1;


        while (count < position - 1 && current->next != NULL) {

            current = current->next;

            count++;

        }


        if (count == position - 1) {

            newNode->next = current->next;

            newNode->prev = current;

            if (current->next != NULL) {

                current->next->prev = newNode;

            }

            current->next = newNode;

        }

    }


    return head;

}


// 从指定位置删除一个整数

Node* deleteNode(Node* head, int position) {

    if (head == NULL) {

        // 空链表,无需删除

        return head;

    }


    if (position == 1) {

        // 删除头节点

        Node* temp = head;

        head = head->next;

        if (head != NULL) {

            head->prev = NULL;

        }

        free(temp);

    } else {

        // 删除指定位置节点

        Node* current = head;

        int count = 1;


        while (count < position && current != NULL) {

            current = current->next;

            count++;

        }


        if (count == position && current != NULL) {

            current->prev->next = current->next;

            if (current->next != NULL) {

                current->next->prev = current->prev;

            }

            free(current);

        }

    }


    return head;

}


// 打印双向链表中的所有元素

void printList(Node* head) {

    Node* current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区