Skip to content

Commit 4b65b86

Browse files
committed
Improved code for DLList remove method.
1 parent f5a8031 commit 4b65b86

2 files changed

Lines changed: 87 additions & 10 deletions

File tree

source/code/projects/DLList_ICollection/DLList_ICollection/DLList.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public bool IsEmpty()
4141
// at the end of the list.
4242
public void Add(T value)
4343
{
44-
if (isReadonly)
44+
if (IsReadOnly)
4545
{
4646
throw new InvalidOperationException(
4747
"List is read-only."
@@ -108,7 +108,7 @@ public void CopyTo(T[] array, int arrayIndex)
108108

109109
public bool Remove(T value)
110110
{
111-
if (isReadonly)
111+
if (IsReadOnly)
112112
{
113113
throw new InvalidOperationException(
114114
"List is read-only"
@@ -117,13 +117,26 @@ public bool Remove(T value)
117117
bool removed = false;
118118
if (!IsEmpty())
119119
{
120+
// If the value we are looking for
121+
// is held by head
120122
if (head.Data.Equals(value))
121123
{
122124
head = head.Next;
125+
// If there was more than one
126+
// cell in our list
123127
if (head != null)
124128
{
129+
// We delete the reference
130+
// to the node
131+
// we want to remove.
125132
head.Previous = null;
126133
}
134+
else
135+
{
136+
// Since there was only one cell in our list,
137+
// the tail needs to be updated.
138+
tail = null;
139+
}
127140
removed = true;
128141
}
129142
else
@@ -133,13 +146,24 @@ public bool Remove(T value)
133146
{
134147
if (cCell.Next.Data.Equals(value))
135148
{
136-
cCell.Next.Previous = cCell.Previous;
137-
138149
cCell.Next = cCell.Next.Next;
150+
// We test if we reached the end of the list
151+
if (cCell.Next != null)
152+
{
153+
cCell.Next.Previous = cCell;
154+
}
155+
else
156+
{
157+
// If we did, we update the tail.
158+
tail = cCell;
159+
}
139160
removed = true;
140161
}
141162
else
142163
{
164+
// If we did not find the value,
165+
// we move the cCell to the next
166+
// cell to continue searching.
143167
cCell = cCell.Next;
144168
}
145169
}
@@ -163,12 +187,7 @@ public int Count
163187
}
164188
}
165189

166-
public bool isReadonly = false;
167-
public bool IsReadOnly
168-
{
169-
get { return isReadonly; }
170-
set { isReadonly = value; }
171-
}
190+
public bool IsReadOnly { get; set; }
172191

173192
public IEnumerator<T> GetEnumerator()
174193
{

source/code/projects/DLList_ICollection/DLList_ICollection/Program.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ static void Main(string[] args)
4141
Console.WriteLine(myList1);
4242
Console.WriteLine("Removed 2: " + myList1.Remove(2));
4343
Console.WriteLine(myList1);
44+
Console.WriteLine("Removed 1: " + myList1.Remove(1));
45+
Console.WriteLine(myList1);
46+
Console.WriteLine("Removed 22: " + myList1.Remove(22));
47+
Console.WriteLine(myList1);
4448

4549
// A benefit of realizing the ICollection interface
4650
// is that we can iterate over elements of lists now:
@@ -90,5 +94,59 @@ static void Main(string[] args)
9094
{
9195
Console.Write(i + " ");
9296
}
97+
/*
98+
* Testing Remove in detail
99+
*/
100+
101+
// Trying to remove from an empty list.
102+
DLList<string> myList3 = new DLList<string>();
103+
Console.WriteLine(myList3);
104+
Console.WriteLine(
105+
"Remove \"test\": " + myList3.Remove("Test")
106+
);
107+
Console.WriteLine(myList3);
108+
109+
// Trying to remove the only element of a list.
110+
myList3.Add("Test");
111+
Console.WriteLine(myList3);
112+
Console.WriteLine(
113+
"Remove \"test\": " + myList3.Remove("Test")
114+
);
115+
Console.WriteLine(myList3);
116+
117+
// Trying to remove the first element of a list of size 2.
118+
myList3.Add("Test");
119+
myList3.Add("String 1");
120+
Console.WriteLine(myList3);
121+
Console.WriteLine(
122+
"Remove \"test\": " + myList3.Remove("Test")
123+
);
124+
Console.WriteLine(myList3);
125+
126+
// Trying to remove the last element of a list of size 3.
127+
myList3.Add("String 2");
128+
myList3.Add("Test");
129+
Console.WriteLine(myList3);
130+
Console.WriteLine(
131+
"Remove \"test\": " + myList3.Remove("Test")
132+
);
133+
Console.WriteLine(myList3);
134+
135+
// Trying to remove an element that is not in a list.
136+
Console.WriteLine(myList3);
137+
Console.WriteLine(
138+
"Remove \"test\": " + myList3.Remove("Test")
139+
);
140+
Console.WriteLine(myList3);
141+
142+
// Trying to remove an element in the middle of a list of size 5.
143+
myList3.Add("Test");
144+
myList3.Add("String 3");
145+
myList3.Add("String 4");
146+
Console.WriteLine(myList3);
147+
Console.WriteLine(
148+
"Remove \"test\": " + myList3.Remove("Test")
149+
);
150+
Console.WriteLine(myList3);
93151
}
94152
}

0 commit comments

Comments
 (0)