数据结构day3

循环列表实现约瑟夫杯

主函数(main.c文件)

#include<stdio.h>
#include<stdlib.h>
#include"a.h"
int main(int argc,const char *argv[])
{
        Looplink *L=link_create();
        if(NULL==L)
        {
                return -1;
        }
        int i=1;
        int j=0,n,m;
        printf("m=");
        scanf(" %d",&m);
        printf("n=");
        scanf(" %d",&n);
        while(i<=n)
        {
                list_tail_insert(L,i);
                i++;
        }
        list_traver(L);
        Looplink *p=L->next;
        Looplink *S=link_create();
        Looplink *q=kill_head(L);
        printf("约瑟夫杯:");
        while(j<n)
        {
                for(i=0;i<m;i++)
                {
                    if(i==m-1)
                        {
                                //1.
                                //printf("%d ",p->data);
                                //2.
                                list_tail_insert(S,p->data);
                                p=kill_head(p);
                        }
                        else
                        {
                                p=p->next;
                        }
                }
                j++;
        }
        list_traver(S);
}


两种表现方式,注释1为输出显示约瑟夫杯,注释2的地方为将寻找的数从新放进一个新的循环链表中,再利用链表的遍历输出。

.h函数部分(主要使用的部分)

删除头节点函数(主要利用函数)

Looplink *kill_head(Looplink *L)
{
        if(NULL==L || list_empty(L))
        {
                printf("delate failed!\n");
                return NULL;
        }
        Looplink *q=L->next;
        while(q->next!=L)
        {
                q=q->next;
        }
        q->next=L->next;
        free(L);
        L=NULL;
        printf("head delate successful!\n");
        return q->next;
}

创建函数

Looplink *link_create()
{
        Looplink *L=(Looplink*)malloc(sizeof(Looplink));
        if(NULL == L)
        {
                printf("create failed!\n");
                return NULL;
        }
        L->len=0;
        L->next=L;
        printf("create successful!\n");
        return L;
}

尾插函数

//tail insert
int list_tail_insert(Looplink *L,datatype e)
{
        if(NULL == L)
        {
                printf("link is wrong!\n");
                return -1;
        }
        Looplink *p=(Looplink*)malloc(sizeof(Looplink));
        if(NULL==p)
        {
                printf("node apply failed!\n");
                return -2;
        }
        p->data=e;
        p->next=NULL;
        Looplink *q=L;
        while(q->next!=L)
        {
                q=q->next;
        }
        p->next=L;
        q->next=p;
        L->len++;
//      printf("insert successful!\n");
        return 0;
}

遍历函数

//traverse
void list_traver(Looplink *L)
{
        if(NULL == L || list_empty(L))
        {
                printf("traverse failed!\n");
                return;
        }
        Looplink *q=L->next;
        while(q!=L)
        {
                printf("%d ",q->data);
                q=q->next;
        }
        printf("\n");
}

无头遍历

​
//no head traverse
void list_show(Looplink *S)
{
        if(NULL==S)
        {
                printf("traverse failed!\n");
                return ;
        }
        Looplink *q=S;
        do
        {
                printf("%d ",q->data);
                q=q->next;
        }while(q!=S);
        printf("\n");
}

​

头删函数

//head delate
Looplink *kill_head(Looplink *L)
{
        if(NULL==L || list_empty(L))
        {
                printf("delate failed!\n");
                return NULL;
        }
        Looplink *q=L->next;
        while(q->next!=L)
        {
                q=q->next;
        }
        q->next=L->next;
        free(L);
        L=NULL;
        printf("head delate successful!\n");
        return q->next;
}

 尾删函数

//tail delate
int list_tail_delate(Looplink *L)
{
        if(NULL == L || list_empty(L))
        {
                printf("tail delate failed!\n");
                return -1;
        }
        Looplink *q=L;
        while(q->next->next!=L)
        {
                q=q->next;
        }
        free(q->next);
        q->next=L;
        L->len--;
        printf("delate successful!\n");
        return 1;
}

.h文件

#ifndef __D__
#define __D__

typedef int datatype;
typedef struct Node
{
        union
        {
                datatype data;
                int len;
        };

        struct Node *next;
}Looplink;

//create
Looplink *link_create();
//empty
int list_empty(Looplink *L);
//tail insert
int list_tail_insert(Looplink *L,datatype e);
//traverse
void list_traver(Looplink *L);
//tail delate
int list_tail_delate(Looplink *L);
//head delate
Looplink *kill_head(Looplink *L);
//no head traverse
void list_show(Looplink *L);
#endif
         

利用栈实现进制转换

主函数(main.c文件)

#include<stdio.h>
#include<stdlib.h>
#include"a.h"
int main(int argc,const char *argv[])
{
        SqStack *S=stack_create();
        if(NULL==S)
        {
                return -1;
        }
        int i,j,k;
        printf("1.二进制转换\n2.八进制转换\n3.十六进制转换\n请选择操作方式:\n");
        scanf("%d",&k);
        switch(k)
        {
                case 1: printf("请输入十进制数:");
                        scanf("%d",&i);
                        printf("转换为二进制数为:");
                        while(i>0)
                        {
                                j=i%2;
                                Push(S,j);
                                i=i/2;
                        }
                        show(S);
                        break;
                case 2: printf("请输入十进制数:");
                        scanf("%d",&i);
                        printf("转换为八进制数为:");
                        while(i>0)
                        {
                                j=i%8;
                                Push(S,j);
                                i=i/8;
                        }
                        show(S);
                        break;
                case 3: printf("请输入十进制数:");
                        scanf("%d",&i);
                        printf("转换为十六进制数为:");
                        while(i>0)
                        {
                                j=i%16;
                                Push(S,j);
                                i=i/16;
                        }
                        show(S);
                        break;
                }
                return 0;
}

在进行十六进制转换的时候,10-15对应A-F,所有在遍历输出的时候需要做一些调整,调整方式如下

int i=S->top;
while(i>=0)
{
    if(S->data[i]>9)
    {
        printf("%c ",'A'+S->data[i]-10);
    }
    else
    {
        printf("%d ",S->data[i]);
    }
    i--;
}

当S->data的值等于10,就输出字符‘A’,S->data的值每增加1,就让字符'A'加上S->data的增值(也就是S->data-10),利用字符串以及askii码的规律使其增加,从而输出十六进制对应的字母。

.c文件

#include<stdio.h>
#include<stdlib.h>
#include"a.h"

SqStack *stack_create()
{
        SqStack *S=(SqStack*)malloc(sizeof(SqStack));
        if(NULL == S)
        {
                printf("创建失败!\n");
                return NULL;
        }
        S->top=-1;
        printf("创建成功!\n");
        return S;
}
//判满
int Full(SqStack *S)
{
        if(S->top == MAX-1)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}
//判空
int Empty(SqStack *S)
{
        if(S->top==-1)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}
//进栈
int Push(SqStack *S,datatype e)
{
        if(Full(S))
        {
                printf("栈满!\n");
                return -1;
        }
        else
        {
                S->top++;
                S->data[S->top]=e;
        //      printf("入栈成功! \n");
                return 0;
        }
}
//出栈
int Pop(SqStack *S)
{
        if(NULL == S || Empty(S))
        {
                printf("出栈失败!\n");
                return -1;
        }
        printf("%d ",S->data[S->top]);
        S->top--;
        return 0;
}
//遍历
void show(SqStack *S)
{
        if(NULL == S || Empty(S))
        {
                printf("遍历失败!\n");
                return;
        }
        int i=S->top;
        while(i>=0)
        {
                if(S->data[i]>9)
                {
                        printf("%c ",'A'+S->data[i]-10);
                }
                else
                {
                        printf("%d ",S->data[i]);
                }
                i--;
        }
        printf("\n");
}
//销毁
void destroy(SqStack *S)
{
    if(NULL==S)
    {
        printf("释放失败\n");
    return;
    }
    free(S);
    S = NULL;
    printf("释放成功\n");
}

.h文件

#ifndef __E__
#define __E__

#define MAX 10

typedef int datatype;

typedef struct
{
        datatype data[MAX];
        int top;
}SqStack;

//create
SqStack *stack_create();
//empty
int Empty(SqStack *S);
//full
int Full(SqStack *S);
//push
int Push(SqStack *S,datatype e);
//pop
int Pop(SqStack *S);
//show
void show(SqStack *S);
//destroy
void destroy(SqStack *S);

#endif
~       

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值