• 【链表】基本线性队列


      1 //基本线性队列
      2 #include<iostream>
      3 
      4 using namespace std;
      5 
      6 typedef struct _NODE_
      7 {
      8 
      9     int a;
     10     _NODE_* pNext;
     11 }Node,*pNode;
     12 
     13 
     14 class CQueue
     15 {
     16 private:
     17     pNode m_pTop;
     18     pNode m_pBase;
     19     int iNodeCount;
     20 
     21 
     22 public:
     23     CQueue()
     24     {
     25 
     26         m_pTop = m_pBase = NULL;
     27         iNodeCount = 0;
     28     }
     29 
     30     ~CQueue()
     31     {
     32 
     33     }
     34 
     35     bool IsEmpty();
     36     void InitQueue();
     37     bool InQueue(int a);
     38     bool OutQueue(int& a);
     39     void DestroyQueue();
     40     bool GetQueueLenth(int& a);
     41     bool GetQueueTop(int& a);
     42 
     43 
     44 };
     45 
     46 
     47 bool CQueue::GetQueueLenth(int& a)
     48 {
     49     a = iNodeCount;
     50 
     51     return true;
     52 }
     53 bool CQueue::GetQueueTop(int& a)
     54 {
     55     a = m_pTop->a;
     56     
     57     return true;
     58 }
     59 
     60 
     61 
     62 bool CQueue::IsEmpty()
     63 {
     64     if(iNodeCount == 0)
     65     {
     66         return true;
     67     }
     68 
     69     return false;
     70 
     71 }
     72 void CQueue::InitQueue()
     73 {
     74     if(!IsEmpty())
     75     {
     76         DestroyQueue();
     77     }
     78 
     79     m_pTop = m_pBase = NULL;
     80     iNodeCount = 0;
     81 }
     82 
     83 bool CQueue::InQueue(int a)
     84 {
     85     pNode pNodeTemp = new Node;
     86     
     87     if(pNodeTemp != NULL)
     88     {
     89         pNodeTemp->a = a;
     90         pNodeTemp->pNext = NULL;
     91     
     92         if(m_pTop == NULL)
     93         {
     94             m_pTop = m_pBase = pNodeTemp;
     95         }
     96         else
     97         {
     98             m_pBase->pNext = pNodeTemp;
     99 
    100             m_pBase = pNodeTemp;
    101         }
    102 
    103         iNodeCount++;
    104     
    105         return true;
    106     }
    107     return false;
    108 
    109 }
    110     
    111 
    112 bool CQueue::OutQueue(int& a)
    113 {
    114     if(m_pTop == NULL)
    115     {
    116         return false;
    117     }
    118     else
    119     {
    120         pNode pNodeTemp = m_pTop;
    121 
    122         m_pTop =m_pTop->pNext;
    123 
    124         a = pNodeTemp->a;
    125 
    126         delete pNodeTemp;
    127 
    128         iNodeCount--;
    129 
    130         if(iNodeCount == 0)
    131         {
    132             m_pBase = NULL;
    133         }
    134 
    135         return true;
    136     }
    137 }
    138 void CQueue::DestroyQueue()
    139 {
    140     pNode pNodeDel = m_pTop;
    141 
    142     while(pNodeDel != NULL)
    143     {
    144 
    145         m_pTop = m_pTop->pNext;
    146 
    147         delete pNodeDel;
    148 
    149         pNodeDel = m_pTop;
    150 
    151         iNodeCount--;
    152     }
    153 
    154     m_pTop = m_pBase = NULL;
    155     iNodeCount = 0;
    156 }
    157 
    158 
    159 
    160 
    161 int main()
    162 {
    163 
    164 
    165     CQueue One;
    166 
    167     One.InitQueue();
    168 
    169     int i = 0;
    170     int iTemp = 0;
    171     int iLenth = 0;
    172     int iTop = 0;
    173     int a[10] = {13,25,33,15,658,3,97,60,26,16};
    174 
    175     printf("基本队列操作:
    
    ");
    176     if(One.IsEmpty())
    177     {
    178         printf("目前队列中为空
    ");
    179     }
    180 
    181     printf("
    进队:
    ");
    182     for(i=0;i<10;i++)
    183     {
    184         One.InQueue(a[i]);    
    185         printf("%d ",a[i]);
    186     }
    187 
    188     if(!One.IsEmpty())
    189     {
    190         printf("
    
    目前队列中有数据
    ");
    191     }
    192     printf("
    
    ");
    193     
    194 
    195     One.GetQueueLenth(iLenth);
    196     printf("队列长度: %d
    
    ",iLenth);
    197 
    198     One.GetQueueTop(iTop);
    199     printf("队列的头: %d
    
    ",iTop);
    200     
    201     printf("出队:
    ");
    202 
    203     for(i=0;i<10;i++)
    204     {
    205         One.OutQueue(iTemp);
    206         printf("%d ",iTemp);
    207     }
    208     printf("
    ");
    209 
    210     if(One.IsEmpty())
    211     {
    212         printf("
    目前队列中为空
    ");
    213     }
    214 
    215 
    216 
    217 
    218     return 0;
    219 }
  • 相关阅读:
    杭电2007
    杭电 2004
    杭电2005
    杭电2001
    杭电 2000
    Section One
    杭电oj 1002
    杭电oj 1001
    JavaScript高级程序设计第14章表单脚本 (学习笔记)
    JavaScript高级程序设计(学习笔记)
  • 原文地址:https://www.cnblogs.com/Lee-geeker/p/3409174.html
Copyright © 2020-2023  润新知