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

📄 chxe.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TD>
<P>Removes the list's head node.</P>
<TR>
<TD>
<pre><font 
color="#008000">RemoveTail()</font></pre>
<TD>
<P>Removes the list's tail node.</P>
<TR>
<TD>
<pre><font color="#008000">SetAt()</font></pre>
<TD>
<P>Sets the node at the specified position.</P></TABLE>
<P><B>Introducing the List Application</B></P>
<P>As 
you've no doubt guessed, now that you know a little about list classes and their member functions, you're going to get a chance to see lists in action, in the List Application. When you run the application, you see the window shown in Figure E.8. The 
window displays the values of the single node with which the list begins. Each node in the list can hold two different values, both of which are integers.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>This application is on the CD in the RefE folder.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<A HREF="IIfig08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig08.gif"><b>Fig. E.8</b></A>
<P><I>The List Demo application starts off with one node in its list.</I></P>
<P>Using the List Demo application, you can experiment with adding and removing nodes 
from a list. To add a node, left-click in the application's window. You then see the dialog box shown in Figure E.9. Enter the two values that you want the new node to hold and then click OK. When you do, the program adds the new node to the tail of the 
list and displays the new list in the window. For example, if you  enter the values 55 and 65 into the dialog box, yousee the display shown in Figure E.10.</P>
<A HREF="IIfig09.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig09.gif"><b>Fig. E.9</b></A>
<P><I>A left-click in the window brings up 
the Add Node dialog box.</I></P>
<A HREF="IIfig10.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig10.gif"><b>Fig. E.10</b></A>
<P><I>Each node you add to the list can hold two different values.</I></P>
<P>You can also delete nodes from the list. To do this, right-click in the window to display 
the Remove Node dialog box (Figure E.11). Using this dialog box, you can choose to remove the head or tail node. If you exit the dialog box by clicking OK, the program deletes the specified node and displays the resulting list in the window.</P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>If you try to delete nodes from an empty list, the List Demo application displays a message box, warning you of your error. If the application didn't catch this possible error, the program could crash when it tries to 
delete a nonexistent node.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<A HREF="IIfig11.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig11.gif"><b>Fig. E.11</b></A>
<P><I>Right-click in the window to delete a node.</I></P>
<P><B>Declaring and Initializing the List</B></P>
<P>Declaring a list is as 
easy as declaring any other data type. Just include the name of the class you're using, followed by the name of the object. For example, the List Demo application declares its list like this:</P>
<pre><font color="#008000">CPtrList list;</font></pre>

<P>Here, the program is declaring an object of the <font color="#008000">CPtrList</font> class. This class holds a linked list of pointers, which means that the list can reference just about any type of information.</P>
<P>Although there's not much you 
need to do to initialize an empty list, you do need to decide what type of information will be pointed to by the pointers in the list. That is, you need to declare exactly what a node in the list will look like. The List Demo application declares a node as 
shown in Listing E.4.</P>
<P><B>Listing E.4 &#151;</B><B><I>CNode</I></B><B> Structure</B></P>
<pre><font color="#008000">struct CNode</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    int value1;</font></pre>
<pre><font 
color="#008000">    int value2;</font></pre>
<pre><font color="#008000">};</font></pre>
<P>Here, a node is defined as a structure holding two integer values. However, you can create any type of data structure you like for your nodes. To add a node to a 
list, you use the <font color="#008000">new</font> operator to create a node structure in memory, and then you add the returned pointer to the pointer list. The List Demo application begins its list with a single node, which is created in the view class's 
constructor, as shown in Listing E.5.</P>
<P><B>Listing E.5&#151;</B><B><I>CMyListView</I></B><B> constructor</B></P>
<pre><font color="#008000">CMyListView::CMyListView()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">     
CNode* pNode = new CNode;</font></pre>
<pre><font color="#008000">     pNode-&gt;value1 = 11;</font></pre>
<pre><font color="#008000">     pNode-&gt;value2 = 22;</font></pre>
<pre><font color="#008000">     list.AddTail(pNode);</font></pre>
<pre><font 
color="#008000">}</font></pre>
<P>In Listing E.5, the program first creates a new <font color="#008000">CNode</font> structure on the heap and then sets the node's two members. After initializing the new node, a quick call to the list's <font 
color="#008000">AddTail()</font> member function adds the node to the list. Because the list was empty, adding a node to the tail of the list is the same as adding the node to the head of the list. That is, the program could have also called <font 
color="#008000">AddHead()</font> to add the node. In either case, the new single node is now both the head and tail of the list.</P>
<P><B>Adding a Node to the List</B></P>
<P>Although you can insert nodes into a list at any position, the easiest way to 
add to a list is to add a node to the head or tail, making the node the new head or tail. In the List Demo application, you left-click in the window to bring up the Add Node dialog box, so you'll want to examine the <font 
color="#008000">OnLButtonDown()</font> function, which looks like Listing E.6.</P>
<P><B>Listing E.6&#151;</B><B><I>CMyListView::OnLButtonDown()</I></B></P>
<pre><font color="#008000">void CMyListView::OnLButtonDown(UINT nFlags, CPoint point) </font></pre>

<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    // Create and initialize the dialog box.</font></pre>
<pre><font color="#008000">    AddNodeDlg dialog;</font></pre>
<pre><font color="#008000">    dialog.m_value1 = 0;</font></pre>

<pre><font color="#008000">    dialog.m_value2 = 0;</font></pre>
<pre><font color="#008000">    // Display the dialog box.</font></pre>
<pre><font color="#008000">    int result = dialog.DoModal();</font></pre>
<pre><font color="#008000">    // If the user 
clicked the OK button...</font></pre>
<pre><font color="#008000">    if (result == IDOK)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        // Create and initialize the new node.</font></pre>
<pre><font 
color="#008000">        CNode* pNode = new CNode;</font></pre>
<pre><font color="#008000">        pNode-&gt;value1 = dialog.m_value1;</font></pre>
<pre><font color="#008000">        pNode-&gt;value2 = dialog.m_value2;</font></pre>
<pre><font 
color="#008000">        // Add the node to the list.</font></pre>
<pre><font color="#008000">        list.AddTail(pNode);</font></pre>
<pre><font color="#008000">        // Repaint the window.</font></pre>
<pre><font color="#008000">        
Invalidate();</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font color="#008000">    CView::OnLButtonDown(nFlags, point);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>In Listing E.6, after displaying the dialog box, the 
program checks whether the user exited the dialog with the OK button. If so, the user wants to add a new node to the list. In this case, the program creates and initializes the new node, just as it did previously for the first node that it added in the 
view class's constructor. The program adds the node in the same way, too, by calling the <font color="#008000">AddTail()</font>. If you want to modify the List Demo application, one thing you could try is giving the user a choice between adding the node at 
the head or the tail of the list, instead of just at the tail.</P>
<P><B>Deleting a Node from the List</B></P>
<P>Deleting a node from a list can be easy or more complicated, depending on where in the list you want to delete the node. As with adding a 
node, dealing with nodes other than the head or tail requires that you first locate the node that you want and then get its position in the list. You'll learn about node positions in the next section, which demonstrates how to iterate over a list. To keep 
things simple, however, this program enables you to delete nodes only from the head or tail of the list, as shown in Listing E.7.</P>
<P><B>Listing E.7&#151;</B><B><I>CMyListView::OnRButtonDown()</I></B></P>
<pre><font color="#008000">void 
CMyListView::OnRButtonDown(UINT nFlags, CPoint point) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    // Create and initialize the dialog box.</font></pre>
<pre><font color="#008000">    RemoveNodeDlg 
dialog;</font></pre>
<pre><font color="#008000">    dialog.m_radio = 0;</font></pre>
<pre><font color="#008000">    // Display the dialog box.</font></pre>
<pre><font color="#008000">    int result = dialog.DoModal();</font></pre>
<pre><font 
color="#008000">    // If the user clicked the OK button...</font></pre>
<pre><font color="#008000">    if (result == IDOK)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        CNode* pNode;</font></pre>
<pre><font 
color="#008000">        // Make sure the list isn't empty.</font></pre>
<pre><font color="#008000">        if (list.IsEmpty())</font></pre>
<pre><font color="#008000">            MessageBox(&quot;No nodes to delete.&quot;);</font></pre>
<pre><font 
color="#008000">        else</font></pre>
<pre><font color="#008000">        {</font></pre>
<pre><font color="#008000">            // Remove the specified node.</font></pre>
<pre><font color="#008000">            if (dialog.m_radio == 0)</font></pre>

<pre><font color="#008000">                pNode = (CNode*)list.RemoveHead();</font></pre>
<pre><font color="#008000">            else</font></pre>
<pre><font color="#008000">                pNode = (CNode*)list.RemoveTail();</font></pre>
<pre><font 
color="#008000">            // Delete the node object and repaint the window.</font></pre>
<pre><font color="#008000">            delete pNode;</font></pre>
<pre><font color="#008000">            Invalidate();</font></pre>
<pre><font color="#008000">        
}</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font color="#008000">    CView::OnRButtonDown(nFlags, point);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>Here, after displaying the dialog box, the program checks whether the 
user exited the dialog box via the OK button. If so, the program must then check whether the user wants to delete a node from the head or tail of the list. If the Remove Head radio button was checked, the dialog box's <font color="#008000">m_radio</font> 
data member will be 0. In this case, the program calls the list class's <font color="#008000">RemoveHead()</font> member function. Otherwise, the program calls <font color="#008000">RemoveTail()</font>. Both of these functions return a pointer to the 
object that was removed from the list. Before calling either of these member functions, however, notice how the program calls <font color="#008000">IsEmpty()</font> in order to determine whether the list contains any nodes. You can't delete a node from an 
empty list.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>Notice that, when removing a node from the list, the List Demo application calls <font color="#008000">delete</font> on the pointer returned by the list. It's important to remember that, when you 
remove a node from a list, the node's pointer is removed from the list, but the object to which the pointer points is still in memory, where it stays until you delete it.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><B>Iterating Over the List</B></P>

<P>Often, you'll want to <I>iterate over</I> (read through) a list. You might, for example, as is the case with List, want to display the values in each node of the list, starting from the head of the list and working your way to the tail. The List 
application does exactly this in its <font color="#008000">OnDraw()</font> function, as shown in Listing E.8.</P>
<P><B>Listing E.8&#151;</B><B><I>CMyListView::OnDraw()</I></B></P>
<pre><font color="#008000">void CMyListView::OnDraw(CDC* pDC)</font></pre>

<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    CListDoc* pDoc = GetDocument();</font></pre>
<pre><font color="#008000">    ASSERT_VALID(pDoc);</font></pre>
<pre><font color="#008000">// Get the current font's height.</font></pre>

<pre><font color="#008000">    TEXTMETRIC textMetric;</font></pre>
<pre><font color="#008000">    pDC-&gt;GetTextMetrics(&amp;textMetric);</font></pre>
<pre><font color="#008000">    int fontHeight = textMetric.tmHeight;</font></pre>
<pre><font 
color="#008000">    // Initialize values used in the loop.</font></pre>
<pre><font color="#008000">    POSITION pos = list.GetHeadPosition();</font></pre>
<pre><font color="#008000">    int displayPosition = 10;</font></pre>
<pre><font color="#008000">    
int index = 0;</font></pre>
<pre><font color="#008000">    // Iterate over the list, displaying each node's values.</font></pre>
<pre><font color="#008000">    while (pos != NULL)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font 
color="#008000">        CNode* pNode = (CNode*)list.GetNext(pos);</font></pre>
<pre><font color="#008000">        char s[81];</font></pre>
<pre><font color="#008000">        wsprintf(s, &quot;Node %d contains %d and %d.&quot;,</font></pre>
<pre><font 
color="#008000">            index, pNode-&gt;value1, pNode-&gt;value2);</font></pre>
<pre><font color="#008000">        pDC-&gt;TextOut(10, displayPosition, s);</font></pre>
<pre><font color="#008000">        displayPosition += fontHeight;</font></pre>

<pre><font color="#008000">        ++index;</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font color="#008000">}</font></pre>
<P>In Listing E.8, the program gets the position of the head node by calling the <font 
color="#008000">GetHeadPosition()</font> member function. The position is a value that many of the list class's member functions use to quickly locate nodes in the list. You must have this starting position value in order to iterate over the list.</P>

<P>In the <font color="#008000">while</font> loop, the iteration actually takes place. The program calls the list object's <font color="#008000">GetNext()</font> member function, which requires as its single argument the position of the node to retrieve. 
The function returns a pointer to the node and sets the position to the next node in the list. When the position is <font color="#008000">NULL</font>, the program has reached the end of the list. In Listing E.8, this <font color="#008000">NULL</font> value 

⌨️ 快捷键说明

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