循环链表解决约瑟夫环问题:
#ifndef D64_H
#define D64_H
#include <myhead.h>
extern int count;
typedef struct Node
{
int num;
struct Node * prio;
struct Node * next;
}Node, *NodePtr;
//链表的创建
NodePtr list_create();
//约瑟夫问题
void game(NodePtr L, int n, int m);
//封装数据
NodePtr apply_node(int e);
//尾插函数
int list_insert(NodePtr L, int e);
//删除函数
int list_delete(NodePtr L, NodePtr p);
//销毁函数
void list_destroy(NodePtr *L);
#endif
#include "d64.h"
extern int count;
//链表的创建
NodePtr list_create()
{
NodePtr L = (NodePtr)malloc(sizeof(Node));
if(NULL == L)
{
printf("创建失败\n");
return NULL;
}
L->num = 1;
L->next = L;
L->prio = L;
count=1;
printf("创建成功\n");
return L;
}
//封装数据
NodePtr apply_node(int e)
{
NodePtr p = (NodePtr)malloc(sizeof(Node));
if(NULL == p)
{
printf("申请失败\n");
return NULL;
}
p->num = e;
p->next =NULL;
p->prio = NULL;
return p;
}
//尾插函数
int list_insert(NodePtr L, int e)
{
if(NULL == L)
{
printf("插入失败\n");
return -1;
}
NodePtr p = apply_node(e);
p->next = L;
p->prio = L->prio;
L->prio->next = p;
L->prio = p;
count++;
printf("插入成功\n");
return 0;
}
//约瑟夫问题
void game(NodePtr L, int n, int m)
{
int kill_num = 0;
for (int i = 2; i <= n; i++)
{
list_insert(L, i);
}
NodePtr p = L;
while(count != 1)
{
if(++kill_num == m)
{
printf("%d死了\n",p->num);
list_delete(L, p);
kill_num = 0;
}
p = p->next;
}
printf("%d还活着\n",L->num);
}
//删除函数
int list_delete(NodePtr L, NodePtr p)
{
if(NULL == L || count == 1)
{
printf("删除失败\n");
return -1;
}
if(p == L)
{
L = p->next;
}
p->next->prio = p->prio;
p->prio->next = p->next;
free(p);
p = NULL;
count--;
return 0;
}
//销毁函数
void list_destroy(NodePtr *L)
{
if(NULL == *L)
{
printf("销毁失败\n");
return;
}
while((*L)->next != *L)
{
list_delete(*L,(*L)->next);
}
free(*L);
*L == NULL;
printf("销毁成功\n");
}
#include "d64.h"
int count = 0;
int main(int argc, char const *argv[])
{
NodePtr L = list_create();
if (NULL == L)
{
return -1;
}
int m = 0, n = 0;
printf(">>>");
scanf("%d %d",&m,&n);
game(L, m, n);
list_destroy(&L);
return 0;
}
顺序栈实现进制转换:
#ifndef D65_H
#define D65_H
#include <myhead.h>
#define MAX 20
typedef struct SeqStqck
{
int *data;
int top;
}Stack, *StackPtr;
//创建栈
StackPtr stack_create();
//进制转换函数
void DtoBOH(StackPtr S, int D, int BOH);
//判空
int stack_empty(StackPtr S);
//判满
int stack_full(StackPtr S);
//入栈
void stack_push(StackPtr S, int e);
//输出转换后的结果
void stack_pop(StackPtr S,int BOH);
//销毁栈
void stack_destroy(StackPtr *S);
#endif
#include "d65.h"
//创建栈
StackPtr stack_create()
{
StackPtr S = (StackPtr)malloc(sizeof(Stack));
if(NULL == S)
{
printf("创建失败\n");
return NULL;
}
S->data = (int *)malloc(sizeof(int)*MAX);
if(NULL == S->data)
{
printf("创建失败\n");
return NULL;
}
memset(S->data, 0, sizeof(int)*MAX);
S->top = -1;
printf("创建成功\n");
return S;
}
//进制转换函数
void DtoBOH(StackPtr S, int D, int BOH)
{
int num =0;
while(D != 0)
{
num = D%BOH;
D = D/BOH;
stack_push(S,num);
}
stack_pop(S, BOH);
}
//判空
int stack_empty(StackPtr S)
{
return S->top == -1;
}
//判满
int stack_full(StackPtr S)
{
return S->top == MAX-1;
}
//入栈
void stack_push(StackPtr S, int e)
{
if(NULL == S || stack_full(S))
{
printf("入栈失败\n");
return;
}
S->top++;
S->data[S->top] = e;
}
//输出转换后的结果
void stack_pop(StackPtr S, int BOH)
{
if(NULL == S || stack_empty(S))
{
printf("输出失败\n");
return;
}
if(BOH == 16)
{
printf("0x");
while(S->top != -1)
{
printf("%x",S->data[S->top--]);
}
}
else if(BOH == 8)
{
printf("0O");
while(S->top != -1)
{
printf("%d",S->data[S->top--]);
}
}
else
{
printf("0B");
while(S->top != -1)
{
printf("%d",S->data[S->top--]);
}
}
printf("\n");
}
//销毁栈
void stack_destroy(StackPtr *S)
{
if(NULL == *S)
{
printf("销毁失败\n");
return;
}
free((*S)->data);
free(*S);
*S=NULL;
printf("销毁成功\n");
}
#include "d65.h"
int main(int argc, char const *argv[])
{
StackPtr S = stack_create();
if (NULL == S)
{
return -1;
}
int D=0, BOH=0;
printf("请输入一个整数和要转换成的进制:");
scanf("%d %d",&D,&BOH);
DtoBOH(S, D, BOH);
stack_destroy(&S);
return 0;
}
思维导图: