【数据结构】——【队列】算法代码实现(C/C++语言实现)(整理不易,请多关注)

这篇博客详细介绍了队列的两种常见表示方式——顺序队列和链式队列。首先,通过数据结构定义了顺序队列和链式队列的类型,并提供了相关操作函数的声明和定义,包括创建、判断队列状态、入队、出队和获取队头元素等。然后,展示了如何使用队列解决经典的农夫过河问题,通过算法实现确保安全状态的转移。整个博客深入浅出地阐述了队列这一基础数据结构的应用和实现细节。
摘要由CSDN通过智能技术生成

目录

1.队列的顺序表示:类型和函数声明

2.队列的顺序表示:函数定义

3.队列链接表示:类型和界面函数声明

4.队列链接表示:函数定义

5.用队列解决农夫过河问题的算法


1.队列的顺序表示:类型和函数声明

typedef int DataType;
#define MAXNUM 20  /* 队列中最大元素个数 */

struct  SeqQueue 
{       /* 顺序队列类型定义 */
    int f, r;
    DataType q[MAXNUM];
};

typedef struct SeqQueue SeqQueue, *PSeqQueue; /* 顺序队列类型和指针类型 */

/*创建一个空队列*/
PSeqQueue createEmptyQueue_seq( void );

/*判队列是否为空队列*/
int isEmptyQueue_seq( PSeqQueue paqu );

/* 在队列中插入一元素x */
void enQueue_seq( PSeqQueue paqu, DataType x );

/* 删除队列头部元素 */
void deQueue_seq( PSeqQueue paqu );

/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq( PSeqQueue paqu );

2.队列的顺序表示:函数定义


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

#include "squeue.h"

/*创建一个空队列*/
PSeqQueue  createEmptyQueue_seq( void ) 
{  
    PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
    if (paqu==NULL)
        printf("Out of space!! \n");
    else
        paqu->f = paqu->r = 0;
    return paqu;
}

/*判队列是否为空队列*/
int  isEmptyQueue_seq( PSeqQueue paqu ) 
{
    return paqu->f == paqu->r;
}

/* 在队列中插入一元素x */
void  enQueue_seq( PSeqQueue paqu, DataType x ) 
{
    if( (paqu->r + 1) % MAXNUM == paqu->f  )
        printf( "Full queue.\n" );
    else       {
        paqu->q[paqu->r] = x;
        paqu->r = (paqu->r + 1) % MAXNUM;
    }
}

/* 删除队列头部元素 */
void  deQueue_seq( PSeqQueue paqu ) 
{
    if( paqu->f == paqu->r )
        printf( "Empty Queue.\n" );
    else
        paqu->f = (paqu->f + 1) % MAXNUM;
}

/* 对非空队列,求队列头部元素 */
DataType  frontQueue_seq( PSeqQueue paqu ) 
{
    return paqu->q[paqu->f];
}

3.队列链接表示:类型和界面函数声明

typedef int DataType;
struct  Node;
typedef  struct Node *PNode;

struct  Node 
{              /* 结点结构 */
    DataType    info;
    PNode       link;
};

struct  LinkQueue 
{       /* 链接队列类型定义 */ 
    PNode  f;          /* 头指针 */
    PNode  r;          /* 尾指针 */
};

typedef struct LinkQueue *PLinkQueue;

/*创建一个空队列*/
PLinkQueue  createEmptyQueue_link( );

/*判断链接表示队列是否为空队列*/ 
int  isEmptyQueue_link( PLinkQueue plqu );

/*进队列*/
void  enQueue_link( PLinkQueue plqu, DataType x);

/*出队列*/
void  deQueue_link( PLinkQueue plqu );

/* 在非空队列中求队头元素 */
DataType  frontQueue_link( PLinkQueue plqu );

4.队列链接表示:函数定义


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

#include "lqueue.h"

/*创建一个空队列*/
PLinkQueue  createEmptyQueue_link( ) 
{       
    PLinkQueue plqu = (PLinkQueue )malloc(sizeof(struct LinkQueue));
    if (plqu != NULL)
        plqu->f = plqu->r = NULL;
    else
        printf("Out of space!! \n");
               
    return (plqu);
}

/*判断链接表示队列是否为空队列*/ 
int  isEmptyQueue_link( PLinkQueue plqu ) 
{
    return (plqu->f == NULL);
}


/*进队列*/
void  enQueue_link( PLinkQueue plqu, DataType x) 
{ 
    PNode p = (PNode )malloc( sizeof( struct Node ) );
    if ( p == NULL  )
        printf("Out of space!");
    else { 
        p->info = x;
        p->link = NULL;
        if (plqu->f == NULL)
            plqu->f = p;
        else
            plqu->r->link = p;

        plqu->r = p;
    }
}

/*出队列*/
void  deQueue_link( PLinkQueue plqu ) 
{ 
    PNode   p;
    if( plqu->f == NULL )
        printf( "Empty queue.\n " );
    else { 
        p = plqu->f;
        plqu->f = plqu->f->link;
        free(p);
    }
}

/* 在非空队列中求队头元素 */
DataType  frontQueue_link( PLinkQueue plqu ) 
{ 
    return (plqu->f->info);
}

5.用队列解决农夫过河问题的算法


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

#define  MAXNUM   20

typedef int DataType;
struct  SeqQueue 
{              /* 顺序队列类型定义 */
    int  f, r;
    DataType q[MAXNUM];
};

typedef struct SeqQueue *PSeqQueue;   /* 顺序队列类型的指针类型 */

PSeqQueue createEmptyQueue_seq( void ) 
{  
    PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
    if (paqu == NULL)
        printf("Out of space!! \n");
    else
        paqu->f = paqu->r = 0;
    return (paqu);
}

int isEmptyQueue_seq( PSeqQueue paqu ) 
{
    return paqu->f == paqu->r;
}

/* 在队列中插入一元素x */
void  enQueue_seq( PSeqQueue paqu, DataType x ) 
{
    if ( (paqu->r + 1) % MAXNUM == paqu->f  )
        printf( "Full queue.\n" );
    else {
        paqu->q[paqu->r] = x;
        paqu->r = (paqu->r + 1) % MAXNUM;
    }
}

/* 删除队列头部元素 */
void  deQueue_seq( PSeqQueue paqu ) 
{
    if( paqu->f == paqu->r )
        printf( "Empty Queue.\n" );
    else
        paqu->f = (paqu->f + 1) % MAXNUM;
}

/* 对非空队列,求队列头部元素 */
DataType  frontQueue_seq( PSeqQueue paqu ) 
{
    return (paqu->q[paqu->f]);
}

int farmer(int location) 
{
    return 0 != (location & 0x08);
}

int wolf(int location) 
{
    return 0 != (location & 0x04);
}

int cabbage(int location) 
{
    return 0 != (location & 0x02);
}

int goat(int location) 
{
    return 0 !=(location & 0x01);
}

/* 若状态安全则返回true */
int safe(int location) 
{
    /* 羊吃白菜 */
    if ((goat(location) == cabbage(location)) &&  
          (goat(location) != farmer(location)) )  
        return 0;
    /* 狼吃羊 */
    if ((goat(location) == wolf(location)) &&  
          (goat(location) != farmer(location)))  
        return 0;
    return 1;   /* 其他状态是安全的 */
}


void farmerProblem( ) 
{
    int movers, i, location, newlocation;
    int route[16];        /*记录已考虑的状态路径*/
    PSeqQueue moveTo; 
    /*准备初值*/
    moveTo = createEmptyQueue_seq( );
    enQueue_seq(moveTo, 0x00);
    for (i = 0; i < 16; i++) route[i] = -1;
    route[0]=0;
    
    /*开始移动*/
while (!isEmptyQueue_seq(moveTo)&&(route[15] == -1)) 
{
        /*得到现在的状态*/
        location = frontQueue_seq(moveTo);
        deQueue_seq(moveTo);
        for (movers = 1; movers <= 8; movers <<= 1) 
{
            /* 农夫总是在移动,随农夫移动的也只能是在农夫同侧的东西 */
            if ((0 != (location & 0x08)) == (0 != (location & movers))) {
                newlocation = location^(0x08|movers);
                if (safe(newlocation) && (route[newlocation] == -1)) 
{ 
                    route[newlocation] = location;
                    enQueue_seq(moveTo, newlocation);
                }
            }
        }
    }
    
    /* 打印出路径 */
if(route[15] != -1) 
{
        printf("The reverse path is : \n");
        for(location = 15; location >= 0; location = route[location]) 
{ 
            printf("The location is : %d\n",location);
            if (location == 0) return;
        }
    }
    else 
        printf("No solution.\n");
}


int main() 
{
    farmerProblem( );
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值