【链表】数据查找和合并

获取链表中间位置的数据

复制代码
#include <stdio.h>
#include <stdlib.h>

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

/* 获取链表中处于中间位置的元素并打印出来*/
void printMiddle(struct Node *head)
{
    struct Node *slow_ptr = head;
    struct Node *fast_ptr = head;

    if (head != NULL)
    {
        while (fast_ptr != NULL && fast_ptr->next != NULL)
        {
            fast_ptr = fast_ptr->next->next;
            slow_ptr = slow_ptr->next;
        }
        printf("The middle element is [%d]\n\n", slow_ptr->data);
    }
}

void push(struct Node **head_ref, int new_data)
{
    /* 分配新节点 */
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));

    /* 加入新的数据 */
    new_node->data = new_data;

    /* 将新节点加入原来的链表中 */
    new_node->next = (*head_ref);

    /* 将链表头指向新节点 */
    (*head_ref) = new_node;
}

/* 打印链表 */
void printList(struct Node *ptr)
{
    while (ptr != NULL)
    {
        printf("%d->", ptr->data);
        ptr = ptr->next;
    }
    printf("NULL\n");
}


int main()
{
    struct Node *head = NULL;
    int i;

    for (i = 5; i > 0; i--)
    {
        push(&head, i);
        printList(head);
        printMiddle(head);
    }

    return 0;
}

运行结果:

合并两个链表

将两个单独的链表合并成一个链表

复制代码
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};

struct node *head1 = NULL;
struct node *head2 = NULL;

/* 合并链表的算法 */

void merge()
{
    struct node *temp1 = head1;
    struct node *temp2 = head2;

    /* 两个临时变量存储两个待操作链表的下一个节点 */
    struct node *holder1 = NULL;
    struct node *holder2 = NULL;

    while (temp1 != NULL && temp2 != NULL)
    {
        holder1 = temp1->next; //存储第一个链表的下一个节点
        temp1->next = temp2; //第一个链表的第一个节点指向第二个链表的第一个节点

        if (holder1 != NULL)
        {
            /* 第二个链表的第一个节点指向第一个链表的第二个节点 */
            holder2 = temp2->next;
            temp2->next = holder1;
        }
        
        /* 更新两个变量temp1和temp2的位置 */
        temp1 = holder1;
        temp2 = holder2;
    }
}

void printlist(struct node *temp)
{
    printf("%d", temp->data);
    temp = temp->next;
    while (temp != NULL)
    {
        printf("->%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main()
{
    // Linked List 1: 1->3->5->7   :   Linked List 2: 2->4->6
    /* 创建8个节点 */
    struct node *one = (struct node *)malloc(sizeof(struct node));
    struct node *two = (struct node *)malloc(sizeof(struct node));
    struct node *three = (struct node *)malloc(sizeof(struct node));
    struct node *four = (struct node *)malloc(sizeof(struct node));
    struct node *five = (struct node *)malloc(sizeof(struct node));
    struct node *six = (struct node *)malloc(sizeof(struct node));
    struct node *seven = (struct node *)malloc(sizeof(struct node));
    struct node *eight = (struct node *)malloc(sizeof(struct node));

    head1 = one; //head1指向第一个链表的第一个节点
    head2 = two; //head2指向第二个链表的第一个节点

    one->data = 1;
    one->next = three;

    two->data = 2;
    two->next = four;

    three->data = 3;
    three->next = five;

    four->data = 4;
    four->next = six;

    five->data = 5;
    five->next = seven;

    /* 第二个链表的最后一个节点 */
    six->data = 6;
    six->next = eight;

    /* 第一个链表的最后一个节点 */
    seven->data = 7;
    seven->next = NULL;
 
    eight->data = 8;
    eight->next = NULL;

    printf("Linked List 1: ");
    printlist(head1);
    printf("\nLinked List 2: ");
    printlist(head2);

    /* 合并两个链表成为一个链表 */
    merge();

    printf("\nMerged Linked List: ");
    printlist(head1);  //合并后的链表

    return 0;
}

运行结果:

相关推荐
张张努力变强39 分钟前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
wWYy.1 小时前
数组快排 链表归并
数据结构·链表
李斯啦果1 小时前
【PTA】L1-019 谁先倒
数据结构·算法
Mr Xu_17 小时前
告别硬编码:前端项目中配置驱动的实战优化指南
前端·javascript·数据结构
czxyvX17 小时前
017-AVL树(C++实现)
开发语言·数据结构·c++
数智工坊17 小时前
【数据结构-队列】3.2 队列的顺序-链式实现-双端队列
数据结构
elseif12317 小时前
【C++】并查集&家谱树
开发语言·数据结构·c++·算法·图论
徐小夕@趣谈前端18 小时前
Web文档的“Office时刻“:jitword共建版2.0发布!让浏览器变成本地生产力
前端·数据结构·vue.js·算法·开源·编辑器·es6
Nebula_g18 小时前
线程进阶: 无人机自动防空平台开发教程(更新)
java·开发语言·数据结构·学习·算法·无人机
xuxie9919 小时前
day 23 树
数据结构