Introduction

A linked list is a collection with the following rules:

• Each item in the list is called a node (this is not an actual rule but a habit or a suggestion)

• If the list is empty, it may have one item referred to as null
• If the list was just empty, when a new node is added, that node becomes the first and last item
• If the list has only one node, that node represents the first and the last item

• If the list contains at least one node, whenever a new node is added, that new node is positioned as the last in the list. This is because the new node is added next to the existing node
• If the list contains more than one node, each item holds a reference to the object next to it

In reality, there are various types of linked lists. A singly linked list is a one-way directional list where each item points (only) to the item next to it (in a somewhat right direction). The description we just gave is conform to a singly linked list. Another type is the doubly linked list:

• Each item in the list is called a node (again, this is not a real rule)
• If the list is empty, it may have one item referred to as null or two items referred to as nulls
• If a new node is added to an empty list, that node is added as the first in the collection
• If the list contains one node, that node has two references to itself: one reference as the next and the other reference as its previous
• If the list contains two nodes:
• One node is the first. That first node holds a reference to the other node as its next
• The other node is the last. That last node holds a reference to the first node as its previous item
• If the list contains more than two nodes:
• The first node holds a reference to the next node (in the right direction)
• The last node holds a reference to the node previous to it (in the left direction)
• Each node holds two references: one reference to the previous node and one reference to the next node

The last type of linked list is called a circular linked list. This list is primarily created as either a singly linked list or a doubly linked list:

• In a circular singly linked list, the list primarily follows the rules of a singly linked list. then:
• If the list has two nodes, the first node holds a reference to the other node as its next and its previous node
• If the list has more than one node:
• The first node holds a reference to the last node as its previous object
• The last node holds a reference to first node as its next

• In a doubly linked list, the list includes the rules of the doubly linked list and combines with those of the circular singly linked list:
• The first node has a reference to the last node as its previous node
• The last node has a reference to the first node as its next node
• Each node has two references: its previous and its next nodes

1. Start Microsoft Visual Studio
2. To create a new application, on the main menu, click FILE -> New Project...
3. In the middle list, make sure Windows Forms Application is selected and change the Name to AltairRealtors1
4. Click OK
5. When asked whether you want to save the current solution, click Yes
6. To create a new class, on the main menu, click PROJECT -> Add Class...
7. Set the Name to RealEstateProperty
9. Change the file as follows:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AltairRealtors1
{
[Serializable]
public class RealEstateProperty
{
public string PropertyNumber { get; set; }
public string PropertyType   { get; set; }
public string Address        { get; set; }
public string City           { get; set; }
public string State          { get; set; }
public string ZIPCode        { get; set; }
public short  Stories        { get; set; }
public int    YearBuilt      { get; set; }
public short  Bedrooms       { get; set; }
public float  Bathrooms      { get; set; }
public string Condition      { get; set; }
public string SaleStatus     { get; set; }
public double MarketValue    { get; set; }
public string PictureFile    { get; set; }

// To determine that two properties are the same,
// we will test only the property number.
// We assume that if two properties have the same number,
// then it is the same property
public override bool Equals(object obj)
{
RealEstateProperty rep = (RealEstateProperty)obj;

if (rep.PropertyNumber == PropertyNumber)
return true;
else
return false;
}

// To avoid a compiler warning
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}```
10. In the Solution Explorer, right-click Form1.cs and click Rename
11. Type AltairRealtors.cs and press Enter twice
12. Display the form and, from the Toolbox, add a list view to the form
13. Right-click the list view on the form and click Edit Columns...
14. Create the columns as follows:

 (Name) Text TextAlign Width colPropertyNumber Property # 65 colCity City 75 colStories Stories Right 45 colYearBuilt Year Right 40 colBedrooms Beds Right 38 colBathrooms Baths Right 40 colCondition Condition 80 colSaleStatus Status 70 colMarketValue Value Right 75
15. Click OK
16. Design the form as follows:

 Control (Name) Anchor BorderStyle SizeMode Text ListView lvwProperties Top, Bottom, Left, Right Button btnNewProperty Bottom, Left New Real Estate Property... PictureBox pbxPicture Bottom, Right FixedSingle Zoom Button btnClose Bottom, Right Close
Form
Text: Altair Realtors - Properties Listing
StartPosition: CenterScreen
17. Right-click the form and click Edit Groups...
18. Create the groups as follows:

 Header Name Condominium lvgCondominium Townhouse lvgTownhouse Single Family lvgSingleFamily
19. Click OK

Although you can create a linked list collection class from scratch, to assist you, the .NET Framework provides a class named LinkedList and that is a member of the System.Collections.Generic namespace. LinkedList is a generic collection class with three constructors. The default constructor allows you to create an empty linked list. Here is an example of using it:

```using System;
using System.Windows.Forms;
using System.Collections.Generic;

{

{
InitializeComponent();
}

private void InitializeComponent()
{

StartPosition = FormStartPosition.CenterScreen;
}

private void btnLinkedListClicked(object sender, EventArgs e)
{
}
}

public class Exercise
{
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
return 0;
}
}```

Another constructor allows you to create a linked using an existing list. Its syntax is:

`public LinkedList(IEnumerable<T> collection);`

The argument can be a variable from any class that implements the IEnumerable<T> interface. Here is an example of using this second constructor:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

}```
 Fundamental Operations on a Linked List

 Introduction to a Node as an Object

As mentioned already, it is a tradition to call an item of a linked list a node. To define a node as a true object, the .NET Framework provides the LinkedListNode sealed class:

`public sealed class LinkedListNode<T>`

This class has only properties, no methods.

 The Number of Nodes of a List

The LinkedList class starts as follows:

```public class LinkedList<T> : ICollection<T>,
IEnumerable<T>,
ICollection, IEnumerable,
ISerializable,
IDeserializationCallback```

As you can see, the LinkedList class implements the ICollection interface. This gives it a Count property that produces the number of nodes. Here is an example of accessing it:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

MessageBox.Show("There are " + numbers.Count.ToString() + " numbers in the list",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}```

The primary operation to perform on a linked list is to add a new node to it. To support it, the LinkedList class is equipped with various methods. One of them is named AddLast that is overloaded in two versions. One of them uses the following syntax:

`public LinkedListNode<T> AddLast(T value);`

This method expects the new value as argument. Here is an example of calling it:

```using System;
using System.Windows.Forms;
using System.Collections.Generic;

{

{
InitializeComponent();
}

private void InitializeComponent()
{

StartPosition = FormStartPosition.CenterScreen;
}

private void btnLinkedListClicked(object sender, EventArgs e)
{

}
}

public class Exercise
{
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
return 0;
}
}```

Another version of this method is:

`public void AddLast(LinkedListNode<T> node);`

This version expects a LinkedListNode object as argument. Here is an example of calling it:

```private void btnLinkedListClicked(object sender, EventArgs e)
{

}```

In the same way, you can use this method to add new items. Here are examples:

```private void btnLinkedListClicked(object sender, EventArgs e)
{

}```
 Looking for a Node

 Introduction

Because the LinkedList implements the ICollection interface, it inherits the Contains method. As a reminder, its syntax is:

`public bool Contains(T value);`

This method checks whether the linked list contains the (a) node that has the value passed as argument. If that node is found, the method returns true. Otherwise it returns false. Here is an example of calling it:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

if( numbers.Contains(2222.06) == true )
MessageBox.Show("The list contains 2222.06.",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("There is no such a number in the list.",
MessageBoxButtons.OK, MessageBoxIcon.Information);```

This method works only if the type of the node is able to perform the comparison for equality. If you are using values of primitive types (int, char, double, DateTime, etc) or string, the method would work fine. If you are using your own class, make sure you override the Equals() method.

 Finding a Node

While the Contains() method 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() method. Its syntax is:

`public LinkedListNode<T> Find(T value);`

When this method is called, it starts looking for the value in the linked list. If it finds it, it returns its node. If there is more than one node with that value, the method returns only the first node that has that value. Here is an example of calling this method:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

if( numbers.Find(2747.06) != null )
MessageBox.Show("2747.06 was found in the list.",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("2747.06 is nowhere in the list.",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}```

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() method.

`public LinkedListNode<T> FindLast(T value);`

Once again, remember that these two methods are ready to work on primitive types. If you are using your own class for the type of node, you should (must) override the Equals() method.

 Getting Each Node

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() method and as we will see in the next sections, each method 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 class implements the IEnumerable interface. This makes it possible to use foreach to get to access each node. This can be done as follows:

```private void btnLinkedListClicked(object sender, EventArgs e)
{

foreach (double nbr in numbers)
MessageBox.Show(nbr.ToString());
}```
 Topic Applied: Getting Each Node of a Linked List
1. Double-click an unoccupied area of the Altair Realtors form
2. Change the file as follows:
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

namespace AltairRealtors1
{
public partial class AltairRealtors : Form
{
ListViewItem lviSelected;

public AltairRealtors()
{
InitializeComponent();
}

private void ShowProperties()
{
BinaryFormatter bfmProperty = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

// Make sure the file exists
if (File.Exists(strFileName) == true)
{
// if so, create a file stream
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
// If some properties were created already,
// get them and store them in the collection

// First, empty the list view
lvwProperties.Items.Clear();
ListViewItem lviProperty = null;

// Visit each property in the collection and add it to the list view
foreach (RealEstateProperty house in properties)
{
if (house.PropertyType.Equals("Condominium"))
lviProperty = new ListViewItem(house.PropertyNumber, lvwProperties.Groups[0]);
else if (house.PropertyType.Equals("Townhouse"))
lviProperty = new ListViewItem(house.PropertyNumber, lvwProperties.Groups[1]);
else // if (house.PropertyType.Equals("Single Family"))
lviProperty = new ListViewItem(house.PropertyNumber, lvwProperties.Groups[2]);

}

} // Close the file stream
}
}

private void AltairRealtors_Load(object sender, EventArgs e)
{
Directory.CreateDirectory("C:\\Microsoft Visual C# Application Design\\Altair Realtors");
ShowProperties();
lviSelected = new ListViewItem();
}
}
}```
 Navigating Among the Nodes

 The First and the Last Nodes

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 LinkedListNode<T> First { get; }`

The last node is represented by a read-only property of the same name and that, too, is a LinkedListNode object:

`public LinkedListNode<T> Last { get; }`

Here are examples of accessing these properties:

```private void btnLinkedListClicked(object sender, EventArgs e)
{

MessageBox.Show("The value of the first node is " + numbers.First.Value);
MessageBox.Show("The value of the last node is " + numbers.Last.Value);
}```
 The Next and Previous Nodes

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 LinkedListNode<T> Next { get; }`

To let you access the node previous to an existing one, the LinkedListNode class is equipped with the read-only Previous property:

`public LinkedListNode Previous { get; }`

Remember that in both cases, you need a node as reference.

 Creating Nodes

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() method to add a new node. In reality, there is no such a thing as simply adding a new node to a linked list. 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() method, the new node would be added before any existing node in the collection.

1. To create a new form, on the main menu, click PROJECT -> Add Windows Form...
2. Set the Name to PropertyEditor
4. In the Dialogs section of the Toolbox, click OpenFileDialog
5. Click the form
6. In the Properties window, change its characteristics as follows:
(Name):        dlgPicture
DefaultExt:  jpg
Filter:           JPEG Files (*.jpg,*.jpeg)|*.jpg
Title:            Select House Picture
7. Design the form as follows:

 Control (Name) DropDownStyle Text Items Modifiers Other Properties Label Property Type: ComboBox cbxPropertyTypes DropDownList UnknownTownhouseSingle Family Condominium Public Label Property #: TextBox txtPropertyNumber Public Label Address: TextBox txtAddress Public Label City: TextBox txtCity Public Label State: ComboBox cbxStates DropDownList AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY Public Label ZIP Code: TextBox txtZIPCode Public Label Stories: TextBox txtStories Public Label Year Built: TextBox txtYearBuilt Public Label Condition: ComboBox cbxConditions DropDownList ExcellentGood ShapeNeeds Fixing Public Label Bedrooms: TextBox txtBedrooms 0 Public Label Bathrooms: TextBox txtBathrooms 0.00 Public Label Market Value: TextBox txtMarketValue 0.00 Public Label Sale Status: ComboBox cbxSaleStatus DropDownList SoldAvailableUnspecified Public Button btnPicture Picture... PictureBox pbxProperty BorderStyle: FixedSingleSizeMode: Zoom Button btnOK OK DialogResult: OK Button btnCancel Cancel DialogResult: Cancel
Form
FormBorderStyle: FixedDialog
Text: Altair Realtors - Property Editor
StartPosition: CenterScreen
AcceptButton: btnOK
CancelButton: btnCancel
MaximizeBox: False
MinimizeBox: False
8. Double-click an unoccupied area of the form
10. Change the file as follows:
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AltairRealtors1
{
public partial class PropertyEditor : Form
{
public bool pictureChanged;
public string pictureFile;

public PropertyEditor()
{
InitializeComponent();
}

private void PropertyEditor_Load(object sender, EventArgs e)
{
pictureChanged = false;
pictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg";
}

private void btnPicture_Click(object sender, EventArgs e)
{
if (dlgPicture.ShowDialog() == DialogResult.OK)
{
pbxProperty.Image = Image.FromFile(dlgPicture.FileName);
pictureFile = dlgPicture.FileName;
pictureChanged = true;
}
}
}
}```
11. Display the Altair Realtors form and, on the form, double-click the New Real Estale Property button
12. Implement its event as follows:
```private void btnNewProperty_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
BinaryFormatter bfmProperty = new BinaryFormatter();

Random rndNumber = new Random(DateTime.Now.Millisecond);
int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + number2;

editor.txtPropertyNumber.Text = propNumber;

// Get a reference to the file that holds the properties
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

// First check if the file was previously created
if (File.Exists(strFileName) == true)
{
// If the list of properties exists already,
// get it and store it in a file stream
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
// Store the list of properties in the collection
} // Close the file stream
}

if (editor.ShowDialog() == DialogResult.OK)
{
RealEstateProperty prop = new RealEstateProperty();

prop.PropertyNumber = editor.txtPropertyNumber.Text;
prop.PropertyType = editor.cbxPropertyTypes.Text;
prop.City = editor.txtCity.Text;
prop.State = editor.cbxStates.Text;
prop.ZIPCode = editor.txtZIPCode.Text;
prop.Stories = short.Parse(editor.txtStories.Text);
prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
prop.Condition = editor.cbxConditions.Text;
prop.SaleStatus = editor.cbxSaleStatus.Text;
prop.MarketValue = double.Parse(editor.txtMarketValue.Text);
if (!editor.pictureFile.Equals(""))
{
FileInfo flePicture = new FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" +
editor.txtPropertyNumber.Text + flePicture.Extension, true);
prop.PictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" +
editor.txtPropertyNumber.Text + flePicture.Extension;
}
else
prop.PictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg";

// Add the property in the collection

// Create a file stream to hold the list of properties
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
// Serialize the list of properties
bfmProperty.Serialize(stmProperties, properties);
} // Close the file stream

// Show the list of properties
ShowProperties();
}
}```
13. Return to the Altair Realtors form and click the list view
14. In the Properties window, click Events and double-click ItemSelectionChanged
15. Implement the event as follows:
```private void lvwProperties_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + e.Item.SubItems[0].Text + ".jpg";

if (File.Exists(strFileName))
pbxProperty.Image = Image.FromFile(strFileName);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");
}```
16. Press Ctrl + F5 to execute
17. Click the New Real Estate Property button and create a property as follows:

 Property Type Address City State ZIP Code Stories Year Built Condition Bedrooms Bathrooms Market Value Status Single Family 4288 Lucow Drive Rockville MD 20856 2 1988 Excellent 3 2.5 665580 Available

18. Click OK
19. Copy the following picture and paste (or save) it in the Altair Realtors folder (or the folder that contains the file for the houses of this project):

20. Create the following properties

 Property Type Address City State ZIP Code Stories Year Built Condition Bedrooms Bathrooms Market Value Status Townhouse 719 Beanson Road Arlington VA 22201 4 1995 Good Shape 4 3.5 580795 Sold Condominium 6662 16th Street NW Washington DC 20012 6 1984 Good Shape 1 1 325775 Available

22. On the Altair Realtors form, click the list view
23. In the Events section of the Properties window, double-click DoubleClick
24. Implement its event as follows:
```private void lvwProperties_DoubleClick(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
RealEstateProperty house = new RealEstateProperty();
BinaryFormatter bfmProperties = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

if ((lvwProperties.SelectedItems.Count == 0) || (lvwProperties.SelectedItems.Count > 1))
return;

using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
}

// Get the property that the user double-clicked
ListViewItem lviProperty = lvwProperties.SelectedItems[0];
foreach (RealEstateProperty prop in properties)
if (prop.PropertyNumber == lviProperty.Text)
house = prop;

// Prepare to fill the editor with the values of the property the user double-clicked
editor.txtPropertyNumber.Text = house.PropertyNumber;
editor.cbxPropertyTypes.Text = house.PropertyType;
editor.txtCity.Text = house.City;
editor.cbxStates.Text = house.State;
editor.txtZIPCode.Text = house.ZIPCode;
editor.txtStories.Text = house.Stories.ToString();
editor.txtYearBuilt.Text = house.YearBuilt.ToString();
editor.txtBedrooms.Text = house.Bedrooms.ToString();
editor.txtBathrooms.Text = house.Bathrooms.ToString("F");
editor.cbxConditions.Text = house.Condition;
editor.cbxSaleStatus.Text = house.SaleStatus;
editor.txtMarketValue.Text = house.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + house.PropertyNumber + ".jpg";

if (File.Exists(strPictureFile))
editor.pbxProperty.Image = Image.FromFile(strPictureFile);
else
editor.pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

// Disable the property number so the user cannot change it
editor.txtPropertyNumber.Enabled = false;

editor.ShowDialog();

// Show the list of properties
ShowProperties();
}```
25. Press Ctrl + F5 to execute the application
26. Double-click one of the records in the list view

By contrast, you can call a method named AddLast. It is overloaded with versions whose syntaxes are:

```public LinkedListNode<T> AddLast(T value);

When you call this method, the value or node you pass will be added as the last item in the list. Here is an example:

```using System;
using System.Windows.Forms;
using System.Collections.Generic;

{

{
InitializeComponent();
}

private void InitializeComponent()
{

StartPosition = FormStartPosition.CenterScreen;
}

private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

foreach (double dbl in numbers)
}
}

public class Exercise
{
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
return 0;
}
}```

 Nodes Maintenance in a Linked List

 Inserting a Node Before a Referenced One

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 method named AddBefore. This method is overloaded with two versions whose syntaxes are:

```public void AddBefore(LinkedListNode<T> node, LinkedListNode<T> newNode);

In both cases, you pass a first argument as an existing node. In the first case, you must pass the LinkedListNode object that will be inserted the node. Here is an example:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

foreach (double dbl in numbers)
}```

In the second version, you directly pass the value to be positioned before node.

 Inserting a Node After a Referenced One

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 method named AddAfter. It comes in two versions and their syntaxes are:

```public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode);

The arguments follow the same description as the AddBefore() method, only in reverse. Here is an example:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

foreach (double dbl in numbers)
}```

 Topic Applied: Inserting Nodes
1. Display the Altair Realtors - Properties Listing form
2. In the Menus & Toolbars section of the Toolbox, click ContextMenuStrip and click the form
3. Click the menu items as follows:

 (Name) Enabled Text mnuNewProperty New Property... mnuEditProperty False Edit Property... mnuInsertBefore False Insert Property... mnuInsertAfter False Insert After
4. Under the form, click the context menu strip
5. In the Properties window, change its name to cmsProperties
6. On the form, click the list view
7. In the Properties window, set its ContextMenuStrip to cmsProperties
8. Under the form, click cmsProperties
9. Under ContextMenuStrip, double-click New Property...
10. Implement the event as follows:
```private void mnuNewProperty_Click(object sender, EventArgs e)
{
btnNewProperty_Click(sender, e);
}```
11. Locate the ItemSelectionChanged event and change its implementation as follows:
```private void lvwProperties_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + e.Item.SubItems[0].Text + ".jpg";

if (lvwProperties.SelectedItems.Count == 1)
lviSelected = lvwProperties.SelectedItems[0];

if (File.Exists(strFileName))
pbxProperty.Image = Image.FromFile(strFileName);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

if (lvwProperties.SelectedItems.Count == 1)
{
mnuInsertBefore.Enabled = true;
mnuEditProperty.Enabled = true;
mnuInsertAfter.Enabled = true;
}
else
{
mnuInsertBefore.Enabled = false;
mnuEditProperty.Enabled = false;
mnuInsertAfter.Enabled = false;
}
}```
On the form, double-click Edit properties
13. Implement its event as follows:
```private void mnuEditProperty_Click(object sender, EventArgs e)
{
lvwProperties_DoubleClick(sender, e);
}```
14. Return to the form and, on the form, double-click Insert Before...
15. Implement the event as follows:
```private void mnuInsertBefore_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
RealEstateProperty rep = new RealEstateProperty();
BinaryFormatter bfmProperties = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

if (File.Exists(strFileName) == true)
{
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
}
}

string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

Random rndNumber = new Random(DateTime.Now.Millisecond);
int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + number2;

editor.txtPropertyNumber.Text = propNumber;
editor.cbxPropertyTypes.Enabled = false;

if (nodProperty != null)
{
if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
RealEstateProperty prop = new RealEstateProperty();

prop.PropertyNumber = editor.txtPropertyNumber.Text;
prop.PropertyType = editor.cbxPropertyTypes.Text;
prop.City = editor.txtCity.Text;
prop.State = editor.cbxStates.Text;
prop.ZIPCode = editor.txtZIPCode.Text;
prop.Stories = short.Parse(editor.txtStories.Text);
prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
prop.Condition = editor.cbxConditions.Text;
prop.SaleStatus = editor.cbxSaleStatus.Text;
prop.MarketValue = double.Parse(editor.txtMarketValue.Text);
if (!editor.pictureFile.Equals(""))
{
FileInfo flePicture = new FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension);
prop.PictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension;
}
else
prop.PictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg";

// Insert the property before the currently selected node

// Create a file stream to hold the list of properties
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
bfmProperties.Serialize(stmProperties, properties);
}

// Show the list of properties
ShowProperties();
}
}
}```
16. Return to the form and, on the form, double-click Insert After...
17. Implement the event as follows:
```private void mnuInsertAfter_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
RealEstateProperty rep = new RealEstateProperty();
BinaryFormatter bfmProperties = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

if (File.Exists(strFileName) == true)
{
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
}
}

string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

Random rndNumber = new Random(DateTime.Now.Millisecond);
int number1 = rndNumber.Next(100, 999);
int number2 = rndNumber.Next(100, 999);
string propNumber = number1 + "-" + number2;

editor.txtPropertyNumber.Text = propNumber;
editor.cbxPropertyTypes.Enabled = false;

if (nodProperty != null)
{
if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
RealEstateProperty prop = new RealEstateProperty();

prop.PropertyNumber = editor.txtPropertyNumber.Text;
prop.PropertyType = editor.cbxPropertyTypes.Text;
prop.City = editor.txtCity.Text;
prop.State = editor.cbxStates.Text;
prop.ZIPCode = editor.txtZIPCode.Text;
prop.Stories = short.Parse(editor.txtStories.Text);
prop.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
prop.Bedrooms = short.Parse(editor.txtBedrooms.Text);
prop.Bathrooms = float.Parse(editor.txtBathrooms.Text);
prop.Condition = editor.cbxConditions.Text;
prop.SaleStatus = editor.cbxSaleStatus.Text;
prop.MarketValue = double.Parse(editor.txtMarketValue.Text);
if (!editor.pictureFile.Equals(""))
{
FileInfo flePicture = new FileInfo(editor.pictureFile);
flePicture.CopyTo("C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension);
prop.PictureFile = "C:\\Altair Realtors1\\" +
editor.txtPropertyNumber.Text +
flePicture.Extension;
}
else
prop.PictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg";

// Insert the property before the currently selected node

// Create a file stream to hold the list of properties
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
bfmProperties.Serialize(stmProperties, properties);
}

// Show the list of properties
ShowProperties();
}
}
}```
18. To execute, press Ctrl + F5
19. Create the following properties (let the application generate the property number) (the new records are in bold):

 Property Type Address City State ZIP Code Stories Year Built Condition Bedrooms Bathrooms Market Value Status Single Family 4288 Lucow Drive Rockville MD 20856 2 1988 Excellent 3 2.5 665580 Available Single Family 10202 Lockwood Ave Silver Spring MD 20906 3 2002 Excellent 5 3.5 675880 Sold Single Family 8604 L Street NE Washington DC 20016 2 1988 Excellent 3 2.5 465580 Available Single Family 6218 Mandarin Road McLean VA 22101 3 1992 Excellent 5 3.5 785680 Available Single Family 508 Capitol Street Alexandria VA 22311 3 2006 Excellent 5 3.5 852500 Sold Townhouse 719 Beanson Road Arlington VA 22201 4 1995 Good Shape 4 3.5 580795 Sold Townhouse 4220 Melmann Drive Baltimore MD 21206 3 1982 Good Shape 3 1 275970 Available Townhouse 2428 Wisconsin Ave Washington DC 20008 2 1975 Good Shape 3 1.5 385600 Sold Condominium 6662 16th Street NW Washington DC 20012 6 1984 Good Shape 1 1 325775 Available Condominium 2644 Swanson Drive Charleston WV 25301 1 2004 Good Shape 2 2 225450 Available Condominium 808D Green Tree Rd Arbovale WV 24915 3 2006 Excellent 1 2 216925 Available
 The Value of a Node

Probably the most important aspect 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.

 Topic Applied: Using the Value of a Node
1. To create a new form, on the main menu, click PROJECT -> Add Windows Form...
2. Set the Name to PropertiesReview
4. Design the form as follows:

 Control (Name) DropDownStyle Text Other Properties Label Property #: TextBox txtPropertyNumber Label Property Type: TextBox txtPropertyType Label Address: TextBox txtAddress Label City: TextBox txtCity Label State: TexBox txtState Label ZIP Code: TextBox txtZIPCode Label Stories: TextBox txtStories Label Year Built: TextBox txtYearBuilt Label Condition: TextBox txtCondition Label Bedrooms: TextBox txtBedrooms 0 Label Bathrooms: TextBox txtBathrooms 0.00 Label Market Value: TextBox txtMarketValue 0.00 Label Sale Status: TextBox txtStatus PictureBox pbxProperty Anchor: Top, Bottom, Left, RightBorderStyle: FixedSingleSizeMode: Zoom Button btnClose Close Anchor: Bottom, Left Button btnFirst First Anchor: Bottom, Right Label lblRecordNumber 000 of 000 Anchor: Bottom, Right Button btnPrevious Previous Anchor: Bottom, Right Button btnLast Last Anchor: Bottom, Right
5. Double-click an unoccupied area of the form
6. Return to the Property Review form and double-click the First button
7. Change the file as follows:
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace AltairRealtors1
{
public partial class PropertiesReview : Form
{
int index;

public PropertiesReview()
{
InitializeComponent();
}

private void PropertiesReview_Load(object sender, EventArgs e)
{
index = 1;
BinaryFormatter bfmProperty = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

if (File.Exists(strFileName) == true)
{
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{

btnFirst_Click(sender, e);
}
}
}

private void btnFirst_Click(object sender, EventArgs e)
{
index = 1;
nodCurrent = properties.First;

txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
txtPropertyType.Text = nodCurrent.Value.PropertyType;
txtCity.Text = nodCurrent.Value.City;
txtState.Text = nodCurrent.Value.State;
txtZIPCode.Text = nodCurrent.Value.ZIPCode;
txtStories.Text = nodCurrent.Value.Stories.ToString();
txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + txtPropertyNumber.Text + ".jpg";

if (File.Exists(strPictureFile))
pbxProperty.Image = Image.FromFile(strPictureFile);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

lblRecordNumber.Text = "1 of " + properties.Count.ToString();
}
}
}```
8. Return to the Property Review form and double-click the Previous button
9. Implement its event as follows:
```private void btnPrevious_Click(object sender, EventArgs e)
{
index--;

if (index <= 1)
btnFirst_Click(sender, e);
else
{
nodCurrent = nodCurrent.Previous;

txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
txtPropertyType.Text = nodCurrent.Value.PropertyType;
txtCity.Text = nodCurrent.Value.City;
txtState.Text = nodCurrent.Value.State;
txtZIPCode.Text = nodCurrent.Value.ZIPCode;
txtStories.Text = nodCurrent.Value.Stories.ToString();
txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + txtPropertyNumber.Text + ".jpg";

if (File.Exists(strPictureFile))
pbxProperty.Image = Image.FromFile(strPictureFile);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

lblRecordNumber.Text = index.ToString() + " of " + properties.Count.ToString();
}
}```
10. Return to the Property Review form and double-click the Next button
11. Return to the Property Review form and double-click the Last button
12. Implement both events as follows:
```private void btnNext_Click(object sender, EventArgs e)
{
index++;

if (index >= properties.Count)
btnLast_Click(sender, e);
else
{
nodCurrent = nodCurrent.Next;

txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
txtPropertyType.Text = nodCurrent.Value.PropertyType;
txtCity.Text = nodCurrent.Value.City;
txtState.Text = nodCurrent.Value.State;
txtZIPCode.Text = nodCurrent.Value.ZIPCode;
txtStories.Text = nodCurrent.Value.Stories.ToString();
txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + txtPropertyNumber.Text + ".jpg";

if (File.Exists(strPictureFile))
pbxProperty.Image = Image.FromFile(strPictureFile);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

lblRecordNumber.Text = index.ToString() + " of " + properties.Count.ToString();
}
}

private void btnLast_Click(object sender, EventArgs e)
{
index = properties.Count;
nodCurrent = properties.Last;

txtPropertyNumber.Text = nodCurrent.Value.PropertyNumber;
txtPropertyType.Text = nodCurrent.Value.PropertyType;
txtCity.Text = nodCurrent.Value.City;
txtState.Text = nodCurrent.Value.State;
txtZIPCode.Text = nodCurrent.Value.ZIPCode;
txtStories.Text = nodCurrent.Value.Stories.ToString();
txtYearBuilt.Text = nodCurrent.Value.YearBuilt.ToString();
txtBedrooms.Text = nodCurrent.Value.Bedrooms.ToString();
txtBathrooms.Text = nodCurrent.Value.Bathrooms.ToString("F");
txtCondition.Text = nodCurrent.Value.Condition;
txtSaleStatus.Text = nodCurrent.Value.SaleStatus;
txtMarketValue.Text = nodCurrent.Value.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + nodCurrent.Value.PropertyNumber + ".jpg";

if (File.Exists(strPictureFile))
pbxProperty.Image = Image.FromFile(strPictureFile);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

lblRecordNumber.Text = properties.Count.ToString() + " of " + properties.Count.ToString();
}```
13. Return to the Properties Review form and double-click the Close button
14. Type Close();
15. Display the Altair Realtors form
16. On the right side of the New Real Estate Property button, add a new button and set its characteristics as follows:
(Name): btnReviewProperties
Text:     Review Properties...

17. Double-click the Review Properties button and implement its event as follows:
```private void btnReviewProperties_Click(object sender, EventArgs e)
{
PropertiesReview pr = new PropertiesReview();

pr.ShowDialog();
}```
18. Save all
 Topic Applied: Updating a Node
1. Change the DoubleClick event of the list view as follows:
```private void lvwProperties_DoubleClick(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
RealEstateProperty house = new RealEstateProperty();
BinaryFormatter bfProperties = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

if ((lvwProperties.SelectedItems.Count == 0) || (lvwProperties.SelectedItems.Count > 1))
return;

using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
}

// Get the property that the user double-clicked
ListViewItem lviProperty = lvwProperties.SelectedItems[0];
// Get the property number the user double-clicked
house.PropertyNumber = lviProperty.Text;

// Find the property in the list

// Prepare to fill the editor with the values of the property the user double-clicked
editor.txtPropertyNumber.Text = nodProperty.Value.PropertyNumber;
editor.cbxPropertyTypes.Text = nodProperty.Value.PropertyType;
editor.txtCity.Text = nodProperty.Value.City;
editor.cbxStates.Text = nodProperty.Value.State;
editor.txtZIPCode.Text = nodProperty.Value.ZIPCode;
editor.txtStories.Text = nodProperty.Value.Stories.ToString();
editor.txtYearBuilt.Text = nodProperty.Value.YearBuilt.ToString();
editor.txtBedrooms.Text = nodProperty.Value.Bedrooms.ToString();
editor.txtBathrooms.Text = nodProperty.Value.Bathrooms.ToString("F");
editor.cbxConditions.Text = nodProperty.Value.Condition;
editor.cbxSaleStatus.Text = nodProperty.Value.SaleStatus;
editor.txtMarketValue.Text = nodProperty.Value.MarketValue.ToString("F");

string strPictureFile = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + house.PropertyNumber + ".jpg";

if (File.Exists(strPictureFile))
editor.pbxProperty.Image = Image.FromFile(strPictureFile);
else
editor.pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

// Disable the property number so the user cannot change it
editor.txtPropertyNumber.Enabled = false;

// Show the property editor
if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// For each value that has changed in the property, update its nnode
nodProperty.Value.PropertyType = editor.cbxPropertyTypes.Text;
nodProperty.Value.City = editor.txtCity.Text;
nodProperty.Value.State = editor.cbxStates.Text;
nodProperty.Value.ZIPCode = editor.txtZIPCode.Text;
nodProperty.Value.Stories = short.Parse(editor.txtStories.Text);
nodProperty.Value.YearBuilt = int.Parse(editor.txtYearBuilt.Text);
nodProperty.Value.Bedrooms = short.Parse(editor.txtBedrooms.Text);
nodProperty.Value.Bathrooms = float.Parse(editor.txtBathrooms.Text);
nodProperty.Value.Condition = editor.cbxConditions.Text;
nodProperty.Value.SaleStatus = editor.cbxSaleStatus.Text;
nodProperty.Value.MarketValue = double.Parse(editor.txtMarketValue.Text);
nodProperty.Value.PictureFile = editor.pictureFile;

using (FileStream stmProperties = new FileStream(strFileName,
FileMode.OpenOrCreate,
{
bfProperties.Serialize(stmProperties, properties);
}
}

// Show the list of properties
ShowProperties();
}```
2. Press Ctrl + F5 to execute
3. Double-click the row in the Townhouse section
4. Change its year built to 2005
5. Change its market value to 585985 (If possible, also change its picture)

6. Click OK

 Deleting Nodes

 Deleting the First or Last Node

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() method. Its syntax is:

`public void RemoveFirst();`

As you can see, this method takes no argument. When it is called:

• If the list is empty, the compiler throws an InvalidOperationException exception:
• If the list contains one node, that node gets deleted
•  If the list contains more than one node, the first one gets deleted

To delete the last node, you can call the RemoveLast() method whose syntax is:

`public void RemoveLast();`

This method follows the same logic as the RemoveFirst() method, only in reverse. Here are examples of calling these methods:

```using System;
using System.Windows.Forms;
using System.Collections.Generic;

{

{
InitializeComponent();
}

private void InitializeComponent()
{

StartPosition = FormStartPosition.CenterScreen;
}

private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

foreach (double dbl in numbers)

numbers.RemoveFirst();
numbers.RemoveLast();

foreach (double dbl in numbers)
}
}

public class Exercise
{
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
return 0;
}
}```

 Removing a Node by Value

There are two ways you can delete an item inside the collection. This can be done using the Remove() method. It comes in two versions. If you know the exact value of the item you want to remove, you can call the following version of that method:

`public bool Remove(T value);`

When calling this method, pass the value to delete. The compiler would first look for a node that has that value:

• If there is a node with that value in the list, it would be deleted
• If there is no node with that value, nothing would happen (the compiler would not throw an exception)

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 method, pass a reference to the node you want to delete. Here is an example:

```private void btnLinkedListClicked(object sender, EventArgs e)
{
List<double> values = new List<double>();

foreach (double dbl in numbers)

numbers.Remove(number275775);

foreach (double dbl in numbers)
}```

 Topic Applied: Deleting a Node
1. Display the Properties Listing form
2. Under the form, click cmsProperties

 (Name) Enabled Text mnuNewProperty New Property... mnuEditProperty False Edit Property... mnuInsertBefore False Insert Property... mnuInsertAfter False Insert After mnuDeleteProperty False Delete Property
4. Double-click the Delete Property menu item
5. In the file, locate the ItemSelectionChanged event and change its implementation as follows:
```private void lvwProperties_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
RealEstateProperty currentProperty = new RealEstateProperty();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\" + e.Item.SubItems[0].Text + ".jpg";

if (lvwProperties.SelectedItems.Count == 1)
lviSelected = lvwProperties.SelectedItems[0];

if (File.Exists(strFileName))
pbxProperty.Image = Image.FromFile(strFileName);
else
pbxProperty.Image = Image.FromFile("C:\\Microsoft Visual C# Application Design\\Altair Realtors\\000-000.jpg");

if (lvwProperties.SelectedItems.Count == 1)
{
mnuInsertBefore.Enabled = true;
mnuEditProperty.Enabled = true;
mnuInsertAfter.Enabled = true;
mnuDeleteProperty.Enabled = true;
}
else
{
mnuInsertBefore.Enabled = false;
mnuEditProperty.Enabled = false;
mnuInsertAfter.Enabled = false;
mnuDeleteProperty.Enabled = false;
}
}```
6. Scroll down and implement the new event as follows:
```private void mnuDeleteProperty_Click(object sender, EventArgs e)
{
PropertyEditor editor = new PropertyEditor();
RealEstateProperty rep = new RealEstateProperty();
BinaryFormatter bfProperties = new BinaryFormatter();
string strFileName = "C:\\Microsoft Visual C# Application Design\\Altair Realtors\\properties.ars";

// Open the file that holds the properties
if (File.Exists(strFileName))
{
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Open,
{
}
}

// Create a real estate property using the property number that the user double-clicked
string strPropertyNumber = lvwProperties.SelectedItems[0].Text;
rep.PropertyNumber = lvwProperties.SelectedItems[0].Text;

// Ask the compiler to locate that property

// Just in case, make sure the property was found
if (nodProperty != null)
{
// Present a warning message to the user
if (MessageBox.Show("Are you sure you want to delete this property?",
"Altair Realtors",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
// If the user clicks yes, delete the property
properties.Remove(nodProperty);

// Save the list of properties
using (FileStream stmProperties = new FileStream(strFileName,
FileMode.Create,
FileAccess.Write))
{
bfProperties.Serialize(stmProperties, properties);
}
}
}

// Show the new list of properties
ShowProperties();
}```
8. Implement it as follows:
```private void btnClose_Click(object sender, EventArgs e)
{
Close();
}```
9. To execute, press Ctrl + F5
10. Right-click one of the records and click Delete Property
11. Click No
12. Right-click another row and click Delete Property
13. Click Yes

Clearing a list consists of deleting all of its item. To do this, you could continuous call one of the versions of the Remove() method. A faster solution is to call the Clear() method. Its syntax is:

`public void Clear();`