• 线性表-链表队列


      1: //queue.cpp
    
      2: 
    
      3: #include "queue.h"
    
      4: 
    
      5: Queue::Queue()
    
      6: {
    
      7:   front = new Node;
    
      8:   rear = new Node;
    
      9:   front->next = NULL;
    
     10:   rear = front;
    
     11:   length = 0;
    
     12: }
    
     13: 
    
     14: Queue::~Queue()
    
     15: {
    
     16:   Node * pTemp;
    
     17:   while(front->next != NULL)
    
     18:   {
    
     19:     pTemp = front;
    
     20:     front = front->next;
    
     21:     delete pTemp;
    
     22:   }
    
     23:   delete rear;
    
     24: }
    
     25: 
    
     26: void Queue::InitQueue()
    
     27: {
    
     28:   rear = front;
    
     29:   length = 0;
    
     30: }
    
     31: 
    
     32: void Queue::ClearQueue()
    
     33: {
    
     34:   Node * pTemp;
    
     35:   while(front->next != NULL)
    
     36:   {
    
     37:     pTemp = front->next;
    
     38:     front = pTemp->next;
    
     39:     delete pTemp;
    
     40:   }
    
     41:   rear = front;
    
     42:   length = 0;
    
     43: }
    
     44: 
    
     45: bool Queue::IsEmpty()
    
     46: {
    
     47:   return (length == 0);
    
     48: }
    
     49: 
    
     50: void Queue::EnQueue(int item)
    
     51: {
    
     52:   Node * pTemp = new Node;
    
     53:   pTemp->data = item;
    
     54:   pTemp->next = NULL;
    
     55:   rear->next = pTemp;
    
     56:   rear = pTemp;
    
     57:   length += 1;
    
     58: }
    
     59: 
    
     60: void Queue::DeQueue()
    
     61: {
    
     62:   if(front->next != rear)
    
     63:   {
    
     64:     Node * pTemp = front->next;
    
     65:     front->next = front->next->next;
    
     66:     delete pTemp;
    
     67:     length -= 1;
    
     68:   }
    
     69: }
    
     70: 
    
     71: int Queue::GetLength()
    
     72: {
    
     73:   return length;
    
     74: }
    
     75: 
    
     76: void Queue::PrintQueue()
    
     77: {
    
     78:   Node * pTemp = front->next;
    
     79:   while(front->next != rear)
    
     80:   {
    
     81:     cout<<front->next->data<<endl;
    
     82:     front->next = front->next->next;
    
     83:   }
    
     84:   front->next = pTemp;
    
     85:   cout<<rear->data<<endl;
    
     86: }
      1: //queue.h
    
      2: 
    
      3: #ifndef QUEUE_H_H
    
      4: #define QUEUE_H_H
    
      5: 
    
      6: #include <iostream>
    
      7: using namespace std;
    
      8: 
    
      9: struct Node
    
     10: {
    
     11:   int data;
    
     12:   Node * next;
    
     13: };
    
     14: 
    
     15: class Queue
    
     16: {
    
     17:   Node * front;
    
     18:   Node * rear;
    
     19:   int length;
    
     20: public:
    
     21:   Queue();
    
     22:   ~Queue();
    
     23:   void InitQueue();
    
     24:   void ClearQueue();
    
     25:   bool IsEmpty();
    
     26:   void EnQueue(int item);
    
     27:   void DeQueue();
    
     28:   int GetLength();
    
     29:   void PrintQueue();
    
     30: };
    
     31: 
    
     32: #endif
      1: //test.cpp
    
      2: 
    
      3: #include "queue.h"
    
      4: #include <iostream>
    
      5: 
    
      6: int main(int argc, char * argv[])
    
      7: {
    
      8:   Queue * queue = new Queue;
    
      9:   queue->InitQueue();
    
     10:   queue->EnQueue(1);
    
     11:   queue->EnQueue(2);
    
     12:   queue->EnQueue(3);
    
     13:   queue->EnQueue(4);
    
     14:   queue->EnQueue(5);
    
     15:   queue->PrintQueue();
    
     16:   if(queue->IsEmpty())
    
     17:   {
    
     18:     cout<<"empty."<<endl;
    
     19:   }
    
     20:   else
    
     21:   {
    
     22:     cout<<"not empty."<<endl;
    
     23:   }
    
     24:   cout<<"length is "<<queue->GetLength()<<endl;
    
     25:   queue->DeQueue();
    
     26:   queue->PrintQueue();
    
     27:   queue->DeQueue();
    
     28:   queue->PrintQueue();
    
     29:   
    
     30:   return 0;
    
     31: }
    作者:Lucas Hsueh
    文章部分是自己的学习体会、代码等,还有收集和整理其他技术牛人的文章。
  • 相关阅读:
    递归算法——求取斐波那契数列(2)
    递归算法——求取斐波那契数列(1)
    《鸟哥的Linux私房菜》学习笔记(1)——文件与目录
    C# FINEUI 多页签(tab)中的笔记
    echarts 在.net 中和数据库交互
    FineUiMVC的tab控件加载URL(内部的_外部的)
    随想
    工作记录
    sql生成唯一标识
    ubuntu 中安装oracle 步骤
  • 原文地址:https://www.cnblogs.com/lucas-hsueh/p/3711338.html
Copyright © 2020-2023  润新知