单链表的应用(2)

编程入门 行业动态 更新时间:2024-10-11 21:30:24

单<a href=https://www.elefans.com/category/jswz/34/1769662.html style=链表的应用(2)"/>

单链表的应用(2)

环形链表的约瑟夫问题

编号为 1nn 个人围成一圈。从编号为 1 的人开始报数,报到 m 的人离开。
下一个人继续从 1 开始报数。
n-1 轮结束以后,只剩下一个人,问最后留下的这个人编号是多少?

  • 利用链表实现
    思路:(1)创建一个不带头单向循环链表,需要注意的是链表创建后返回的结点是最后一个结点,为的是链表可快速找到第一个结点和最后一个结点
    (2)创建结构体指针prevcur,分别代表最后一个结点和第一个结点,因为cur已经为第一个结点,因此count=1。遍历链表直到pcurnext还是pcur(即链表中只含有一个结点)时退出循环,循环过程中当countm时需要将当前位置的pcur置空,count重置为1。不为count时,只需将链表往后执行即可
    (3)退出循环后,返回cur->val即可
 typedef struct ListNode ListNode;ListNode* ListBuyNode(int x){ListNode* node=(ListNode*)malloc(sizeof(ListNode));if(node == NULL){perror("malloc:");exit(1);}node->val=x;node->next=NULL;return node; }ListNode* CreatList(int n)
{ListNode* head=ListBuyNode(1);ListNode* tail=head;for(int i=2;i<=n;i++){ListNode* node=ListBuyNode(i);tail->next=node;tail=tail->next;}tail->next=head;return tail;// !!!
}int ysf(int n, int m ) 
{ListNode* prev=CreatList(n);ListNode* cur=prev->next;int count=1;while(cur->next != cur){if(count == m){prev->next=cur->next;free(cur);cur=prev->next;count=1;}else {prev=cur;cur=cur->next;count++;}}return cur->val;
}
  • 利用循环语句实现
    思路:(1)利用i,形成一个可循环遍历的类似圆形的数组
    (2)利用j,来判断报的数,当报的数正好为m时,将a[i]赋值为1,并且不进行下面的循环,直到数组中仅剩一个数组的值为0
    (3)退出循环,遍历数组输出值为0的数组的下标
#include<stdio.h>int main()
{int n = 0;int m = 0;scanf("%d %d",&n,&m);int a[30] = { 0 };int count = 0;int i = 0;int j = 0;while (count < n - 1){i++;if (i>n)i = 1;if (a[i] == 0){j++;if (j % m == 0){count++;a[i] = 1;j = 0;}}}for (i = 1; i < n; i++){if (a[i] != 1){printf("%d\n", i);break;}}return 0;
}

分割链表

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有小于x的节点都出现在 大于或等于x的节点之前。
你不需要保留每个分区中各节点的初始相对位置。

思路:(1)判断head是否为空,空则直接返回head
(2)创建两个两个带头单向不循环链表,一个存放小于x的值的结点,一个存放大于等于x的值的结点。lessheadgreaterhead分别为两个链表的头结点,lesstailgreatertail分别为两个链表的尾结点。
(3)创建一个pcur代替head进行链表遍历,当pcurval小于x时将pcur存入less链表,大于等于x时将pcur存入greater链表
(4)遍历结束判断greatertail是否为空,不为空则将greatertailnext赋值为空,再将lesstailnext赋值为greatertailnext,将大小链表连接在一起
(5)创建retail赋值为lessheadnext,再将lesshead进行free置空,最后返回retail即可

typedef struct ListNode ListNode;
struct ListNode* partition(struct ListNode* head, int x)
{if(head == NULL){return head;}ListNode* lesshead=(ListNode*)malloc(sizeof(ListNode));ListNode* greaterhead=(ListNode*)malloc(sizeof(ListNode));ListNode* lesstail=lesshead;ListNode* greatertail=greaterhead;ListNode* pcur=head;while(pcur){if((pcur->val) < x){lesstail->next=pcur;lesstail=lesstail->next;pcur=pcur->next;}else{greatertail->next=pcur;greatertail=greatertail->next;pcur=pcur->next;}}if(greatertail)greatertail->next=NULL;lesstail->next=greaterhead->next;ListNode* retail=lesshead->next;free(lesshead);lesshead=NULL;return retail;
}

更多推荐

单链表的应用(2)

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

发布评论

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

>www.elefans.com

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