数据结构-循环链表,约瑟夫环

一、循环链表

#include "stdio.h"
#include "stdlib.h"

typedef struct node
{
	int data;
	struct node *next;
}node;

初始化循环链表

void ds_init(node **pNode)  // 初始化循环链表, **pNode:链表上
{
	int item;
	node *temp;
	node *target;
	printf("输入结点的值, 输入0完成初始化\n");
	while(1)
	{
		scanf("%d", &item);
		fflush(stdin);
		if(item == 0)
			return;
		if((*pNode) == NULL){
			// 循环链表中只有一个结点
			*pNode = (node*)malloc(sizeof(struct CLinkList));  // 分配空间
			if(!(*pNode))
				exit(0);  // 错误退出
			(*pNode)->data = item;
			(*pNode)->next = *pNode;
		}
		else{
			// 找到next指向第一个结点的结点
			for(target = (*pNode); target->next != (*pNode); target = target->next);
			// 生成一个新的结点
			temp = (node*)malloc(sizeof(struct CLinkList));
		}
	}
}

返回结点所在位置

int ds_search(node *pNode, int elem)
{
	node *target;
	int i = 1;
	for(target = pNode; target->data != elem && target->next != pNode; ++i)
	{
		target = target->next;
	}
	if(target->next == pNode)  // 表中不存在改元素
		return 0;
	else
		return i;
}

/**********************************
功能:插入结点
参数:链表的第一个位置,插入的位置
**********************************/

void ds_insert(node **pNode, int i)
{
	node *temp;
	node *target;
	node *p;
	int item;
	int j = 1;
	
	printf("输入要插入的结点值:");
	scanf("%d", &item);
	if(i == 1){
		// 新增结点作为你第一个结点
		temp = (node *)malloc(sizeof(struct CLinkList));
		if(!temp)
			exit(0);
		temp->data = item;
		// 寻找到最后一个结点
		for(target = 0; target->next != (*pNode); target = target->next);   // 寻找到最后一个结点
		temp->next = (*pNode);  // 插入结点的指针指向第一个结点的
		target->next = temp;    // 最后一个结点指针指向新插入的
		*pNode = temp;          // 
	}
	else{
		target = *pNode;
		for(; j < (i - 1); ++j)
		{
			target = target->next;
		}
		// 若i = 3  则target指向第三个元素
		temp = (node *)malloc(sizeof(struct CLinkList));
		if(!temp)
			exit(0);
		temp->data = item;
		p = target->next;
		target->next = temp;
		temp->next = p;
	}
}

删除结点

void ds_delete(node **pNode, int i)
{
	node *target;
	node *temp;
	int j = 1;
	if(i == 1){  // 删除的是第一个结点 
		for(target = *pNode; target->next != *pNode; target = target->next);   // 寻找到最后一个结点
		temp = *pNode;
		*pNode = (*pNode)->next;
		target->next = *pNode;
		free(temp);
	}
	else{
		target = *pNode;
		for(; j < (i - 1); ++j)
		{
			target = target->next;  // 找到删除的结点
		}
		temp = target->next;
		target->next = temp->next;
		free(temp);
	}
}

二、约瑟夫环

在这里插入图片描述

#include "stdio.h"
#include "stdlib.h"

typedef struct node
{
	int data;
	struct node *next;
}node;


node *create(int n)
{
	node *p = NULL, *head;
	head = (node*)malloc(sizeof (node));
	p = head;
	node *s;
	int i = 1;
	if(0 != n){
		while(i <= n){ 
			s = (node *)malloc(sizeof (node));
			s->data = i++;  // 先赋值,再自增
			p->next = s;
			p = s;
		}
		s->next = head->next;
	}
	free(head);  // 去掉头结点构成环
	return s->next;
}


int main()
{
	int n = 41;
	int m = 3;
	int i;
	node *p = create(n);
	node *temp;
	m %= n;  // m = 2
	while(p != p->next){   // 若指向头,头为空退出循环
		for(i = 1; i < m - 1; i++)  // 执行一次
		{
			p = p->next;  // 第一个结点指针指向第二个,p 为第二个结点
		}
		printf("%d->", p->next->data);  // 打印第三个结点的数据
		temp = p->next;  // 将第三个结点暂时赋给temp
		p->next = temp->next;  // 第二个结点指针指向第四个结点
		free(temp);  // 释放第三个结点
		p = p->next;  // p赋值为第四个结点
	}
	printf("%d\n", p->data);
	return 0;
}

三、问题

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环链表是一种特殊的链表,它的最后一个节点指向第一个节点,形成一个环。使用循环链表可以很方便地实现约瑟夫环问题。 具体实现步骤如下: 1. 定义一个节点结构体,包含一个数据域和一个指向下一个节点的指针域。 2. 构建循环链表,将每个节点连接起来形成一个环。 3. 定义一个计数器,从指定位置开始遍历链表,每遍历到一个节点就将计数器加1,当计数器的值等于指定的数n时,将该节点从链表中删除。 4. 重复步骤3,直到链表中只剩下一个节点为止。 下面是一个C语言实现的例子: ```c #include <stdio.h> #include <stdlib.h> // 定义节点结构体 typedef struct node { int data; struct node *next; } Node; // 构建循环链表 Node *buildList(int n) { Node *head = NULL, *tail = NULL; for (int i = 1; i <= n; i++) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = i; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } tail->next = head; // 将最后一个节点指向头节点,形成循环链表 return head; } // 删除节点 Node *deleteNode(Node *head, int n, int m) { Node *p = head, *pre = NULL; int count = 0; while (p->next != p) { // 当链表中只剩下一个节点时结束循环 count++; if (count == m) { // 找到要删除的节点 printf("%d ", p->data); // 输出该节点的值 pre->next = p->next; // 将该节点从链表中删除 Node *temp = p; p = p->next; free(temp); count = 0; } else { pre = p; p = p->next; } } printf("%d\n", p->data); // 输出最后剩下的节点的值 free(p); return NULL; } int main() { int n, m; printf("请输入总人数n和报数m:"); scanf("%d %d", &n, &m); Node *head = buildList(n); printf("出列顺序为:"); head = deleteNode(head, n, m); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值