双向链表及约瑟夫环代码
来源:互联网 发布:c语言波兰表达式 编辑:程序博客网 时间:2024/04/29 05:49
双向链表
//注意:该文件操作的链表为带头结点双向链表,头结点数据为-1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OK 1
#define ERROR 0
typedef int data_t;
typedef struct DulNode
{
data_t data;
struct DulNode *prior;
struct DulNode *next;
}DulNode,*DuLinkList;
int GetElem(DuLinkList L,int i,data_t *data)//读取双向链表的第i个元素
{
int j;
DuLinkList p;
p = L;
j = 1;
while(p && j<i)
{
p = p->next;//让p指向下一个节点
j++;
}
if(!p)
{
printf("第%d个位置不存在!\n",i);
return ERROR;
}
*data = p->data;
return OK;
}
int ListInsert(DuLinkList L,int i,data_t e)
{
int j;
DuLinkList p,s;
p=L;
j=1;
while(p && j<i)
{
p=p->next;
j++;
}
if(!p)
{
printf("第%d个位置不存在!\n",i);
return ERROR;
}
s=(DuLinkList)malloc(sizeof(DulNode));
s->data=e;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
return OK;
}
int ListDelete(DuLinkList L,int i,data_t *e)
{
int j;
DuLinkList p;
p=L;
j=1;
while(p->next && j<=i)
{
p=p->next;
j++;
}
if(!(p->next))
{
printf("第%d个位置不存在!\n",i);
return ERROR;
}
*e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
p=NULL;
return OK;
}
DuLinkList CreateEmptyList()
{
DuLinkList p;
p = (DuLinkList)malloc(sizeof(DulNode));
if(p==NULL)
{
perror("CreateEmptyList error");
exit(0);
}
p->prior=NULL;
p->next=NULL;
p->data=-1;
return p;
}
DuLinkList CreateList(DuLinkList L,int n)
{
DuLinkList p,r;
int i;
srand(time(NULL));
r = L;
for(i=0;i<n;i++)
{
p = (DuLinkList)malloc(sizeof(DulNode));
p->data = rand()%100+1;
r->next = p;
p->prior = r;
r = p;
}
r->next = NULL;
return L;
}
int ClearList(DuLinkList L)
{
DuLinkList p,q;
p=L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next=NULL;
return OK;
}
int PrintList(DuLinkList L)
{
DuLinkList p=L;
while(p)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
return OK;
}
int main()
{
/*
DuLinkList head;
head = CreateEmptyList();
head = CreateList(head,15);
int i=5;
data_t data=12;
PrintList(head);
printf("插入head第%d个位置,数据为%d\n",i,data);
ListInsert(head,i,data);
printf("插入后链表为:\n");
PrintList(head);
i=8;
ListDelete(head,i,&data);
printf("删除head第%d个位置,删除数据为%d\n",i,data);
printf("删除后链表为:\n");
PrintList(head);
if(ClearList(head)==OK)
{
printf("head已被成功删除!\n");
}
*/
return 0;
}
约瑟夫环代码
#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
/* use a cycle linked list without header node */
typedef struct node_t
{
data_t data;
struct node_t *next;
} linknode_t, *linklist_t;
int main()
{
int i, n, m, k;
linklist_t p, q;
printf("total N people will join this suicide game, please input N:");
scanf("%d", &n);
printf( "people stand in a circle, assume everyone is assigned\n"
"a sequence number from 1 to %d.\n"\
"which one would you like to start the number off (1~%d):", n, n);
scanf("%d", &k);
printf("every Xth people should kill himself, please decide the X:");
scanf("%d", &m);
if (n < 1 || k > n || m < 1) {
printf("input error!\n");
return -1;
}
printf("game is starting ...\n");
/* added the first one */
p = q = (linklist_t)malloc(sizeof(linknode_t));
p->data = 1;
/* added other left people */
for (i = 2; i <= n; i++) {
q->next = (linklist_t)malloc(sizeof(linknode_t));
q = q->next;
q->data = i;
}
/* complete the circle */
q->next = p;
/* find the people ahead of #k */
q = p;
while (q->next != p) {
if (q->next->data == k)
break;
q = q->next;
}
while (q->next != q) { /* till the last one */
/* every m people */
for (i = 0; i < m - 1; i++)
{
q = q->next;
}
/* kill the m-th people */
p = q->next;
q->next = p->next;
printf("%-2d was killed\n", p->data);
free(p);
}
/* kill the last one */
printf("%-2d was alive\n", q->data);
free(q);
return 0;
}
- 双向链表及约瑟夫环代码
- 约瑟夫双向链表
- 约瑟夫环(1)-双向链表
- 双向链表实现约瑟夫环
- 循环双向链表 及 约瑟夫问题的解决
- SzNOI 双向约瑟夫(双向链表)
- 约瑟夫环问题(双向循环链表模拟)
- 双向循环链表-解决顺逆时针约瑟夫环
- 双向链表实现约瑟夫环 2016/6/12
- 利用双向链表实现约瑟夫问题
- 约瑟夫问题(双向循环链表)
- 约瑟夫问题---list双向链表
- 约瑟夫问题,双向链表实现
- UVa 133 双向约瑟夫环
- 双向链表 代码
- 双向链表基本操作及代码优化技巧
- josephus约瑟夫环变种 双向链表实现 特点是游戏规则在动态改变
- C++模板库中std::list(双向链表)兼约瑟夫环问题
- JDBC
- MIPS R10000 超标量微处理器(一)
- LeetCode之Two Sum
- 字符串逆序输出
- 面试中常问的关于链表的题目
- 双向链表及约瑟夫环代码
- 二叉树构建以及前中后序遍历(递归和非递归)Java实现
- 读Spring(1)
- log4j使用详解
- C++作业三
- 虚拟机下给新添加的硬盘挂载
- 回溯法_n皇后问题
- Johnsen-lindenstrauss定理(J-L定理)
- URL重写及Session