线性表插入删除 数据结构

我们刚学,没有看见有不带void和bool的写这种运算的,我们不让用带void和bool,那应该怎么写呢?主函数里输入数字要用循环。


#include <iostream>
using namespace std;

class LinearList {
private:
    int capacity;
    int size;
    int* data;

public:
    LinearList(int capacity) {
        this->capacity = capacity;
        this->size = 0;
        this->data = new int[capacity];
    }

    ~LinearList() {
        delete[] data;
    }

    bool is_empty() {
        return size == 0;
    }

    bool is_full() {
        return size == capacity;
    }

    void insert(int index, int value) {
        if (is_full()) {
            cout << "The linear list is full. Cannot insert element." << endl;
            return;
        }
        if (index < 0 || index > size) {
            cout << "Invalid index. Cannot insert element." << endl;
            return;
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = value;
        size++;
    }

    int remove(int index) {
        if (is_empty()) {
            cout << "The linear list is empty. Cannot remove element." << endl;
            return -1;
        }
        if (index < 0 || index >= size) {
            cout << "Invalid index. Cannot remove element." << endl;
            return -1;
        }
        int deleted_value = data[index];
        for (int i = index; i < size - 1; i++) {
            data[i] = data[i + 1];
        }
        data[size - 1] = 0;
        size--;
        return deleted_value;
    }
};

int main() {
    LinearList linear_list(5);

    cout << "Inserting elements:" << endl;
    for (int i = 0; i < 5; i++) {
        linear_list.insert(i, i + 1);
    }

    cout << "Size: " << linear_list.size << endl;
    cout << "Data: ";
    for (int i = 0; i < linear_list.size; i++) {
        cout << linear_list.data[i] << " ";
    }
    cout << endl;

    cout << "\nRemoving elements:" << endl;
    for (int i = 0; i < linear_list.size; i++) {
        cout << "Deleted value: " << linear_list.remove(0) << endl;
        cout << "Size: " << linear_list.size << endl;
        cout << "Data: ";
        for (int j = 0; j < linear_list.size; j++) {
            cout << linear_list.data[j] << " ";
        }
        cout << endl;
    }

    return 0;
}

【以下回答由 GPT 生成】

解决方案

1. 插入操作

插入操作可以通过找到要插入位置的前一个节点,并调整指针来完成。

以下是一个示例代码,实现了在一个链表中插入一个新节点的操作:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void insertNode(Node** head, int position, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;

    if (position == 0) {  // 插入到链表头部
        newNode->next = *head;
        *head = newNode;
    } else {
        Node* currentNode = *head;
        for (int i = 0; i < position - 1; i++) {
            if (currentNode->next == NULL) {
                printf("无法在指定位置插入节点\n");
                return;
            }
            currentNode = currentNode->next;
        }
        newNode->next = currentNode->next;
        currentNode->next = newNode;
    }
}

void printList(Node* head) {
    Node* currentNode = head;
    while (currentNode != NULL) {
        printf("%d ", currentNode->data);
        currentNode = currentNode->next;
    }
    printf("\n");
}

int main() {
    Node* head = NULL;

    insertNode(&head, 0, 1);  // 在链表头部插入节点
    insertNode(&head, 1, 3);  // 在链表第二个位置插入节点
    insertNode(&head, 1, 2);  // 在链表第二个位置插入节点

    printList(head);  // 输出链表:1 2 3

    return 0;
}

这段代码定义了一个链表节点的结构体Node,插入操作的函数insertNode以及打印链表的函数printList。在主函数中将链表的头部指针传递给insertNode函数来进行插入操作,然后使用printList函数打印链表。

2. 删除操作

删除操作可以通过找到要删除节点的前一个节点,调整指针并释放内存来完成。

以下是一个示例代码,实现了在一个链表中删除指定位置的节点的操作:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void deleteNode(Node** head, int position) {
    if (*head == NULL) {
        printf("链表为空,无法删除节点\n");
        return;
    }

    Node* currentNode = *head;
    if (position == 0) {  // 删除头节点
        *head = currentNode->next;
        free(currentNode);
    } else {
        for (int i = 0; i < position - 1; i++) {
            if (currentNode->next == NULL) {
                printf("无法在指定位置删除节点\n");
                return;
            }
            currentNode = currentNode->next;
        }
        Node* nodeToDelete = currentNode->next;
        currentNode->next = nodeToDelete->next;
        free(nodeToDelete);
    }
}

void printList(Node* head) {
    Node* currentNode = head;
    while (currentNode != NULL) {
        printf("%d ", currentNode->data);
        currentNode = currentNode->next;
    }
    printf("\n");
}

int main() {
    Node* head = NULL;

    // 创建一个有3个节点的链表
    head = (Node*)malloc(sizeof(Node));
    head->data = 1;
    head->next = (Node*)malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = (Node*)malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

    deleteNode(&head, 1);  // 删除链表第二个位置的节点

    printList(head);  // 输出链表:1 3

    return 0;
}

这段代码同样定义了一个链表节点的结构体Node,删除操作的函数deleteNode以及打印链表的函数printList。在主函数中先创建一个有3个节点的链表,然后使用deleteNode函数删除链表中的一个节点,最后使用printList函数打印链表。

3. 循环读取用户输入的数字

在主函数中实现一个循环,可以使用while循环来不断接收用户输入的数字,并根据用户输入的命令执行相应的操作。

以下是一个示例代码,实现了循环读取用户输入的数字,并根据用户输入的命令执行插入和删除操作的功能:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void insertNode(Node** head, int position, int data) {
    // 插入节点的代码,同上
}

void deleteNode(Node** head, int position) {
    // 删除节点的代码,同上
}

void printList(Node* head) {
    // 打印链表的代码,同上
}

int main() {
    Node* head = NULL;
    int input, position, data;

    while (1) {
        printf("请输入操作代号(1-插入, 2-删除, 3-打印, 0-退出):");
        scanf("%d", &input);

        switch (input) {
            case 1:  // 插入操作
                printf("请输入要插入的位置和数据(以空格分隔):");
                scanf("%d %d", &position, &data);
                insertNode(&head, position, data);
                break;
            case 2:  // 删除操作
                printf("请输入要删除的位置:");
                scanf("%d", &position);
                deleteNode(&head, position);
                break;
            case 3:  // 打印链表
                printList(head);
                break;
            case 0:  // 退出循环
                return 0;
            default:  // 无效操作
                printf("无效的操作代号\n");
                break;
        }
    }
}

这段代码在循环中通过scanf函数读取用户输入的操作代号,然后根据输入的代号执行相应的操作。其中,插入操作需要输入要插入的位置和数据,删除操作需要输入要删除的位置。

希望以上解决方案对你有帮助,如果有任何疑问请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^