• 数据结构-循环列表


      1 ///////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  FileName    :   clist.h
      4 //  Version     :   0.10
      5 //  Author      :   Ryan Han
      6 //  Date        :   2013/06/25 10:05:10
      7 //  Comment     :  
      8 //
      9 ///////////////////////////////////////////////////////////////////////////////
     10 #ifndef __CIRC_LIST_H__
     11 #define __CIRC_LIST_H__
     12 
     13 #include "slist.h"
     14 
     15     
     16 template <typename T>
     17 class CCList : public CSList<T>
     18 {
     19 public:
     20     using CSList<T>::m_nCount;
     21     using CSList<T>::m_pNodeHead;
     22 
     23 protected:
     24     CNode<T> *m_pNodeCurr;
     25 
     26 public:
     27     CCList();
     28 
     29 public:
     30     T& GetNext();
     31     void RemoveAt(const int pos);
     32     int GetCurrentIndex() const;
     33 };
     34 
     35 template <class T>
     36 inline CCList<T>::CCList()
     37 {
     38 }
     39 
     40 template <typename T>
     41 inline T& CCList<T>::GetNext()
     42 {
     43     ASSERT(0 != m_nCount);
     44     
     45     if((NULL == m_pNodeCurr) || (NULL == m_pNodeCurr->next))
     46         m_pNodeCurr = m_pNodeHead;
     47     else
     48         m_pNodeCurr = m_pNodeCurr->next;
     49 
     50     return m_pNodeCurr->data;
     51 }
     52 
     53 template <typename T>
     54 inline int CCList<T>::GetCurrentIndex() const
     55 {
     56     ASSERT(0 != m_nCount);
     57     
     58     int i;
     59     CNode<T> *pTmpNode = m_pNodeHead;
     60     
     61     for(i = 1; i <= m_nCount; ++i)
     62     {
     63         if(pTmpNode == m_pNodeCurr)
     64             return i;
     65         else
     66             pTmpNode = pTmpNode->next;
     67     }
     68     
     69     return 0;
     70 }
     71 
     72 template <typename T>
     73 inline void CCList<T>::RemoveAt(const int pos)
     74 {
     75     ASSERT(1 <= pos && pos <= m_nCount);
     76     
     77     int i;
     78     CNode<T> *pTmpNode1;
     79     CNode<T> *pTmpNode2;
     80     
     81     pTmpNode1 = m_pNodeHead;
     82     
     83     //head node?
     84     if(1 == pos)
     85     {
     86         if(m_pNodeCurr == m_pNodeHead)
     87             m_pNodeCurr = NULL;
     88         m_pNodeHead = m_pNodeHead->next;
     89         goto Exit1;
     90     }
     91 
     92     for(i = 1; i < pos; ++i)
     93     {
     94         pTmpNode2 = pTmpNode1;
     95         pTmpNode1 = pTmpNode1->next;
     96     }
     97     pTmpNode2->next = pTmpNode1->next;
     98     
     99     m_pNodeCurr = pTmpNode2;
    100         
    101 Exit1:
    102     delete pTmpNode1;
    103     --m_nCount;
    104 }
    105 
    106 /*
    107 template <typename T>
    108 inline CCList<T>::~CCList() : m_pNodeCurr(NULL)
    109 {
    110 }
    111 */
    112 
    113 #endif
    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   joseph.cpp
    //  Version     :   0.10
    //  Author      :   Ryan Han
    //  Date        :   2013/06/26 12:55:13
    //  Comment     :  
    //
    ///////////////////////////////////////////////////////////////////////////////
    #include "clist.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int i;
    	int n;
    	int m;
    	int nNumber;
    	int nCurIndex;
    	CCList<int> clist;
    	
    //#ifdef _DEBUG
    //	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    //#endif
    
    	cout << "Please input the number of person: ";
    	cin >> n;
    	
    	cout << "Please enter the deatch number: ";
    	cin >> m;
    	
    	for(i = 1; i <= n; ++i)
    	{
    		clist.AddTail(i);
    	}
    	
    	i = 0;
    	do 
    	{
    		++i;
    		nNumber = clist.GetNext();
    		if(i == m)
    		{
    			cout << "The " << nNumber << " is removed." << endl;
    			
    			nCurIndex  = clist.GetCurrentIndex();
    			clist.RemoveAt(nCurIndex);
    			--n;
    			
    			i = 0;
    		}
    		
    	}while(1 != n);
    	
    	cout << "The last number left is: " << clist.GetHead() << endl;
    }
    

      

  • 相关阅读:
    前端请求跨域理解
    可视化交互行为
    文章标题
    在map上标记point
    基于force布局的map
    stack布局
    python一些特有语法
    histogram布局用法
    patition布局
    Shell命令行处理JSON
  • 原文地址:https://www.cnblogs.com/dracohan/p/3158485.html
Copyright © 2020-2023  润新知