m0_74193820 2024-03-17 16:18 采纳率: 33.3%
浏览 6
已结题

如何用c语言实现顺序表

img


用c语言实现顺序表的一系列操作 应该还是比较简单的 没有学过c 有会的直接把代码发给我 如果采纳直接获得酬金

  • 写回答

7条回答 默认 最新

  • 白驹_过隙 新星创作者: 算法与数据结构技术领域 2024-03-17 16:44
    关注

    img

    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    // 定义链表节点结构体
    typedef struct Node {
        int data;
        struct Node *next;
    } Node;
    
    // 生成有序链表
    Node *createOrderedList(int *arr, int size) {
        Node *head = NULL, *curr = NULL, *temp;
        int i;
    
        for (i = 0; i < size; i++) {
            temp = (Node *)malloc(sizeof(Node));
            temp->data = arr[i];
            temp->next = NULL;
    
            if (head == NULL) {
                head = temp;
                curr = temp;
            } else {
                curr->next = temp;
                curr = temp;
            }
        }
    
        return head;
    }
    
    // 打印链表
    void printList(Node *head, char *msg) {
        Node *curr = head;
        printf("%s", msg);
        printf("%d", curr->data);
        curr = curr->next;
        while (curr != NULL) {
            printf("->%d", curr->data);
            curr = curr->next;
        }
        printf("\n");
    }
    
    // 合并两个有序链表
    Node *mergeLists(Node *head1, Node *head2) {
        Node *curr1 = head1, *curr2 = head2, *head3 = NULL, *curr3 = NULL;
    
        while (curr1 != NULL && curr2 != NULL) {
            Node *temp = (Node *)malloc(sizeof(Node));
            if (curr1->data < curr2->data) {
                temp->data = curr1->data;
                curr1 = curr1->next;
            } else {
                temp->data = curr2->data;
                curr2 = curr2->next;
            }
            temp->next = NULL;
    
            if (head3 == NULL) {
                head3 = temp;
                curr3 = temp;
            } else {
                curr3->next = temp;
                curr3 = temp;
            }
        }
    
        while (curr1 != NULL) {
            Node *temp = (Node *)malloc(sizeof(Node));
            temp->data = curr1->data;
            temp->next = NULL;
            curr3->next = temp;
            curr3 = temp;
            curr1 = curr1->next;
        }
    
        while (curr2 != NULL) {
            Node *temp = (Node *)malloc(sizeof(Node));
            temp->data = curr2->data;
            temp->next = NULL;
            curr3->next = temp;
            curr3 = temp;
            curr2 = curr2->next;
        }
    
        return head3;
    }
    
    // 删除指定位置的节点
    Node *deleteNode(Node *head, int pos) {
        Node *curr = head, *prev = NULL;
        int i;
    
        if (pos == 1) {
            head = curr->next;
            free(curr);
            return head;
        }
    
        for (i = 1; i < pos; i++) {
            prev = curr;
            curr = curr->next;
        }
    
        prev->next = curr->next;
        free(curr);
    
        return head;
    }
    
    // 插入节点
    Node *insertNode(Node *head, int val) {
        Node *curr = head, *prev = NULL, *temp, *newNode;
        int pos = 1;
    
        newNode = (Node *)malloc(sizeof(Node));
        newNode->data = val;
        newNode->next = NULL;
    
        if (head == NULL || val <= head->data) {
            newNode->next = head;
            return newNode;
        }
    
        while (curr != NULL && val > curr->data) {
            prev = curr;
            curr = curr->next;
            pos++;
        }
    
        prev->next = newNode;
        newNode->next = curr;
    
        return head;
    }
    Node *createL2List() {
        Node *head = NULL, *curr = NULL, *temp;
        int i, prev_data = 4;
    
        // 创建第一个节点
        temp = (Node *)malloc(sizeof(Node));
        temp->data = prev_data;
        temp->next = NULL;
        head = temp;
        curr = temp;
    
        // 创建剩余9个节点
        for (i = 1; i < 10; i++) {
            temp = (Node *)malloc(sizeof(Node));
            temp->data = prev_data + (rand() % 9 + 1); // 随机增加1~9
            temp->next = NULL;
            curr->next = temp;
            curr = temp;
            prev_data = temp->data;
        }
    
        return head;
    }
    int main() {
        int arr1[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
        int arr2[] = {4, 5, 11, 13, 18, 20, 25, 33, 40, 49};
        int size1 = sizeof(arr1) / sizeof(int);
        int size2 = sizeof(arr2) / sizeof(int);
        Node *head1, *head2, *head3;
    
        srand(time(NULL)); // 初始化随机数种子
    
        head1 = createOrderedList(arr1, size1);
    
        head2 = createL2List();
    
        printf("[顺序表L1已生成]\n");
        printList(head1, "L1:");
    
        printf("[顺序表L2已生成]\n");
        printList(head2, "L2:");
    
        head3 = mergeLists(head1, head2);
        printf("[L1和L2有序合并到L3]\n");
        printList(head3, "L3:");
    
        int randomNum1 = rand() % 10 + 1;
        head3 = deleteNode(head3, randomNum1);
        printf("[随机删除L3第%d个元素]\n", randomNum1);
        printList(head3, "L3:");
    
        int randomNum2 = rand() % 100 + 1;
        head3 = insertNode(head3, randomNum2);
        printf("[随机产生数字%d插入到L3]\n", randomNum2);
        printList(head3, "L3:");
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 3月25日
  • 已采纳回答 3月17日
  • 创建了问题 3月17日