A linked list supports the concept of inserting a node. With a linked list, you must add a node before or after an existing node used as reference. Before inserting a node, you must identify the position where you want to put it. That is, you must identify what node you will use as reference:
In this case, you want to insert a new node before the Other Node. Behind the scenes, the reference between the two existing nodes must be brocken. Then the new node points to the Other Node as its next and the Other Node points at the New Node as its previous:
After the new node has been added, it must point to the previous node (Some Node in our example) as its previous item. The previous node (Some Node in our example) must now point to the new node as its next item:
As you may imagine, to insert a node, you must provides two pieces of information: a reference to the node that will succeed the new node, and the new node (or its value). If the referenced node is the first item of the list, the new node would become the new first object. To assist you with this operation, the CList class provides a method named InsertBefore. This method is overloaded with two versions whose syntaxes are: POSITION InsertBefore(POSITION position, ARG_TYPE newElement); The second argument is the new node to be inserted. The first argument is the position before which the new node will be inserted. Here is an example: void CExampleDlg::OnBnClickedLinkedlistBtn()
{
CList<double, double> numbers(5);
numbers.AddTail(52739.470);
numbers.AddTail(82.63);
numbers.AddHead(374.0052);
POSITION pos = numbers.AddTail(1.864);
numbers.AddTail(29374.04);
CString strNumber;
numbers.InsertBefore(pos, 991155);
pos = numbers.GetHeadPosition();
for(int i = 0; i < numbers.GetCount(); i++)
{
strNumber.Format(L"%.4f", numbers.GetNext(pos));
MessageBox(strNumber, L"Linked List");
}
}
Instead of inserting a node before a certain position, you can add it after one. The approach is logically the same as inserting a node before a position, except that the sequence is reversed. First identify the node whose position that will be used as reference. Start the process to add the new node after that one. Behind the scenes, the referenced node will point to the new node as its next and the new node will point to the existing node as its previous:
After the new node as been added, it will point to the node after it as its next. The other node will point to the new node as its previous:
If the new node is added after the last node, the new node will become the new last node. To let you insert a node after a certain position, the CList class is equipped with a member function named InsertAfter. Its syntax is: POSITION InsertAfter(POSITION position, ARG_TYPE newElement); The arguments follow the same description as the InsertBefore() member function. Here is an example: void CExampleDlg::OnBnClickedLinkedlistBtn()
{
CList<double, double> numbers(5);
numbers.AddTail(52739.470);
POSITION pos = numbers.AddTail(82.63);
numbers.AddHead(374.0052);
numbers.AddTail(1.864);
numbers.AddTail(29374.04);
CString strNumber;
numbers.InsertAfter(pos, 991.155);
pos = numbers.GetHeadPosition();
for(int i = 0; i < numbers.GetCount(); i++)
{
strNumber.Format(L"%.4f", numbers.GetNext(pos));
MessageBox(strNumber, L"Linked List");
}
}
Besides adding a new node to a linked list, probably the second most routine operation is to access one of the elements. The CList class provides various options. As mentioned already, a linked list that has at least one node has an element known as the head. That first node of a linked list occupies a position identified with the GetHeadPosition() member function. The syntax of this member function is: POSITION GetHeadPosition() const; The primary purose of this member function is to let you get to the first node of the linked list. This function returns a POSITION value that you can use as necessary.
The reverse of the head is the tail node. To give you access to it, the CList class has a function named GetTailPosition. Its syntax is: POSITION GetTailPosition() const;
If a linked list has more than one node, each node has an item next to it. To let you get to that next node, the CList class provides a function named GetNext. It has two syntaxes as fo: TYPE& GetNext(POSITION& rPosition); const TYPE& GetNext(POSITION& rPosition) const; This function takes as argument the position of the node whose next item you want to get. Here is an example of calling this function: void CExampleDlg::OnBnClickedLinkedlistBtn() { CList<double, double> numbers(5); numbers.AddTail(52739.470); numbers.AddTail(82.63); numbers.AddTail(374.0052); numbers.AddTail(1.864); numbers.AddTail(29374.04); POSITION pos = numbers.GetHeadPosition(); CString strNumber; strNumber.Format(L"The value of the node next to the head is %.34f", numbers.GetNext(pos)); AfxMessageBox(strNumber); } In the same way, you can combine these member functions to iterate through a collection to visit each member. Here is an example: void CExampleDlg::OnBnClickedLinkedlistBtn() { CList<double, double> numbers(5); numbers.AddTail(52739.470); numbers.AddTail(82.63); numbers.AddTail(374.0052); numbers.AddTail(1.864); numbers.AddTail(29374.04); CString strNumber; POSITION pos = numbers.GetHeadPosition(); for(int i = 0; i < numbers.GetCount(); i++) { strNumber.Format(L"%.4f", numbers.GetNext(pos)); AfxMessageBox(strNumber); } }
To let you get the node succeeding another node, the CList class provides a member function named GetPrev. Its syntax is: TYPE& GetPrev(POSITION& rPosition); const TYPE& GetPrev(POSITION& rPosition) const;
To let you access a node based on its position, the CList class provides the GetAt() method that is overloaded with two versions. Their syntaxes are: TYPE& GetAt(POSITION position); const TYPE& GetAt(POSITION position) const; As you can see, this method takes an index as argument and produces the node at that index. Here is an example: void CExampleDlg::OnBnClickedLinkedlistBtn() { CList<double, double> numbers(5); numbers.AddTail(52739.470); numbers.AddTail(82.63); numbers.AddTail(374.0052); numbers.AddTail(1.864); numbers.AddTail(29374.04); CString strNumber; POSITION pos = numbers.GetHeadPosition(); strNumber.Format(L"The value of the first node is %.4f", numbers.GetAt(pos)); AfxMessageBox(strNumber); }
Editing a node consists of changing its value. To support this operation, the CList class provides the SetAt() function. Its syntax is: void SetAt(POSITION pos, ARG_TYPE newElement); When calling this function, for the first argument, pass the position of the node you want to edit. The second argument is the value that must be set at that position.
As mentioned already, a linked list has a first and a last nodes, also called heal and tail. To let you access the first node, the CList class is equippped with the GetHead() member function. Its syntax is: const TYPE& GetHead() const; TYPE& GetHead(); This member function produces the first node of the collection. Here is an example of using it: void CExampleDlg::OnBnClickedLinkedlistBtn()
{
CList<double, double> numbers(5);
numbers.AddTail(52739.470);
numbers.AddTail(82.63);
numbers.AddTail(374.0052);
numbers.AddTail(1.864);
numbers.AddTail(29374.04);
CString strNumber;
strNumber.Format(L"The value of the first node is %.4f", numbers.GetTail());
AfxMessageBox(strNumber);
}
The opposite of the first node is the last. To get it, you can call the GetTail() member function whose syntax is: TYPE& GetTail(); const TYPE& GetTail() const;
With a collection that contains elements already, you may want to look for a particular nodes. One of the member functions you can use is named Find. Its syntax is: POSITION Find(ARG_TYPE searchValue, POSITION startAfter = NULL) const; This function takes one required and one option arguments. The searchValue is the node to look for in the collection. The second argument is optional. Here is an example of calling this function: void CExampleDlg::OnBnClickedLinkedlistBtn() { CList<double, double> numbers(5); numbers.AddTail(52739.470); numbers.AddTail(82.63); numbers.AddTail(374.0052); numbers.AddTail(1.864); numbers.AddTail(29374.04); POSITION index = numbers.Find(374.0052); if( index != NULL ) AfxMessageBox(L"374.0052 exists in the list."); else AfxMessageBox(L"374.0052 was not found in the list."); } If you omit the second argumen, the compiler would start looking for the searchValue from the beginning of the list. Otherwise, you can ask the compiler to start looking at a specific position. To provide this information, pass the second argument. The CList class provides an alternative to the Find() member function. It is called FindIndex and its syntax is POSITION FindIndex(INT_PTR nIndex) const;
When it comes time to delete a node, you have many options, such as deleting the first or the last node of the list. To let you delete the first node, the CList class provides the RemoveHead() member function. Its syntax is: TYPE RemoveHead(); As you can see, this member function takes no argument. When it is called, it deletes the first node and returns it. Here are examples of calling it: void CExampleDlg::OnBnClickedLinkedlistBtn()
{
CList<double, double> numbers(5);
numbers.AddTail(52739.470);
numbers.AddTail(82.63);
numbers.AddHead(374.0052);
numbers.AddTail(1.864);
numbers.AddTail(29374.04);
CString strNumber;
for(int i = 0; i < numbers.GetCount(); i++)
{
double number = numbers.RemoveHead();
strNumber.Format(L"The number that was deleted is %.4f", number);
MessageBox(strNumber, L"Linked List");
}
}
To delete the last node, you can call the RemoveTail() method whose syntax is: TYPE RemoveTail(); This member function deletes the last node and returns it.
To delete a node using its position, you can call the RemoveAt() member function. Its syntax is: void RemoveAt(POSITION position); When calling this method, pass the position as argument.
Clearing a list consists of deleting all of its item. To do this, you could continuous call one of the remove-based member functions. A faster solution is to call RemoveAll(). Its syntax is: void RemoveAll(); |
|
|||||||||||||||||||||||||||||||||||||||||||||||
|