Remove an element on a given position from a LinkedList - c#

I try to remove an element after a specified node, code doesn't give me an error but doesn't to its job .
public List<int> list;
public class Node
{
public object Cont;
public Node Next;
}
public Node head;
linkedlist()
{
head = null;
}
public linkedlist(object value)
{
head = new Node();
head.Cont = value;
}
public void removeAfter(int nume1)
{
Node currentnode = head;
int current = 0;
while (current <= nume1 && currentnode.Next != null)
{
currentnode = currentnode.Next;
current++;
}
currentnode = currentnode.Next;
}
I try to remove the element on the "nume1"th position from the LinkedList.
I know c# has a built-in LinkedList but I need to work on this one. The file has more code but I think this is enough to get a proper answer

In order to remove an item after another item in a singly-linked list, we simply need to set Item.Next = Item.Next.Next. This effectively removes the Node at Item.Next from the list because:
If we have a node "first" at index 0, then it's Next will point to "second" at index 1, and second.Next will point to "third" at index 2
So first.Next.Next points to "third" at index 2
And setting first.Next = first.Next.Next changes the next item for "first" to "third".
For example:
class Node
{
public string Data { get; set; }
public Node Next { get; set; }
}
class LinkedList
{
public Node Head { get; private set; }
public void Add(string data)
{
var node = new Node {Data = data};
if (Head == null)
{
Head = node;
}
else
{
var current = Head;
while (current.Next != null) current = current.Next;
current.Next = node;
}
}
public void RemoveAfterIndex(int index)
{
if (index < -1) return;
if (index == -1)
{
Head = Head.Next;
return;
}
var current = Head;
var count = 0;
while (count < index && current.Next != null)
{
current = current.Next;
count++;
}
current.Next = current.Next?.Next;
}
public void WriteNodes()
{
var current = Head;
var index = 0;
while (current != null)
{
Console.WriteLine(current.Data.PadRight(10) + $" (index {index++})");
current = current.Next;
}
}
}
If we wanted to create a list with 10 nodes, and then remove the node after the node at index 3, it might look something like:
private static void Main()
{
var linkedList = new LinkedList();
for (int i = 0; i < 10; i++)
{
linkedList.Add($"Item #{i + 1}");
}
linkedList.WriteNodes();
var dash = new string('-', 25);
Console.WriteLine($"{dash}\nCalling: RemoveAfterIndex(3)\n{dash}");
linkedList.RemoveAfterIndex(3);
linkedList.WriteNodes();
GetKeyFromUser("\nDone! Press any key to exit...");
}
Output

In a single-linked list you remove element n by setting Next on element n-1 to element n+1. Since each element has a reference to the following element you need to iterate through the list until you locate element n, retaining the previous element as you go. Then it's just a simple assignment to cut the element from the list.
In code this looks something like:
public void RemoveAt(int position)
{
// reference to current position in list
Node current = head;
// check for empty list
if (current == null)
return;
// special case if we're removing the first item in the list
if (position == 0)
{
root = current.Next;
return;
}
// reference to previous position in list
Node previous = null;
// scan through the array to locate the item to remove
for (int i = 0; i < position && current != null; i++)
{
// update previous reference
previous = current;
// step to next element in list
current = current.Next;
}
if (previous != null && current != null)
{
previous.Next = current.Next;
}
}

Where you say this:
currentnode = currentnode.Next;
You should consider saying this:
currentnode.Next = currentnode.Next?.Next;
If you think about it, to remove the 5th node in a list, you want to make the .Next property of the 4th node, point to node 6. If you're looking at the 4th node, you want its .Next (pointing to 5) to be the .Next.Next (pointing to 5.pointing to 6) instead.
The ? after the first .Next helps us out if we are at the end of the list. If we are looking at the last item in a list, then .Next is null, so calling .Next.Next would result in a NullReferenceException. The ?prevents this by realising the first .Next is null and not trying to evaluate the second .Next on something that is null, but instead stopping evaluation early and returning a null at that point
It is conceptually equivalent to
currentnode.Next = (currentnode.Next == null ? null : currentNode.Next.Next);
It can be done any number of times:
a = b.C?.D?.E?.F;
If any of C, D, E is null, a will end up null without causing an exception. You don't need it after F because if only the last thing is null no exception occurs - we aren't accessing any method or property of the null F
Couple of other points:
Microsoft's own LinkedList has a method of removing nodes based on position, called RemoveAt. It's more useful than RemoveAfter because if can remove the first node in the list, whereas RemoveAfter cannot, unless you take the slightly counterintuitive approach that supplying any index less than the start index will remove the first node (e.g RemoveAfter(-1)). You could create a RemoveBefore or RemoveFirst, but it does add a bit of unnecessary complexity. Generally if taking the academic exercise of re-implementing a class that already exists somewhere in the framework, it's a sensible notion to model your methods on the existing one then it's a good overall fit for "the Microsoft way" in which the rest of the framework is written; fellow developers used to "the Microsoft way" will appreciate it if you're writing a library for them to use
Your naming convention (lowercase first letter for method) is java form, and I did wonder if you were using java but tagged c# for some reason. I'm not sure if null promoting ? exists in java- if it doesn't the inline-if form given last will also work fine.

Related

How can I delete a node from a singly linked list?

I'm taking lines (I:3, I:6, D:5, etc) from a text file and splitting them on the colon. Then, taking the number after the colon and pushing it to a node in a linked list. Depending on the line, it will insert(I) or delete(D) the node. However, I'm having trouble deleting a node. I created a method called deleteNode()
I reference this method in my if statement to check whether or not the command in the file starts with I or D to be told whether it gets inserted or deleted or not. I'm having trouble on how to reference the node to be deleted in llist.deletenode();
public class LinkedList
{
Node head; // the head of list
public class Node
{
public int data;
public Node next;
// constructor
public Node(int d)
{
data = d;
next = null;
} // end of constructor
}
public void printList()
{
// traversing list and printing the contents starting from head(1)
Node n = head;
int count = 0;
while (n != null)
{
count++;
Console.Write("Node" + count + ":" + " " + n.data + " ");
n = n.next;
}
}
public void push(int new_data)
{
// ads node to list
Node new_node = new Node(new_data); //allocate new node, put in data
new_node.next = head; //make next of new node as head
head = new_node; //moving head to point to the new node
}
public static void deleteNode(Node node, Node n)
{
// deletes node from list
// find the previous node
Node prev = node;
while (prev.next != null && prev.next != n)
{
prev = prev.next;
}
// Check if node really exists in Linked List
if (prev.next == null)
{
Console.WriteLine("Given node is not" +
"present in Linked List");
return;
}
// Remove node from Linked List
prev.next = prev.next.next;
// Free memory
GC.Collect();
return;
}
}
// main method to create a linked list with 3 nodes
public static void Main(String[] args)
{
// starting with an empty list
LinkedList llist = new LinkedList();
string[] lines = File.ReadAllLines(#"C:\\Users\project\text.txt");
foreach (string line in lines)
{
// splitting the lines on the colon
string[] bits = line.Split(':');
// taking the bit after the colon and parsing
// into an integer - the i is already parsed
int x = int.Parse(bits[1]); //the value after colon
if (bits[0] == "i")
{
llist.push(x);
}
else if (bits[0] == "d")
{
deleteNode(llist, existingNode); //error here
}
}
llist.printList();
}
I think the logic should probably match that of the push method (not sure why push isn't called Add or Insert instead, though...), in that it should look for the first node whose data matches the data we want to delete.
If we start at the head, we first want to determine if the head should be deleted. If so, then we reset the head to head.next and we're done!
Otherwise, we examine head.Next to see if it's the one to delete. If it is, then we set head.Next equal to head.Next.Next, effectively removing head.Next from our list (no node is pointing to it anymore).
If head.Next is not the node to delete, then we move to the next node and continue the process.
Here's an example:
public void DeleteNode(int nodeData)
{
// First check if the head is the node to delete
if (head != null && head.data == nodeData)
{
// If it is, set the head to the next node (effectively removing it from the list)
head = head.next;
return;
}
// Start at the head node
var current = head;
while (current != null)
{
// Get the next node
var next = current.next;
// See if the next node is the one to delte
if (next != null && next.data == nodeData)
{
// It is, so set the current node's Next pointer to the *next* node's
// Next pointer (effectively removing the Next node from the list)
current.next = next.next;
return;
}
// Update our current node to the next one and keep looking
current = next;
}
}
There are a lot of issues with your code ranging from aesthetic (name casing), to syntax errors, to design errors (static functions instead of methods).
I am offering some cleanup and an example below. The result is
Print List:
Node1: 0 Node2: 1 Node3: 2 Node4: 3 Node5: 4 Node6: 5
Deleting 4
Print List:
Node1: 0 Node2: 1 Node3: 2 Node4: 3 Node5: 5
and the sample code. The main addition is a separate function that finds the previous node FindPrevious() and use the existing Head of the list in DeleteNode().
Also changed fields into properties and PascalCasing as recommended per C# design rules.
namespace ConsoleApp1
{
public class Node
{
public int Data { get; set; }
public Node Next { get; set; }
// constructor
public Node(int data)
: this(null, data)
{ }
// always add a full constructor
public Node(Node next, int data)
{
this.Data = data;
this.Next = next;
}
}
public class LinkedList
{
public Node Head { get; set; }
public string PrintList()
{
// traversing list and printing the contents starting from head(1)
Node n = Head;
int count = 0;
StringBuilder sb = new StringBuilder();
while (n != null)
{
count++;
sb.Append("Node" + count + ":" + " " + n.Data + " ");
n = n.Next;
}
return sb.ToString();
}
// adds node to list
public void Push(int data)
{
//allocate new node, put in data
//and make next of new node as head
//moving head to point to the new node
Head = new Node(Head, data);
}
public Node FindPrevious(Node node)
{
Node n = Head;
while (n!=null)
{
if (n.Next == node)
{
return n;
}
n = n.Next;
}
return null;
}
public void DeleteNode(Node node)
{
if (node==null)
{
return;
}
Node prev = FindPrevious(node);
if (prev!=null)
{
// skip over node
prev.Next = node.Next;
}
}
}
static class Program
{
static void Main(string[] args)
{
LinkedList llist = new LinkedList();
llist.Push(5);
llist.Push(4);
llist.Push(3);
llist.Push(2);
llist.Push(1);
llist.Push(0);
Console.WriteLine($"Print List:");
Console.WriteLine(llist.PrintList());
Console.WriteLine();
var existingNode = llist.Head.Next.Next.Next.Next;
Console.WriteLine($"Deleting {existingNode.Data}");
llist.DeleteNode(existingNode);
Console.WriteLine($"Print List:");
Console.WriteLine(llist.PrintList());
}
}
}
DeleteNode method takes in two arguments. You are using deleteNode as an extension method but it's a simple method with two inputs.
deleteNode(llist, nodeYouWantToDelete);
Also, your loop that checks for D isn't right
if (bits[0] == "i") { //loop that checks if command starts with i (for inserting)
llist.push(x);
} else if(bits[0] == "d") { // CHECK bits[0] not [1]
// Get the node that matches value
// check here if the node already exists then delete
deleteNode(llist, existingNode);
}
update
public void push(int new_data) { //ads node to list
Node new_node = new Node(new_data); //allocate new node, put in data
new_node.next = null; //next should always be null.
// start from head and find the node whose next = null, point that to new_node
}
public void deleteNode(Node nodeToDelete) { //deletes node from list
// start from head and keep going next until node.next = nodeToDelete
// set node.next = node.next.next;
}
Working Solution for you
public class LinkedList
{
Node head;
Node last;
public class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
} // end of constructor
}
public void print()
{
// traversing list and printing the contents starting from head(1)
Node n = head;
int count = 0;
while (n != null)
{
Console.Write("Node" + count++ + ":" + " " + n.data + " ");
n = n.next;
}
}
public void push(int new_data)
{
Node thisNode = new Node(new_data);
if (head == null)
{
head = thisNode;
last = head;
return;
}
last.next = thisNode;
last = last.next;
}
public void delete(Node node)
{
if (head.data == node.data)
head = head.next;
else
{
Node iterate = head;
bool deleted = false;
while (iterate.next != null)
{
if (iterate.next.data == node.data && !deleted)
{
iterate.next = iterate.next.next;
deleted = true;
continue;
}
iterate.next = iterate.next.next;
}
last = iterate;
if (!deleted)
{
Console.WriteLine("Given node is not " +
"present in Linked List");
}
// Free memory
GC.Collect();
return;
}
}
}
//and use it in the main like,
public static void Main(string[] args)
{
LinkedList llist = new LinkedList();
string[] lines = new[] { "i:1", "i:3", "d:3", "i:2", "i:1", "d:3" };
foreach(string line in lines)
{
string[] split = line.Split(':');
if (split[0] == "i") // INSERTS At the end of the list.
llist.push(int.Parse(split[1]));
else if (split[0] == "d") // Deletes the "FIRST" occurence of said number
llist.delete(new LinkedList.Node(int.Parse(split[1])));
}
Console.Read();
}

Selection Sort on a Singly Linked List

As I checked, I find the right minimum value and previous node. After that the only thing I need to do is swapping nodes,however after implementing this code the output is nothing.
After drawing the question I thought the problem is sorted part.So I added one more node which name is sorted but still I couldn't solve my problem.
Here is my example code:
public void selectionSort()
{
Node<T> first = head;
Node<T> previous = head;
Node<T> minimum = head;
Node<T> compare;
Node<T> temp;
Node<T> sorted = head;
while (first.Next != null)
{
sorted = minimum; // with this I'm finding the last sorted node
minimum = first;
compare = first.Next;
while (compare.Next != null)
{
if (minimum.Value.CompareTo(compare.Next.Value) > 0)
{
previous = compare; // Need previous node to changing connections
minimum = compare.Next; // Saving minimum value
}
compare = compare.Next;
}
// Swapping nodes
temp = first;
previous.Next = first;
first.Next = minimum.Next;
minimum.Next = temp.Next;
if ( temp != head)
{
sorted.Next = minimum; // Adding minimum node to sorted part
}
first = first.Next;
}
}
I renamed some variables in your code to more meaningful ones:
currentOuter tracks the current node in the outer loop
currentInner tracks the current node in the inner loop
Swapping by value/data instead of by nodes simplifies the code a lot:
public void selectionSort<T>(Node<T> head) where T:IComparable
{
Node<T> currentOuter = head;
while (currentOuter != null)
{
Node<T> minimum = currentOuter;
Node<T> currentInner = currentOuter.Next;
while (currentInner != null)
{
if (currentInner.Value.CompareTo(minimum.Value) < 0)
{
minimum = currentInner;
}
currentInner = currentInner.Next;
}
if (!Object.ReferenceEquals(minimum, currentOuter))
{
T temp = currentOuter.Value;
currentOuter.Value = minimum.Value;
minimum.Value = temp;
}
currentOuter = currentOuter.Next;
}
}

C# Node pointer issue

I am having some trouble setting child nodes using C#. I am trying to build a tree of nodes where each node holds an int value and can have up to a number of children equal to it's value.
My issue appears when I iterate in a node looking for empty(null) children so that I may add a new node into that spot. I can find and return the null node, but when I set the new node to it, it loses connection to the parent node.
So if I add 1 node, then it is linked to my head node, but if I try to add a second it does not become a child of the head node. I am trying to build this with unit tests so here is the test code showing that indeed the head does not show the new node as it's child (also confirmed with visual studios debugger):
[TestMethod]
public void addSecondNodeAsFirstChildToHead()
{
//arange
Problem3 p3 = new Problem3();
p3.addNode(2, p3._head);
Node expected = null;
Node expected2 = p3._head.children[0];
int count = 2;
//act
Node actual = p3.addNode(1, p3._head);
Node expected3 = p3._head.children[0];
//assert
Assert.AreNotEqual(expected, actual, "Node not added"); //pass
Assert.AreNotEqual(expected2, actual, "Node not added as first child"); //pass
Assert.AreEqual(expected3, actual, "Node not added as first child"); //FAILS HERE
Assert.AreEqual(count, p3.nodeCount, "Not added"); //pass
}
Here is my code.
public class Node
{
public Node[] children;
public int data;
public Node(int value)
{
data = value;
children = new Node[value];
for(int i = 0; i < value; i++)
{
children[i] = null;
}
}
}
public class Problem3
{
public Node _head;
public int nodeCount;
public Problem3()
{
_head = null;
nodeCount = 0;
}
public Node addNode(int value, Node currentNode)
{
if(value < 1)
{
return null;
}
Node temp = new Node(value);
//check head
if (_head == null)
{
_head = temp;
nodeCount++;
return _head;
}
//start at Current Node
if (currentNode == null)
{
currentNode = temp;
nodeCount++;
return currentNode;
}
//find first empty child
Node emptyChild = findEmptyChild(currentNode);
emptyChild = temp;
nodeCount++;
return emptyChild;
}
public Node findEmptyChild(Node currentNode)
{
Node emptyChild = null;
//find first empty child of current node
for (int i = 0; i < currentNode.children.Length; i++)
{
if (currentNode.children[i] == null)
{
return currentNode.children[i];
}
}
//move to first child and check it's children for an empty
//**this causes values to always accumulate on left side of the tree
emptyChild = findEmptyChild(currentNode.children[0]);
return emptyChild;
}
I feel the problem is I am trying to treat the nodes as pointers like I would in C++ but that it is not working as I expect.
It is impossible for a function to return a handle (or a pointer) to something that does not yet exist. Either you initialize non existent value inside the function, or you provide enough variables for it to be initialized outside of the function.
One solution would be to rename the function findEmptyChild to something like initializeEmptyChild(Node currentNode, Node newNode), adding one more Node parameter to it (when calling it that would be temp value), and in the loop before return you initialize the previously empty Node, currentNode.children[i] = newNode.
Another solution would be not to return just one Node but two values, a parent node and an index where empty child is found, Tuple<Node, int> findEmptyChild(Node currentNode), and in the loop instead of return currentNode.children[i] you do return new Tuple<Node, int>(currentNode, i). When calling the function you would change the code to
var parentAndIndex = findEmptyChild(currentNode);
parentAndIndex.Item1.children[parentAndIndex.Item2] = temp;
Look at this part of your code:
Node temp = new Node(value);
//...
Node emptyChild = findEmptyChild(currentNode);
emptyChild = temp;
You are assigning the emptyChild to a new node, doing so you will "loose" the connection with any parent node. You should write something like this:
emptyChild.data = temp.data;
emptyChild.children = temp.children;
As others said, your approach using null checking could be improved. You mentioned that Node.data holds the numbers of children of a given node, so you could simply say that when you have Node.data == 0, that node should be treated as being null, or empty. For example, instead of having:
rootNode.children[0] = null; // rootNode can have a lot of children
rootNode.children[1] = null;
//...
you would have:
rootNode.children[0] = new Node(0);
rootNode.children[1] = new Node(0);
//...
At this point your code will look similar to this:
public class Node
{
public Node[] children;
public int data;
public Node(int value)
{
data = value;
children = new Node[value];
// Instead of "pointing" to null,
// create a new empty node for each child.
for (int i = 0; i < value; i++)
{
children[i] = new Node(0);
}
}
}
public class Problem3
{
public Node _head;
public int nodeCount;
public Problem3()
{
_head = null;
nodeCount = 0;
}
public Node addNode(int value, Node currentNode)
{
if (value < 1)
{
return null;
}
Node temp = new Node(value);
//check head
if (_head == null)
{
_head = temp;
nodeCount++;
return _head;
}
//start at Current Node
if (currentNode == null)
{
currentNode = temp;
nodeCount++;
return currentNode;
}
//find first empty child
Node emptyChild = findEmptyChild(currentNode);
if (emptyChild != null)
{
emptyChild.data = temp.data;
emptyChild.children = temp.children;
nodeCount++;
}
return emptyChild;
}
public Node findEmptyChild(Node currentNode)
{
// Null checking.
if (currentNode == null)
return null;
// If current node is empty, return it.
if (currentNode.data == 0)
return currentNode;
// If current node is non-empty, check its children.
// If no child is empty, null will be returned.
// You could change this method to check even the
// children of the children and so on...
return currentNode.children.FirstOrDefault(node => node.data == 0);
}
}
Let's look now at the testing part (please see the comments for clarification):
[TestMethod]
public void addSecondNodeAsFirstChildToHead()
{
//arange
Problem3 p3 = new Problem3();
p3.addNode(2, p3._head); // Adding two empty nodes to _head, this means that now _head can
// contain two nodes, but for now they are empty (think of them as
// being "null", even if it's not true)
Node expected = null;
Node expected2 = p3._head.children[0]; // Should be the first of the empty nodes added before.
// Be careful: if you later change p3._head.children[0]
// values, expected2 will change too, because they are
// now pointing to the same object in memory
int count = 2;
//act
Node actual = p3.addNode(1, p3._head); // Now we add a non-empty node to _head, this means
// that we will have a total of two non-empty nodes:
// this one fresly added and _head (added before)
Node expected3 = p3._head.children[0]; // This was an empty node, but now should be non-empty
// because of the statement above. Now expected2 should
// be non-empty too.
//assert
Assert.AreNotEqual(expected, actual, "Node not added"); //pass
// This assert won't work anymore, because expected2, expected 3 and actual
// are now pointing at the same object in memory: p3._head.children[0].
// In your code, this assert was working because
// In order to make it work, you should replace this statement:
// Node expected2 = p3._head.children[0];
// with this one:
// Node expected2 = new Node(0); // Create an empty node.
// expected2.data = p3._head.children[0].data; // Copy data
// expected2.children = p3._head.children[0].children;
// This will make a copy of the node instead of changing its reference.
Assert.AreNotEqual(expected2, actual, "Node not added as first child");
// Now this will work.
Assert.AreEqual(expected3, actual, "Node not added as first child");
Assert.AreEqual(count, p3.nodeCount, "Not added"); //pass
}

Removing a node from a LinkedList (C#)

I created a LinkedList class with a function delete to remove a certain node from the list if found, however it's not working:
public class LinkedList
{
public Node head;
<...>
public void delete(string n)
{
Node x = search(n); //returns the node to delete or null if not found
if (x != null)
x = x.next;
}
<...>
}
I figured all I needed to do is find the node and set it to the next one (so the node is "removed" out of the linked list), however it's not. If anyone could help me out it'd be much appreciated!
EDIT: Forgot to mention I have a single linked list.
EDIT2: My new code:
public void delete(string n)
{
Node x = head;
while (x != null && x.name != n)
{
if (x.next.name == n)
x.next = x.next.next;
x = x.next;
}
if (x != null)
x = null;
}
You'll want to loop through the list until the next node is the one you want to delete. Then set the current to the next nodes next node.
public void Delete(string value)
{
if (head == null) return;
if (head.Value == value)
{
head = head.Next;
return;
}
var n = head;
while (n.Next != null)
{
if (n.Next.Value == value)
{
n.Next = n.Next.Next;
return;
}
n = n.Next;
}
}
This of course assumes you only want to delete the first match.

Having trouble with removing a node from a Doubly linked List

I have a Doubly Linked List and I method which is supposed to remove a given node. It originally worked with my 3 test nodes when I originally developed the code but when I added more Nodes to the Doubly Linked List it stopped working, now I am getting an NullReferenceException:"Object reference not set to an instance of an object" I know I could just use a generic linked list but I am trying to learn how doubly linked lists work.
I will mark the line in my code where I am getting this exception with "**" at the beginning of the line(it is in the RemoveNode method in the Doubly Linked List class).
Here is my code:
Doubly Linked List class:
class DoublyLinkedList
{
public Node head, current;
public void AddNode(object n) // add a new node
{
if (head == null)
{
head = new Node(n); //head is pointed to the 1st node in list
current = head;
}
else
{
while (current.next != null)
{
current = current.next;
}
current.next = new Node(n, current); //current is pointed to the newly added node
}
}
public Node FindNode(object n) //Find a given node in the DLL
{
current = head;
while ((current != null) && (current.data != n))
current = current.next;
if (current == null)
return null;
else
return current;
}
public string RemoveNode(object n)//remove nodes
{
String Output = "";
if (head == null)
{
Output += "\r\nLink list is empty";
}
else
{
current = FindNode(n);
if (current == null)
{
Output += "Node not found in Doubly Linked List\r\n";
}
else
{
****current.next.prev = current.prev;**
current.prev.next = current.next;
current.prev = null;
current.next = null;
Output += "Node removed from Doubly Linked List\r\n";
}
}
return Output;
}
public String PrintNode() // print nodes
{
String Output = "";
Node printNode = head;
if (printNode != null)
{
while (printNode != null)
{
Output += printNode.data.ToString() + "\r\n";
printNode = printNode.next;
}
}
else
{
Output += "No items in Doubly Linked List";
}
return Output;
}
}
Node class:
class Node
{
public Node prev, next; // to store the links
public object data; // to store the data in a node
public Node()
{
this.prev = null;
this.next = null;
}
public Node(object data)
{
this.data = data;
this.prev = null;
this.next = null;
}
public Node(object data, Node prev)
{
this.data = data;
this.prev = prev;
this.next = null;
}
public Node(object data, Node prev, Node next)
{
this.data = data;
this.prev = prev;
this.next = next;
}
}
I got it working. Although I slightly altered you logic. I use the current field to mark a tail and for searching I use a separate variable. Now, I tested it with integer values and what caused a problem was the line where you compare values
(current.data != n)
where you can get that 10 != 10 because the values are boxed and references differs. Just use current.data.Equals(n) instead. Equals() is a virtual method that bubbles down to the real objects and compares data rather then references.
Furthermore, your removing logic has to be more complex. You can also do it more readable by removing all those necessary if/else statements.
public void AddNode(object n) // add a new node
{
if (head == null)
{
head = new Node(n); //head is pointed to the 1st node in list
current = head;
}
else
{
var newNode = new Node(n, current);
current.next = newNode;
current = newNode; //current is pointed to the newly added node
}
}
public Node FindNode(object n) //Find a given node in the DLL
{
var index = head;
while (index != null)
{
if (index.data.Equals(n))
break;
index = index.next;
}
return index ?? null;
}
public string RemoveNode(object n) //remove node
{
if (head == null)
return "\r\nLink list is empty";
var node = FindNode(n);
if (node == null)
return "Node not found in Doubly Linked List\r\n";
if (node != head)
node.prev.next = node.next;
if (node.next != null)
node.next.prev = node.prev;
return "Node removed from Doubly Linked List\r\n";
}
Do you try to delete an object from a list with only one Element (= head)? You got an error there, check your delete routine. You're receiving an element by using FindNode (which will then return your head node), afterwards you're calling current.prev.next = current.next;, which will fail because your headnode has neither a previous nor next node.
I'd assume your remove function should look similar to this:
public string RemoveNode(object n)//remove nodes
{
String Output = "";
if (head == null)
{
Output += "\r\nLink list is empty";
}
else
{
var toRemove = FindNode(n);
if (toRemove == null)
{
Output += "Node not found in Doubly Linked List\r\n";
}
else
{
if(toRemove.prev != null)
toRemove.prev.next = toRemove.next != null ? toRemove.Next : null;
if(toRemove.next != null)
toRemove.next.prev = toRemove.prev != null ? toRemove.prev : null;
Output += "Node removed from Doubly Linked List\r\n";
}
}
return Output;
}

Categories

Resources