⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ql.cpp

📁 数据结构中所有算法的实现
💻 CPP
字号:
#include "d:\cpp\head.h"
typedef char ElemType;
typedef struct QNode{
  ElemType data;
  struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
  QueuePtr front;
  QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q){
  Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
  if(!Q.front)exit(OVERFLOW);
  Q.front->next=NULL;
  return OK;
}
Status DestroyQueue(LinkQueue &Q){
  while(Q.front){
    Q.rear=Q.front->next;
    free(Q.front);
    Q.front=Q.rear;
  }//while
  return OK;
}
Status ClearQueue(LinkQueue &Q){
  QNode *p;
  p=Q.rear=Q.front->next;
  Q.front->next=NULL;
  while(Q.rear){
    Q.rear=p->next;
    free(p);
    p=Q.rear;
  }//while
  Q.rear=Q.front;
  return OK;
}
Status QueueEmpty(LinkQueue Q){
  if(Q.rear==Q.front)return TRUE;
  else return FALSE;
}
int QueueLength(LinkQueue Q){
  int i=0;
  Q.rear=Q.front->next;
  while(Q.rear){
    i++;
    Q.rear=Q.rear->next;
  }
  return i;
}
Status GetHead(LinkQueue Q,ElemType &e){
  if(QueueEmpty(Q))return ERROR;
  e=Q.front->next->data;
  return OK;
}
Status EnQueue(LinkQueue &Q,ElemType e){
  QueuePtr p;
  p=(QueuePtr)malloc(sizeof(QNode));
  if(!p)return ERROR;
  p->next=NULL;
  p->data=e;
  Q.rear->next=p;
  Q.rear=p;
  return OK;
}
Status DeQueue(LinkQueue &Q,ElemType &e){
  QueuePtr p;
  if(QueueEmpty(Q))return ERROR;
  //if(QueueEmpty)return ERROR;  qieji qie jie yi ge yan zhong de cuo wu.
  p=Q.front->next;
  e=p->data;
  Q.front->next=p->next;
  if(p==Q.rear)Q.rear=Q.front;
  free(p);
  return OK;
}
void visit(ElemType e){
  printf("%5c",e);
}
void Traverse(LinkQueue Q,void (*visit)(ElemType e)){
  Q.rear=Q.front->next;
  while(Q.rear){
    visit(Q.rear->data);
    Q.rear=Q.rear->next;
  }
  printf("\n TRAVERSE END \n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -