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

📄 clist.h

📁 我自己写的vc数据结构的作业
💻 H
字号:
/*
 * 循环单链表
 * FileName:clist.h
*/
#ifndef CLIST_H
#define CLIST_H

#include "../slist/slist.h"
#include <cassert>

template<class T>
class CList : public SList<T>
{
protected:
    Node<T> *m_pNodeCurr;

public:
    CList();

public:
    T&      GetNext();
    void    RemoveAt(const int pos);
    int     GetCurrentIndex() const;
};

template<class T>
inline T& CList<T>::GetNext()
{
    assert(0 != m_nCount);

    if ((NULL == m_pNodeCurr) || (NULL == m_pNodeCurr->next))
        m_pNodeCurr = m_pHead;
    else
        m_pNodeCurr = m_pNodeCurr->next;

    return m_pNodeCurr->data;
}

template<class T>
inline int CList<T>::GetCurrentIndex() const
{
    assert(0 != m_nCount);

    int i;
    Node<T> *pTmpNode = m_pHead;

    for (i = 1; i <= m_nCount; ++i)
    {
        if (pTmpNode == m_pNodeCurr)
            return i;
        else
            pTmpNode = pTmpNode->next;
    }

    return 0;
}

template<class T>
inline void CList<T>::RemoveAt(const int pos)
{
    assert(1 <= pos && pos <= m_nCount);

    int i;
    Node<T> *pTmpNode1;
    Node<T> *pTmpNode2;

    pTmpNode1 = m_pHead;

    // head node?
    if (1 == pos)
    {
        m_pHead = m_pHead->next;

        // added for loop list
        // m_pNodeCurr will be set to m_pHead in function GetNext()
        m_pNodeCurr = NULL;

    }
	else
	{

		for (i = 1; i < pos; ++i)
		{
			// we will get the previous node of the target node after
			// the for loop finished, and it would be stored into pTmpNode2
			pTmpNode2 = pTmpNode1;
			pTmpNode1 = pTmpNode1->next;
		}
		pTmpNode2->next = pTmpNode1->next;

		// added for loop list
		m_pNodeCurr = pTmpNode2;
	}

    delete pTmpNode1;
    --m_nCount;
}

template<class T>
inline CList<T>::CList() : m_pNodeCurr(NULL)
{
}


#endif//CLIST_H

⌨️ 快捷键说明

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