【链表】数据查找和合并

获取链表中间位置的数据

复制代码
#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;
}

运行结果:

相关推荐
月盈缺5 小时前
学习嵌入式的第二十二天——数据结构——双向链表
数据结构·学习·链表
科大饭桶6 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
躲在云朵里`7 小时前
深入理解数据结构:从数组、链表到B树家族
数据结构·b树
1白天的黑夜110 小时前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表
养成系小王16 小时前
四大常用排序算法
数据结构·算法·排序算法
闪电麦坤9518 小时前
数据结构:从前序遍历序列重建一棵二叉搜索树 (Generating from Preorder)
数据结构··二叉搜索树
闪电麦坤9518 小时前
数据结构:二叉树的遍历 (Binary Tree Traversals)
数据结构·二叉树·
球king18 小时前
数据结构中邻接矩阵中的无向图和有向图
数据结构
野渡拾光20 小时前
【考研408数据结构-05】 串与KMP算法:模式匹配的艺术
数据结构·考研·算法
pusue_the_sun1 天前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树