【链表】数据查找和合并

编程入门 行业动态 更新时间:2024-10-22 09:43:57

【<a href=https://www.elefans.com/category/jswz/34/1769662.html style=链表】数据查找和合并"/>

【链表】数据查找和合并

 获取链表中间位置的数据

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

运行结果:

 

更多推荐

【链表】数据查找和合并

本文发布于:2023-11-15 10:20:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链表   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!