• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Jesus Program
博客园    首页    新随笔    联系   管理    订阅  订阅
队列的表示与实现
Code
//队列的表示和实现
#include<stdio.h>
#include
<malloc.h>
#include
<stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
struct Qnode//队列的数据结构
{
    
int data;
    
struct Qnode *next;
};
struct LinkQueue
{
    
struct Qnode *front;//队头指针
    struct Qnode *rear;//队尾指针
};
int InitQueue(struct LinkQueue *Q)//队列的初始化
{
    Q
->front=Q->rear=(struct Qnode *)malloc(sizeof(Qnode));
    
if(!Q->front)
        exit(OVERFLOW);
    Q
->front->next=NULL;
    
return OK;
}
int QueueEmpty(struct LinkQueue *Q)//若队列为空,返回TRUE,否则返回FALSE
{
    
if(Q->front==Q->rear)
        
return TRUE;
    
else
        
return FALSE;
}
int DestroyQueue(struct LinkQueue *Q)//销毁队列
{
    
while(Q->front){
    Q
->rear=Q->front->next;
    free(Q
->front);
    Q
->front=Q->rear;
    }
    
return OK;
}
int EnQueue(struct LinkQueue *Q,int e)//插入元素e为Q的新的队尾元素
{
    
struct Qnode *p;
    p
=(Qnode *)malloc(sizeof(Qnode));
    
if(!p)
        exit(OVERFLOW);
    p
->data=e;
    p
->next=NULL;
    Q
->rear->next=p;
    Q
->rear=p;
    
return OK;
}
int DelQueue(struct LinkQueue *Q,int *e)//若队列不为空,则删除Q的队头元素,并用e返回其值,并返回OK
{
  
struct Qnode *p;
  
if(Q->front==Q->rear)
      
return ERROR;
  p
=Q->front->next;
  
*e=p->data;
  Q
->front->next=p->next;
  
if(Q->rear==p)
      Q
->rear=Q->front;
  free(p);
  
return OK;
}
void Queue()
{
    LinkQueue Q;
    
int N;
    
int e;
    InitQueue(
&Q);
    scanf(
"%d",&N);
    
while(N)
    {
        EnQueue(
&Q,N%8);
        N
=N/8;
    }
    
while(!QueueEmpty(&Q))
    {   
        DelQueue(
&Q,&e);
        printf(
"%d",e);
    }
}
//Queue
int main()
{ 
    printf(
"Input:\n");
    Queue();
    printf(
"\n");
    
return OK;
}
posted on 2009-09-20 21:35  Jesus Program  阅读(167)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3