题目:
题目一:用循环列表实现约瑟夫环
题目二:十进制转换成各种进制
代码:
题目一代码:
looplink.c
#include"looplink.h"
//创建
LoopLink *list_create()
{
LoopLink *L = (LoopLink *)malloc(sizeof(LoopLink));
if(NULL==L)
{
printf("创建失败\n");
return NULL;
}
//初始化
L->len = 0;
L->next = L; //将指针域指向自己
printf("创建成功\n");
return L;
}
//判空
int list_empty(LoopLink *L)
{
return L == L->next;
}
//头插
int list_insert_head(LoopLink *L, datatype e)
{
if(L==NULL)
{
printf("所给链表不合法\n");
return 0;
}
LoopLink *p=(LoopLink*)malloc(sizeof(LoopLink));
if(p==NULL)
{
printf("节点申请失败\n");
return 0;
}
p->data=e;
p->next=NULL;
p->next=L->next;
L->next=p;
L->len++;
printf("插入成功\n");
return 1;
}
//遍历
void list_show(LoopLink *L)
{
if(NULL==L||list_empty(L))
{
printf("遍历失败\n");
return ;
}
LoopLink *p=L->next;
while(p!=L)
{
printf("%-4d",p->data);
p=p->next;
}
printf("\n");
}
//头删
int list_delete_head(LoopLink *L)
{
if(NULL==L||list_empty(L))
{
printf("删除失败\n");
return 0;
}
LoopLink *p=L->next;
L->next=p->next;
free(p);
p=NULL;
L->len--;
printf("删除成功\n");
return 1;
}
//删除头结点
LoopLink *list_kill_head(LoopLink *L)
{
if(NULL==L||list_empty(L))
{
printf("去头失败\n");
return 0;
}
LoopLink *p=L;
while(p->next!=L) p=p->next;
p->next=L->next;
free(L);
L=NULL;
printf("去头成功\n");
return p->next;
}
//删除头结点后的遍历
void list_show2(LoopLink *H)
{
if(NULL==H)
{
printf("遍历失败\n");
return;
}
LoopLink *p=H;
do
{
printf("%-4d",p->data);
p=p->next;
}while(p!=H);
printf("\n");
}
void ysf(LoopLink *L,int m)
{
int n,n1;
if(NULL==L||m<1)
{
printf("失败\n");
return;
}
n=L->len;
n1=L->len;
LoopLink *H=list_kill_head(L);
while(n)
{
for(int i=0;i<(m%n1-1)+(n-1);i++) H=H->next;
printf("%-3d",H->next->data);
LoopLink *p=H->next;
H->next=p->next;
free(p);
p=NULL;
H=H->next;
n--;
}
printf("\n");
}
looplink.h
#ifndef __LOOPLINK_H__
#define __LOOPLINK_H__
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct Node
{
union{
int len; //头结点数据域
datatype data; //普通节点数据域
};
struct Node *next; //指针域
}LoopLink;
//创建
LoopLink *list_create();
//判空
int list_empty(LoopLink *L);
//头插
int list_insert_head(LoopLink *L, datatype e);
//遍历
void list_show(LoopLink *L);
//头删
int list_delete_head(LoopLink *L);
//删除头结点
LoopLink *list_kill_head(LoopLink *L);
//删除头结点后的遍历
void list_show2(LoopLink *H);
void ysf(LoopLink *L,int m);
#endif
main.c
#include<stdio.h>
#include"looplink.h"
int main(int argc, const char *argv[])
{
LoopLink *L=list_create();
if(NULL==L) return-1;
list_insert_head(L,8);
list_insert_head(L,7);
list_insert_head(L,6);
list_insert_head(L,5);
list_insert_head(L,4);
list_insert_head(L,3);
list_insert_head(L,2);
list_insert_head(L,1);
list_show(L);
// LoopLink *H=list_kill_head(L)
// list_show2(H);
ysf(L,4);
return 0;
}
题目二代码:
seqstack.c
#include"seqstack.h"
//创建
seqStack *stack_create()
{
seqStack *p=(seqStack*)malloc(sizeof(seqStack));
if(p==NULL)
{
printf("创建失败\n");
return NULL;
}
p->top=-1;
printf("创建成功\n");
return p;
}
//判空
int stack_empty(seqStack *S)
{
return S->top==-1;
}
//判满
int stack_full(seqStack *S)
{
return S->top==MAX-1;
}
//入栈
void stack_push(seqStack *S, datatype e)
{
if(S==NULL||stack_full(S))
{
printf("入栈失败\n");
return;
}
S->top++;
S->data[S->top]=e;
printf("入栈成功\n");
}
//遍历
void stack_show(seqStack *S)
{
if(S==NULL||stack_empty(S))
{
printf("遍历失败\n");
return;
}
for(int i=S->top;i>=0;i--)printf("%-4d",S->data[i]);
printf("\n");
}
//出栈
void stack_pop(seqStack *S)
{
if(S==NULL||stack_empty(S))
{
printf("出栈失败\n");
return;
}
printf("%d出栈成功\n",S->data[S->top]);
S->top--;
}
//栈的容量
int stack_size(seqStack *S)
{
return S->top+1;
}
//返回栈顶元素
datatype stack_top(seqStack *S)
{
if(S==NULL||stack_empty(S))
{
printf("获取失败\n");
return;
}
return S->data[S->top];
}
void DOC_change(seqStack *S,int n,int m)
{
int i=0;
if(S==NULL||stack_full(S))
{
printf("失败\n");
return;
}
while(n/m)
{
stack_push(S,n%m);
n=n/m;
i++;
}
stack_push(S,n);
i++;
for(int j=0;j<i;j++)stack_pop(S);
}
seqstack.h
#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef int datatype;
typedef struct
{
datatype data[MAX]; //存储栈的数组
int top; //记录栈顶元素所在的下标
}seqStack;
//创建
seqStack *stack_create();
//判空
int stack_empty(seqStack *S);
//判满
int stack_full(seqStack *S);
//入栈
void stack_push(seqStack *S, datatype e);
//遍历
void stack_show(seqStack *S);
//出栈
void stack_pop(seqStack *S);
//栈的容量
int stack_size(seqStack *S);
//返回栈顶元素
datatype stack_top(seqStack *S);
void DOC_change(seqStack *S,int n,int m);
#endif
main.c
#include<stdio.h>
#include"seqstack.h"
int main(int argc, const char *argv[])
{
seqStack *S=stack_create();
DOC_change(S,75,8);
return 0;
}
运行结果:
结果一:
ubuntu@ubuntu:11.29$ ./a.out
创建成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
插入成功
1 2 3 4 5 6 7 8
去头成功
4 8 5 2 1 3 7 6
结果二:
ubuntu@ubuntu:seqstack$ ./a.out
创建成功
入栈成功
入栈成功
入栈成功
1出栈成功
1出栈成功
3出栈成功