I'm building a program that manipulates with Singly Linked List which has a one data member ( int ), and there are many functions like: add first, delete, ...
The problem is: How can I draw the list ? ( It is okay if each node represented by a square and inside it there is the int data ).
These are some pics of my program :
The main window:
Resource View:
Files View:
Class View:
( I created a class for each dialog, is that ok ? )
This the source code for "Singly Linked List intDlg":
PHP Code:
// Singly Linked List intDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Singly Linked List int.h"
#include "Singly Linked List intDlg.h"
//$$$$$$$$$$$$$$$$$$$$
#include "AddFirst.h"
#include "Delete.h"
#include "Size.h"
#include "Print.h"
#include "About.h"
//$$$$$$$$$$$$$$$$$$$$
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$$$$$$$$$$$$$$$$$$$$$ Node Class $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$$$$$$$$$$$$$$$$$
AddFirst addFirstDLG;
Delete deleteDLG;
Size sizeDLG;
Print printDLG;
About aboutDLG;
//$$$$$$$$$$$$$$$$$$$
class Node
{
public:
Node( int );
~Node();
int DATA;
Node* next;
};
Node::Node( int nd )
{
DATA = nd;
next = NULL;
}
Node::~Node()
{
next = NULL;
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$$$$$$$$$$$$$$$$$$$$$ Node Class $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
/*********************
**********************/
Node* firstPtr = NULL;
int sizeOfList;
/**********************
*********************/
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSinglyLinkedListintDlg dialog
CSinglyLinkedListintDlg::CSinglyLinkedListintDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSinglyLinkedListintDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSinglyLinkedListintDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSinglyLinkedListintDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSinglyLinkedListintDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSinglyLinkedListintDlg, CDialog)
//{{AFX_MSG_MAP(CSinglyLinkedListintDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_ADD_AFTER, OnAddAfter)
ON_BN_CLICKED(IDC_ADD_BEFORE, OnAddBefore)
ON_BN_CLICKED(IDC_SIZE, OnSize)
ON_BN_CLICKED(IDC_IS_EMPTY, OnIsEmpty)
ON_BN_CLICKED(IDC_SWAP, OnSwap)
ON_BN_CLICKED(IDC_IN_LIST, OnInList)
ON_BN_CLICKED(IDC_GET_INDEX, OnGetIndex)
ON_BN_CLICKED(IDC_PRINT, OnPrint)
ON_BN_CLICKED(IDC_ADD_FIRST, OnAddFirst)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSinglyLinkedListintDlg message handlers
BOOL CSinglyLinkedListintDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CSinglyLinkedListintDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSinglyLinkedListintDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSinglyLinkedListintDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSinglyLinkedListintDlg::OnExit()
{
// TODO: Add your control notification handler code here
MessageBox("Thank you for using our program","Singly Linked List", MB_ICONINFORMATION );
exit(1);
}
void CSinglyLinkedListintDlg::OnAddFirst()
{
// TODO: Add your control notification handler code here
addFirstDLG.DoModal();
int newData = addFirstDLG.m_data;
Node* newNode = new Node( newData );
if ( firstPtr != NULL )
{
newNode->next=firstPtr;
firstPtr=newNode;
}
else
firstPtr = newNode;
}
void CSinglyLinkedListintDlg::OnDelete()
{
// TODO: Add your control notification handler code here
Node* currentPtr = firstPtr;
Node* prevPtr = NULL;
bool deleted = false;
deleteDLG.DoModal();
int sd = deleteDLG.m_toDelete;
while( currentPtr != NULL )
{
if ( currentPtr->DATA == sd )
{
if ( currentPtr == firstPtr )
{
firstPtr = firstPtr->next;
delete currentPtr;
MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION);
deleted = true;
break;
}
if ( currentPtr->next == NULL )
{
prevPtr->next = NULL;
delete currentPtr;
MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION);
deleted = true;
break;
}
prevPtr->next = currentPtr->next;
delete currentPtr;
MessageBox("The node is deleted", "Singly Linked List", MB_ICONINFORMATION);
deleted = true;
break;
}
prevPtr = currentPtr;
currentPtr = currentPtr ->next;
}
if ( deleted == false )
MessageBox("The node does not exist or the list is empty", "Singly Linked List", MB_ICONEXCLAMATION );
}
void CSinglyLinkedListintDlg::OnAddAfter()
{
// TODO: Add your control notification handler code here
}
void CSinglyLinkedListintDlg::OnAddBefore()
{
// TODO: Add your control notification handler code here
}
void CSinglyLinkedListintDlg::OnSize()
{
// TODO: Add your control notification handler code here
Node* currentPtr = firstPtr;
int count=0;
while( currentPtr != NULL )
{
count++;
currentPtr = currentPtr -> next;
}
sizeDLG.m_size = count;
sizeOfList = count;
sizeDLG.DoModal();
}
void CSinglyLinkedListintDlg::OnIsEmpty()
{
// TODO: Add your control notification handler code here
if ( firstPtr == NULL )
MessageBox("The list is empty");
else if ( firstPtr != NULL )
MessageBox("The list is not empty");
}
void CSinglyLinkedListintDlg::OnSwap()
{
// TODO: Add your control notification handler code here
}
void CSinglyLinkedListintDlg::OnInList()
{
// TODO: Add your control notification handler code here
}
void CSinglyLinkedListintDlg::OnGetIndex()
{
// TODO: Add your control notification handler code here
}
void CSinglyLinkedListintDlg::OnPrint()
{
// TODO: Add your control notification handler code here
Node* currentPtr = firstPtr;
printDLG.DoModal();
while( currentPtr != NULL )
{
/*
.
.
.
*/
//cout << currentPtr->DATA << endl;
currentPtr = currentPtr->next;
}
}
void CSinglyLinkedListintDlg::OnAbout()
{
// TODO: Add your control notification handler code here
aboutDLG.DoModal();
}
So, what I want is: How can I create the print function so the user can see the list ?


