Queue.cpp
Upload User: gisslht
Upload Date: 2022-07-26
Package Size: 111k
Code Size: 1k
Category:

Graph program

Development Platform:

Visual C++

  1. #include "Queue.h"
  2. #include "tu.h"
  3. #include <iostream>
  4. using namespace std;
  5. int InitQueue( Queue &Q ) //初始化队列
  6. {
  7. Q.base = (ElemType *)malloc(Max_Vertex_Num*sizeof(ElemType));
  8. if ( !Q.base)
  9. exit(OVERFLOW); //分配失败
  10. Q.front = Q.rear = 0;
  11. return OK;
  12. }
  13. int EnQueue( Queue &Q, int e ) //入队
  14. {
  15. if( ( Q.rear + 1 ) % Max_Vertex_Num == Q.front )
  16. return ERROR; //队满
  17. Q.base[Q.rear] = e;
  18. Q.rear = ( Q.rear +1 ) % Max_Vertex_Num; //尾指针向后走一步
  19. return OK;
  20. }
  21. int DeQueue( Queue &Q, int &u ) //出队
  22. {
  23. int e;
  24. if( Q.front == Q.rear)
  25. return ERROR;
  26. else
  27. {
  28. e = Q.base[Q.front]; //将队中的队头元素(出队)赋给e
  29. Q.base[Q.front] = 0; //将队头元素的值赋值为0
  30. Q.front = ( Q.front + 1 ) % Max_Vertex_Num; //头指针向后走一步
  31. }
  32. u = e;
  33. return OK;
  34. }
  35. int QueueEmpty( Queue &Q ) //判断是否为空
  36. {
  37. if( Q.front == Q.rear )
  38. return ERROR;
  39. else
  40. return OK; 
  41. }