链表实验.

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

// 定义单链表节点结构体
struct Node {
    int data;
    struct Node* next;
};

struct Node* initList() {
    struct Node* list = (struct Node*)malloc(sizeof(struct Node));
    list->data = 0;
    list->next = NULL;
    return list;
}

void headinsert(struct Node* list) {
    struct Node* tail = list;
    int num;
    printf("请输入整数序列(以0结束):\n");

    while (1) {
        scanf("%d", &num);
        if (num == 0) {
            break;
        }
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = num;
        newNode->next = NULL;
        tail->next = newNode;
        tail = newNode;
    }
}

void printList(struct Node* head) {
    printf("链表的各个元素值为:\n");
    struct Node* current = head->next; // 跳过头节点
    while (current) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int findPosition(struct Node* head, int x) {
    int position = 0;
    head = head->next;
    while (head != NULL) {
        position++;
        if (head->data == x) {
            return position;
        }
        head = head->next;
    }
    return 0;
}

void insertElement(struct Node* list, int i, int x) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = x;
    newNode->next = NULL;

    if (i <= 0) {
        newNode->next = list->next;
        list->next = newNode;
    }
    else {
        struct Node* current = list;
        int j = 0;
        while (current && j < i - 1) {
            current = current->next;
            j++;
        }
        if (current) {
            newNode->next = current->next;
            current->next = newNode;
        }
        else {
            printf("Error: Index out of range\n");
            free(newNode); // 释放新节点内存
        }
    }
}

int countnumber(struct Node* head, int x) {
    int count = 0;
    head = head->next;
    while (head != NULL) {
        if (head->data == x) {
            count++;
        }
        head = head->next;
    }
    return count;
}


int main() {
    struct Node* list = initList();
    headinsert(list);
    printList(list);

    int x = 0;
    printf("请输入要查找的元素的值:");
    scanf("%d", &x);
    int position = findPosition(list, x);
    if (position == 0) {
        printf("值为%d的元素不在链表中", x);
        printf("\n");
    }else{
        printf("值为%d的元素所在位置为%d", x, position);
        printf("\n");
    }

    int i = 0;
    int a = 0;
    printf("请输入要插入元素的值:");
    scanf("%d", &a);
    printf("请输入要插入元素的位置:");
    scanf("%d", &i);
    insertElement(list, i, a);
    printList(list);

    int b = 0;
    int number = 0;
    printf("请输入要查找元素个数的值:\n");
    scanf("%d", &b);
    number = countnumber(list, b);
    printf("值为%d的节点的个数为%d", b, number);

    return 0;
}

1. (算法设计题, 35分)设计算法实现将两个递增的带头结点有序链表合并为一个递增的有序链表,要求结果链表仍然使用原来两个链表的存储空间,表中不允许有重复的数据。

void mergeLists(struct ListNode* head1, struct ListNode* head2) {

    struct ListNode* p1 = head1->next;

    struct ListNode* p2 = head2->next;

    struct ListNode* prev = head1;

    while (p1 && p2) {

        if (p1->val < p2->val) {

            prev->next = p1;

            p1 = p1->next;

        } else if (p1->val > p2->val) {

            prev->next = p2;

            p2 = p2->next;

        } else {

            prev->next = p1; // 保留一个相同的节点

            p1 = p1->next;

            p2 = p2->next;

        }

        prev = prev->next;

    }

    if (p1)

        prev->next = p1;

    if (p2)

        prev->next = p2;

}

初始化指针 p1 和 p2 分别指向两个链表的第一个实际节点。

初始化指针 prev 指向 head1,用于追踪结果链表的最后一个节点。

循环比较两个链表的节点值,将较小的节点接入结果链表,并更新对应链表的指针。

如果两个节点值相同,只保留一个节点。

最后,将剩余未遍历完的链表直接接到结果链表的末尾。算法实现将两个递增的带头结点有序链表合并为一个递增的有序链表,结果链表仍然使用原来两个链表的存储空间,没有重复的数据。

有一个带头结点的单链表L,编写在值为x的结点之后插入m个结点的算法。

void insertAfterX(struct ListNode* head, int x, int m) {

    struct ListNode* current = head->next;

    while (current != NULL && current->val != x) {

        current = current->next;

    }

    int number=0;

    if (current != NULL) {

        for (int i = 0; i < m; i++) {

             printf("请输入要插入节点的值:\n");

             scanf("%d",&number);

            struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));

            newNode->val = number; 

            newNode->next = current->next;

            current->next = newNode;

        }

    } else {

        printf("没有找到值为%d的结点",x);

    }

}

遍历链表,寻找值为 x 的结点。

如果找到了值为 x 的结点,则在其后插入 m 个新的结点。

新插入的结点值可以简单地从 1 开始递增。

如果未找到值为 x 的结点,则输出提示信息。

相关推荐

  1. c实现

    2024-04-08 08:26:03       27 阅读
  2. go实现

    2024-04-08 08:26:03       17 阅读
  3. 实现

    2024-04-08 08:26:03       32 阅读
  4. 双向实现

    2024-04-08 08:26:03       20 阅读
  5. rust实现循环

    2024-04-08 08:26:03       18 阅读
  6. 双向实现

    2024-04-08 08:26:03       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-08 08:26:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-08 08:26:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-08 08:26:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-08 08:26:03       20 阅读

热门阅读

  1. 2024.4.7力扣刷题记录-数组篇刷题记录2

    2024-04-08 08:26:03       14 阅读
  2. 蓝桥杯常用模板

    2024-04-08 08:26:03       13 阅读
  3. 设计模式:生活中的迭代器模式

    2024-04-08 08:26:03       14 阅读
  4. [iOS]进程-线程-队列-任务

    2024-04-08 08:26:03       14 阅读
  5. Vue 打包自动清理缓存

    2024-04-08 08:26:03       13 阅读
  6. 清明节作业

    2024-04-08 08:26:03       16 阅读
  7. Python列表排序

    2024-04-08 08:26:03       13 阅读
  8. 面试题Spring、SpringMVC、SpringBoot、SpringCloud

    2024-04-08 08:26:03       13 阅读
  9. ASTM F2613-19儿童折叠椅和凳子安全标准

    2024-04-08 08:26:03       14 阅读