Because the LinkedList implements the ICollection interface, it inherits the Contains member function. As a reminder, its syntax is: public: virtual bool Contains(T value) sealed; This member function checks whether the linked list contains a node that has the value passed as argument. If that node is found, the member function returns true. Otherwise it returns false.
While the Contains() member function is used to look for a value in a linked list, it only lets you know whether the value was found. If you want to get the actual node that has that value, you can call the Find() member function. Its syntax is: public: LinkedListNode<T>^ Find(T value); When this member function is called, it starts looking for the value in the linked list. If it finds it, it returns its node. Here is an example of calling it: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e)
{
List<double> ^ values = gcnew List<double>;
values->Add(84.597);
values->Add(6.47);
values->Add(2747.06);
values->Add(282.924);
LinkedList<double> ^ numbers = gcnew LinkedList<double>(values);
LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(35.75);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(2222.06);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(4.19);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(66.18);
numbers->AddFirst(number);
if( numbers->Find(2747.06) != nullptr )
MessageBox::Show(L"2747.06 was found in the list.",
L"Linked List",
MessageBoxButtons::OK, MessageBoxIcon::Information);
else
MessageBox::Show(L"2747.06 is nowhere in the list.",
L"Linked List",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
If there is more than one node with that value, the member function returns only the first node that has that value. If the list contains more than one node that has the value but you prefer to use the last node, you can call the FindLast() member function. public: LinkedListNode<T>^ FindLast(T value);
As you can see, the LinkedList class doesn't implement the IList interface, which means it doesn't have an Item property. As we have seen with the AddLast() member function and as we will see in the next sections, each member function used to add a node is provided in two versions. One of the versions returns a LinkedListNode object. This means that, when performing an addition operation, you can get the returned value and do what you want with it. The LinkedList class implements the IEnumerable interface. This makes it possible to use for each to get to access each node. This can be done as follows: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e) { LinkedList<double> ^ numbers = gcnew LinkedList<double>; LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24); numbers->AddFirst(number); for each (double nbr in numbers) MessageBox::Show(nbr.ToString()); }
Probably the most important detail of a node is its value. To support it, the LinkedListNode class has a property named Value: public T Value { get; set; } Because this is a read-write property, you can use its write-accessory to specify or change its value. On the other hand, you can access the value of a node using this property.
As mentioned already, a linked list has a first and a last nodes (some people or documentations call them the head and the tail). To identify the first node, the LinkedList class is equippped with a read-only property named First. Its syntax is: public: property LinkedListNode<T>^ First { LinkedListNode<T>^ get(); } The last node is represented by a read-only property of the same name and that, too, is a LinkedListNode object: public: property LinkedListNode<T>^ Last { LinkedListNode<T>^ get(); } Here are examples of accessing these properties: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e) { LinkedList<double> ^ numbers = gcnew LinkedList<double>; LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(35.75); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(2222.06); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(4.19); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(66.18); numbers->AddFirst(number); MessageBox::Show(L"The value of the first node is " + numbers->First->Value); MessageBox::Show(L"The value of the last node is " + numbers->Last->Value); }
To access a node that is next to an existing node, you must first know what node is used as reference. To let you access the next node, the LinkedListNode class is equipped with a read-only property named Next: public: property LinkedListNode<T>^ Next { LinkedListNode<T>^ get (); } To let you access the node previous to an existing one, the LinkedListNode class is equipped with the read-only Previous property: public: property LinkedListNode<T>^ Previous { LinkedListNode<T>^ get (); } Remember that in both cases, you need a node as reference.
When dealing with a linked list, you have many options on how to add a new node. As mentioned already, a linked list has a first node, a last node, and one or more nodes between them. All nodes have and use some references with regards to the node(s) close to them. Based on this, when adding a new node, you have to specify whether you want it as the first node, the last node, the node before a certain node, or the node after a certain one. The LinkedList class easily supports all these operations with very little effort on your part. We saw that you could call the AddFirst() member function to add a new node. In reality, there is no such a thing as simply adding a new node to a linked list (the concept of a linked list is based on the fact that nodes are "linked"). When a linked list has just been created and it is empty, it holds a reference to a null node. There is nothing you can do with that node and you don't need to do anything with it. To start adding nodes, you have the option of setting it as the first or the last item. This would not make any difference because there is no other node in the list. After adding a node, it becomes a reference that new nodes can use. If you call the AddFirst() member function, the new node would be added before any existing node in the collection.
By contrast, you can call a member function named AddLast. It is overloaded with versions whose syntaxes are: public: LinkedListNode<T>^ AddLast(T value); void AddLast(LinkedListNode<T>^ node); When you call this member function, the value or node you pass will be added as the last item in the list. Here is an example: #include <windows.h>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Collections::Generic;
public ref class CExercise : public Form
{
public:
Button ^ btnLinkedList;
ListBox ^ lbxLinkedList;
CExercise()
{
InitializeComponent();
}
private:
void InitializeComponent()
{
btnLinkedList = gcnew Button;
btnLinkedList->Location = System::Drawing::Point(12, 12);
btnLinkedList->AutoSize = true;
btnLinkedList->Text = L"Linked List";
btnLinkedList->Click += gcnew EventHandler(this,
&CExercise::btnLinkedListClicked);
lbxLinkedList = gcnew ListBox();
lbxLinkedList->Location = System::Drawing::Point(12, 40);
lbxLinkedList->Height = 140;
Controls->Add(btnLinkedList);
Controls->Add(lbxLinkedList);
Text = L"Linked List Example";
StartPosition = FormStartPosition::CenterScreen;
}
void btnLinkedListClicked(Object ^ sender, EventArgs ^ e)
{
List<double> ^ values = gcnew List<double>;
values->Add(84.597);
values->Add(6.47);
values->Add(2747.06);
values->Add(282.924);
LinkedList<double> ^ numbers = gcnew LinkedList<double>(values);
LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(35.75);
numbers->AddFirst(number);
numbers->AddLast(2747.06);
number = gcnew LinkedListNode<double>(2222.06);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(4.19);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(66.18);
numbers->AddFirst(number);
for each (double dbl in numbers)
lbxLinkedList->Items->Add(dbl);
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Application::Run(gcnew CExercise());
return 0;
}
A linked list supports the concept of inserting a node but not exactly like traditional collections do it. With a linked list, you must add a node before or after an existing node used as reference. Behind the scenes, 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 provide 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 LinkedList class provides a member function named AddBefore. This member function is overloaded with two versions whose syntaxes are: public: LinkedListNode<T>^ AddBefore(LinkedListNode<T>^ node, T value); void AddBefore(LinkedListNode<T>^ node, LinkedListNode<T>^ newNode); In both cases, you pass the first argument as an existing node. In the first case, you directly pass the value to be positioned before node. Here is an example: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e)
{
List<double> ^ values = gcnew List<double>;
values->Add(84.597);
values->Add(6.47);
values->Add(2747.06);
values->Add(282.924);
LinkedList<double> ^ numbers = gcnew LinkedList<double>(values);
LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(35.75);
numbers->AddFirst(number);
numbers->AddLast(2747.06);
LinkedListNode<double> ^ number222206 = gcnew LinkedListNode<double>(2222.06);
numbers->AddLast(number222206);
number = gcnew LinkedListNode<double>(4.19);
numbers->AddFirst(number);
number = gcnew LinkedListNode<double>(66.18);
numbers->AddBefore(number222206, number);
number = gcnew LinkedListNode<double>(275.775);
numbers->AddLast(number);
for each (double dbl in numbers)
lbxLinkedList->Items->Add(dbl);
}
In the second case, you must pass the LinkedListNode object that will be inserted the node.
Instead of inserting a node before an existing one, you can add it after one. The approach is logically the same as inserting a node before another, except that the sequence is reversed. First, identify the node 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 an existing node, the LinkedList class is equipped with a member function named AddAfter. It comes in two versions and their syntaxes are: public: LinkedListNode<T>^ AddAfter(LinkedListNode<T>^ node, T value;); void AddAfter(LinkedListNode<T>^ node, LinkedListNode<T>^ newNode); The arguments follow the same description as the AddBefore() member function, only in reverse. Here is an example: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e) { List<double> ^ values = gcnew List<double>; values->Add(84.597); values->Add(6.47); values->Add(2747.06); values->Add(282.924); LinkedList<double> ^ numbers = gcnew LinkedList<double>(values); LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24); numbers->AddFirst(number); LinkedListNode<double> ^ number3575 = gcnew LinkedListNode<double>(35.75); numbers->AddFirst(number3575); numbers->AddLast(2747.06); LinkedListNode<double> ^ number222206 = gcnew LinkedListNode<double>(2222.06); numbers->AddLast(number222206); number = gcnew LinkedListNode<double>(4.19); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(66.18); numbers->AddBefore(number222206, number); number = gcnew LinkedListNode<double>(275.775); numbers->AddAfter(number3575, number); for each (double dbl in numbers) lbxLinkedListOriginal->Items->Add(dbl); numbers->RemoveFirst(); numbers->RemoveLast(); for each (double dbl in numbers) lbxLinkedListOther->Items->Add(dbl); }
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 LinkedList class provides the RemoveFirst() member function. Its syntax is: public void RemoveFirst(); As you can see, this member function takes no argument. When it is called:
To delete the last node, you can call the RemoveLast() member function whose syntax is: public: void RemoveFirst(); This member function follows the same logic as the RemoveFirst() member function, only in reverse. Here are examples of calling these methods: using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class LinkedListExample : Form
{
Button btnLinkedList;
ListBox lbxLinkedListOriginal;
ListBox lbxLinkedListOther;
public LinkedListExample()
{
InitializeComponent();
}
private void InitializeComponent()
{
btnLinkedList = new Button();
btnLinkedList.Location = new System.Drawing.Point(12, 12);
btnLinkedList.Text = "Linked List";
btnLinkedList.Click += new EventHandler(btnLinkedListClicked);
lbxLinkedListOriginal = new ListBox();
lbxLinkedListOriginal.Location = new System.Drawing.Point(12, 40);
lbxLinkedListOriginal.Size = new System.Drawing.Size(75, 160);
lbxLinkedListOther = new ListBox();
lbxLinkedListOther.Location = new System.Drawing.Point(100, 40);
lbxLinkedListOther.Size = new System.Drawing.Size(75, 160);
Controls.Add(btnLinkedList);
Controls.Add(lbxLinkedListOriginal);
Controls.Add(lbxLinkedListOther);
Text = "Linked List";
StartPosition = FormStartPosition.CenterScreen;
}
private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();
values.Add(84.597);
values.Add(6.47);
values.Add(2747.06);
values.Add(282.924);
LinkedList<double> numbers = new LinkedList<double>(values);
LinkedListNode<double> number = new LinkedListNode<double>(148.24);
numbers.AddFirst(number);
LinkedListNode<double> number3575 = new LinkedListNode<double>(35.75);
numbers.AddFirst(number3575);
numbers.AddLast(2747.06);
LinkedListNode<double> number222206 = new LinkedListNode<double>(2222.06);
numbers.AddLast(number222206);
number = new LinkedListNode<double>(4.19);
numbers.AddFirst(number);
number = new LinkedListNode<double>(66.18);
numbers.AddBefore(number222206, number);
number = new LinkedListNode<double>(275.775);
numbers.AddAfter(number3575, number);
foreach (double dbl in numbers)
lbxLinkedListOriginal.Items.Add(dbl);
numbers.RemoveFirst();
numbers.RemoveLast();
foreach (double dbl in numbers)
lbxLinkedListOther.Items.Add(dbl);
}
}
public class Exercise
{
public static int Main()
{
Application.Run(new LinkedListExample());
return 0;
}
}
There are two ways you can delete an item inside the collection. This can be done using the Remove() member function. It comes in two versions. If you know the exact value of the item you want to remove, you can call the follwing version of that member function: public: virtual bool Remove(T value) sealed; When calling this member function, pass the value to delete. The compiler would first look for a node that has that value:
An alternative is to delete a node based on its reference. To do this, use the following version: public: void Remove(LinkedListNode<T>^ node); When calling this member function, pass a reference to the node you want to delete. Here is an example: void btnLinkedListClicked(Object ^ sender, EventArgs ^ e) { List<double> ^ values = gcnew List<double>; values->Add(84.597); values->Add(6.47); values->Add(2747.06); values->Add(282.924); LinkedList<double> ^ numbers = gcnew LinkedList<double>(values); LinkedListNode<double> ^ number = gcnew LinkedListNode<double>(148.24); numbers->AddFirst(number); LinkedListNode<double> ^ number3575 = gcnew LinkedListNode<double>(35.75); numbers->AddFirst(number3575); numbers->AddLast(2747.06); LinkedListNode<double> ^ number222206 = gcnew LinkedListNode<double>(2222.06); numbers->AddLast(number222206); number = gcnew LinkedListNode<double>(4.19); numbers->AddFirst(number); number = gcnew LinkedListNode<double>(66.18); numbers->AddBefore(number222206, number); LinkedListNode<double> ^ number275775 = gcnew LinkedListNode<double>(275.775); numbers->AddAfter(number3575, number275775); for each (double dbl in numbers) lbxLinkedListOriginal->Items->Add(dbl); numbers->Remove(number275775); for each (double dbl in numbers) lbxLinkedListOther->Items->Add(dbl); }
Clearing a list consists of deleting all of its item. To do this, you could continuous call one of the versions of the Remove() member function. A faster solution is to call the Clear() member function. Its syntax is: public: virtual void Clear() sealed; |
|
|||||||||||||||||||||||||||||||||||
|