队列的一些基本算法

 

 

 

队列的一些基本算法

队列的一些基本算法

"a.h"

#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"

#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1

typedef int ElemType;
typedef int Status;

typedef struct QNode{
     ElemType data;
  struct QNode *next;
}QNode ,*QueuePtr;

typedef struct {
      QueuePtr front ,rear;
}LinkQueue;

"b.h"

#include"b.c"
Status InitQueue(LinkQueue *q);

Status DestroyQueue(LinkQueue *q);

Status ClearQueue(LinkQueue *q);

Status QueueEmpty(LinkQueue q);

Status QueueLength(LinkQueue q);

Status GetHead(LinkQueue q,ElemType *e);
//取队头元素

Status InsertQueue(LinkQueue *q,ElemType e);
//添加元素,从队尾插入

Status DeleteQueue(LinkQueue *q,ElemType *e);
//删除元素,从队头删除

Status QueueTraverse(LinkQueue q,Status(*fun)(ElemType));

"b.c"

#include"a.h"


Status InitQueue(LinkQueue *q)
{
    (*q).front=(QNode*)malloc(sizeof(QNode));
    if(!(*q).front)
     return ERROR;
    (*q).rear=(*q).front;
    (*q).front->next=NULL;
       return TRUE;
}

Status DestroyQueue(LinkQueue *q)
{
 while((*q).front){
     (*q).rear=(*q).front->next;
  free((*q).front);
  (*q).front=(*q).rear;
 }
 return OK;
}

Status ClearQueue(LinkQueue *q)
{
   QueuePtr p=(*q).front->next;
      while(p){
        (*q).rear=p->next;
     free(p);
     p=(*q).rear;
   }
   (*q).rear=(*q).front=NULL;
   return OK;

}

Status QueueEmpty(LinkQueue q)
{
      if(q.front!=q.rear)
    return FALSE;
   else
    return TRUE;
}

int QueueLength(LinkQueue q)
{
    int i=0;
       QueuePtr p=q.front;
    while(p){
      p=p->next;
      i++;
       
    }
    return i;
}

Status GetHead(LinkQueue q,ElemType *e)
//取队头元素
{
       if(q.front==q.rear)
     return ERROR;
    (*e)=q.front->data;
    return TRUE;
}

Status InsertQueue(LinkQueue *q,ElemType e)
//添加元素,从队尾插入
{
         QueuePtr p=(QNode *)malloc(sizeof(QNode));
   if(!p)
    return ERROR;
   p->data=e;
   p->next=NULL;
         (*q).rear->next=p;
   (*q).rear=p;
         return OK;
}
Status DeleteQueue(LinkQueue *q,ElemType *e)
//删除元素,从队头删除
{
      QueuePtr p=(*q).front->next;
   if((*q).front==(*q).rear)
    return ERROR;
      (*e)=p->data;
         (*q).front->next=p->next;
   free(p);
   return OK;
}
Status QueueTraverse(LinkQueue q,Status(*fun)(ElemType))
{
         QueuePtr p=q.front->next;
    printf("队头");
   while(p){
        fun(p->data);
     p=p->next;
   }
    printf("队尾\n");
   return OK;
}

"main.c"

#include"b.h"
#define LEN 5

Status fun(ElemType e)
{
     printf(" %d ",e);
  return OK;
}

int main()
{
  int i;
     LinkQueue q;
  ElemType e;
  InitQueue(&q);
  if(QueueEmpty(q))
   printf("队列为空\n");

  for(i=0;i
    printf("输入元素并插入:");
    scanf(" %d",&e);
       InsertQueue(&q,e);
  }
  QueueTraverse(q,fun);

  for(i=0;i<3;i++){
      DeleteQueue(&q,&e);
   printf("被删除的元素为 %d\n",e);
  }
     QueueTraverse(q,fun);

  if(!QueueEmpty(q))
   printf("队列不为空\n");
      printf("队列的长度为 %d\n",QueueLength(q));
    
     ClearQueue(&q);
        printf("队列的长度为 %d\n",QueueLength(q));

  DestroyQueue(&q);
       
      return OK;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值