在视图里实现链表

源代码在线查看: source.cpp

软件大小: 46 K
上传用户: dujibei
关键词:
下载地址: 免注册下载 普通下载 VIP

相关代码

				// Source.cpp : implementation file
				//
				
				#include "stdafx.h"
				#include "MyHeader.h"
				
				#ifdef _DEBUG
				#define new DEBUG_NEW
				#undef THIS_FILE
				static char THIS_FILE[] = __FILE__;
				#endif
				
				/////////////////////////////////////////////////////////////////////////////
				// CNode
				
				IMPLEMENT_DYNCREATE(CNode, CCmdTarget)
				
				CNode::CNode()
				{
					m_nLVal=m_nRVal=0;
					m_nXPos=m_nYPos=0;
					m_pNext=m_pPrev=NULL;
					m_bCurrent=FALSE;
				}
				
				CNode::~CNode()
				{
				}
				
				
				BEGIN_MESSAGE_MAP(CNode, CCmdTarget)
					//{{AFX_MSG_MAP(CNode)
						// NOTE - the ClassWizard will add and remove mapping macros here.
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CNode message handlers
				/////////////////////////////////////////////////////////////////////////////
				// CDataList
				
				IMPLEMENT_DYNCREATE(CDataList, CCmdTarget)
				
				CDataList::CDataList()
				{
					m_pHead=m_pTail=m_pCurrent=NULL;
					m_nNodeCount=0;
				}
				
				CDataList::~CDataList()
				{
				}
				
				
				BEGIN_MESSAGE_MAP(CDataList, CCmdTarget)
					//{{AFX_MSG_MAP(CDataList)
						// NOTE - the ClassWizard will add and remove mapping macros here.
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CDataList message handlers
				
				void CDataList::Add(CNode *pNode)
				{
					if(m_nNodeCount==0)
					{
						m_pHead=m_pTail=m_pCurrent=pNode;
						m_pCurrent->SetCurrent(TRUE);
					}
					else
					{
						m_pTail->m_pNext=pNode;
						pNode->m_pPrev=m_pTail;
						m_pTail=pNode;
					}
					m_nNodeCount++;
				}
				/////////////////////////////////////////////////////////////////////////////
				// CInputDlg dialog
				
				
				CInputDlg::CInputDlg(CWnd* pParent /*=NULL*/)
					: CDialog(CInputDlg::IDD, pParent)
				{
					//{{AFX_DATA_INIT(CInputDlg)
					m_LVal = 0;
					m_RVal = 0;
					m_strInfo = _T("");
					//}}AFX_DATA_INIT
				}
				
				
				void CInputDlg::DoDataExchange(CDataExchange* pDX)
				{
					CDialog::DoDataExchange(pDX);
					//{{AFX_DATA_MAP(CInputDlg)
					DDX_Text(pDX, IDC_LVAL, m_LVal);
					DDX_Text(pDX, IDC_RVAL, m_RVal);
					DDX_Text(pDX, IDC_INFO, m_strInfo);
					//}}AFX_DATA_MAP
				}
				
				
				BEGIN_MESSAGE_MAP(CInputDlg, CDialog)
					//{{AFX_MSG_MAP(CInputDlg)
					ON_BN_CLICKED(ID_UPDATE, OnUpdate)
					//}}AFX_MSG_MAP
				END_MESSAGE_MAP()
				
				/////////////////////////////////////////////////////////////////////////////
				// CInputDlg message handlers
				
				void CNode::SetData(int lVal, int rVal)
				{
					m_nLVal=lVal;
					m_nRVal=rVal;
				}
				
				void CNode::SetPos(int xPos, int yPos)
				{
					m_nXPos=xPos;
					m_nYPos=yPos;
				}
				
				void CDataList::Draw(CDC *pDC)
				{
					if(m_nNodeCount==0) return;
					for(CNode* pNode=m_pHead;pNode!=NULL;pNode=pNode->m_pNext)
					{
						pNode->Draw(pDC);
					}
				}
				
				void CNode::Draw(CDC *pDC)
				{
					CPen aPen;
					if(m_bCurrent)
						aPen.CreatePen(PS_SOLID,10,RGB(255,0,0));
					else
						aPen.CreatePen(PS_SOLID,10,RGB(0,0,0));
					pDC->SelectObject(&aPen);
				
					pDC->MoveTo(m_nXPos,m_nYPos);
					pDC->LineTo(m_nXPos+NODEWIDTH,m_nYPos);
					pDC->LineTo(m_nXPos+NODEWIDTH,m_nYPos-NODEHEIGHT);
					pDC->LineTo(m_nXPos,m_nYPos-NODEHEIGHT);
					pDC->LineTo(m_nXPos,m_nYPos);
				
					pDC->MoveTo(m_nXPos+NODEWIDTH/4,m_nYPos);
					pDC->LineTo(m_nXPos+NODEWIDTH/4,m_nYPos-NODEHEIGHT);
				
					pDC->MoveTo(m_nXPos+NODEWIDTH*2/4,m_nYPos);
					pDC->LineTo(m_nXPos+NODEWIDTH*2/4,m_nYPos-NODEHEIGHT);
					
					pDC->MoveTo(m_nXPos+NODEWIDTH*3/4,m_nYPos);
					pDC->LineTo(m_nXPos+NODEWIDTH*3/4,m_nYPos-NODEHEIGHT);
				
				
					pDC->Ellipse(m_nXPos+NODEWIDTH/8-SPOINTSIZE,
							m_nYPos-NODEHEIGHT/2+SPOINTSIZE,
							m_nXPos+NODEWIDTH/8+SPOINTSIZE,
							m_nYPos-NODEHEIGHT/2-SPOINTSIZE);
				
					pDC->Ellipse(m_nXPos+NODEWIDTH*7/8-SPOINTSIZE,
							m_nYPos-NODEHEIGHT/2+SPOINTSIZE,
							m_nXPos+NODEWIDTH*7/8+SPOINTSIZE,
							m_nYPos-NODEHEIGHT/2-SPOINTSIZE);
				
					CString str;
					str.Format("%3d",m_nLVal);
				
					int xDPos,yDPos;
					xDPos=m_nXPos+NODEWIDTH/4+10;
					yDPos=m_nYPos-10;
					pDC->TextOut(xDPos,yDPos,str);
				
					str.Format("%3d",m_nRVal);
					xDPos=m_nXPos+NODEWIDTH/2+10;
					yDPos=m_nYPos-10;
					pDC->TextOut(xDPos,yDPos,str);
				
					if(m_pPrev!=NULL)
					{
						xDPos=m_nXPos+NODEWIDTH/8;
						yDPos=m_nYPos-NODEHEIGHT/2;
						int exPos=m_pPrev->m_nXPos+NODEWIDTH/2;
						int eyPos=m_pPrev->m_nYPos-NODEHEIGHT;
						pDC->MoveTo(xDPos,yDPos);
						pDC->LineTo(exPos,eyPos);
					}
					if(m_pNext!=NULL)
					{
						xDPos=m_nXPos+NODEWIDTH*7/8;
						yDPos=m_nYPos-NODEHEIGHT/2;
						int exPos=m_pNext->m_nXPos+NODEWIDTH/2;
						int eyPos=m_pNext->m_nYPos;
						pDC->MoveTo(xDPos,yDPos);
						pDC->LineTo(exPos,eyPos);
				
					}
				}
				
				void CInputDlg::SetParent(CEx702View *pParent)
				{
					m_pParent=pParent;
				}
				
				void CInputDlg::OnUpdate() 
				{
					// TODO: Add your control notification handler code here
					m_pParent->SendMessage(MSG_UPDATE,0,0L);
				
				}
				
				void CNode::SetCurrent(BOOL bCurrent)
				{
					m_bCurrent=bCurrent;
				}
				
				BOOL CDataList::SetCurrent(CDC *pDC, CPoint point)
				{
				
					BOOL bRet=FALSE;
					if(m_nNodeCount==0) return FALSE;
					for(CNode* pNode=m_pHead;pNode!=NULL;pNode=pNode->m_pNext)
					{
						if(pNode->TestTouch(pDC,point))
						{
							m_pCurrent->SetCurrent(FALSE);
							m_pCurrent=pNode;
							m_pCurrent->SetCurrent(TRUE);			
							bRet=TRUE;
							break;
						}
					}
					return bRet;
				}
				
				BOOL CNode::TestTouch(CDC *pDC, CPoint point)
				{
					CRect rect=CRect(m_nXPos,m_nYPos,
						m_nXPos+NODEWIDTH,m_nYPos-NODEHEIGHT);
					pDC->LPtoDP(&rect);
					if(rect.PtInRect(point))
						return TRUE;
					return FALSE;
				}
				
				void CDataList::DeleteNode()
				{
					if(m_nNodeCount==0)return;
					m_nNodeCount--;
					if(m_nNodeCount==0)
						;
					else if(m_nNodeCount==1)
						;
					else
					{
					}
					if(m_pCurrent!=NULL)
					{
						CNode* pNext=m_pCurrent->m_pNext;
						CNode* pPrev=m_pCurrent->m_pPrev;
						delete m_pCurrent;
						
						m_pCurrent=NULL;
				
						if(pPrev!=NULL)
						{
							m_pCurrent=pPrev;
							pPrev->m_pNext=pNext;
							pNext->m_pPrev=pPrev;
						}
						else if(pNext!=NULL)
						{
							m_pCurrent=pNext;
							pNext->m_pPrev=pPrev;
						}
						else
						{
							m_pHead=m_pTail=m_pCurrent=NULL;
						}
					}
				}
				
				void CDataList::Offset(int xoff, int yOff)
				{
				
				}
							

相关资源