티스토리 뷰

공부 이야기

[C] List?

판다(panda) 2009. 1. 7. 00:03
List.h
#if !defined(AFX_LIST_H__1AAF7FC2_D6C4_4EB1_9396_379E52798202__INCLUDED_)
#define AFX_LIST_H__1AAF7FC2_D6C4_4EB1_9396_379E52798202__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
// _MSC_VER > 1000
struct sNODE
{
      int            nData;
      sNODE     *pNext;
};
class CList 
{
public:
      CList();
      virtual     ~CList();
      void       Insert(int nData);
      int         GetAt(int nIndex);
//   void       Print(int nIndex);
//   int          Find(int nData);

      int          GetCount();
      int          Delete();
private:
      sNODE  *m_pHead,
                   *m_pTail;
};
#endif // !defined(AFX_LIST_H__1AAF7FC2_D6C4_4EB1_9396_379E52798202__INCLUDED_)
 
List.cpp
// List.cpp: implementation of the CList class.
//////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include
"List.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CList::CList()
{
         m_pHead    = NULL;
         m_pTail      = NULL;
         m_pHead    = new sNODE;
         m_pTail      = new sNODE;
         m_pHead->pNext     = m_pTail;
         m_pTail->pNext       = NULL;
}
CList::~CList()
{
 
}
void CList::Insert(int nData)
{
 sNODE         *pNewnode;
 pNewnode               = new sNODE;
 pNewnode->nData    = nData;
 pNewnode->pNext   = m_pHead->pNext;
 m_pHead->pNext     = pNewnode;
}

int CList::GetCount()
{
         sNODE     *pNode;
         int  nCount    = 0;
         pNode          = m_pHead;
         while(pNode->pNext != NULL)
        {
              pNode   = pNode->pNext;
              ++nCount;
        }
         return (nCount-1);
}
int CList::Delete()
{
        if(GetCount == 0)
            return false;
        sNODE     *pNode;
        pNode      = m_pHead;
        pNode      = pNode->pNext;
        m_pHead->pNext   = pNode->pNext;
        delete pNode;
        return true;
}
 
main.cpp
#include <stdio.h>
#include
"List.h"
void main()
{
         CList List;
         List.Insert(5);
         List.Insert(12);
         List.Insert(3);
         List.Insert(9);
         printf("Count %d\n", List.GetCount());
}
 
Result