获取链表中间位置的数据

#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;
}

运行结果:

【链表】数据查找和合并-LMLPHP 

合并两个链表

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

#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;
}

运行结果:

【链表】数据查找和合并-LMLPHP 

11-11 11:46