I have a class in my app that is not transforming my XML data as expected.
XML Data
Below is an excerpt of the XML. The file size can be between 2 GB and 3 GB, and the data is a representation of Mutual Funds. Each Fund usually has managers associated with it, but it's possible that there are none listed. A Fund in the data can have multiple ManagerDetail nodes or can have no ManagerDetail nodes. Each manager can have multiple CollegeEducation nodes or no CollegeEducation nodes.
<MutualFund>
<ManagerList>
<ManagerDetail>
<ManagerRole>M</ManagerRole>
<ManagerId>7394</ManagerId>
<ManagerTenure>3.67</ManagerTenure>
<StartDate>2011-09-30</StartDate>
<OwnershipLevel>6</OwnershipLevel>
<GivenName>Stephen</GivenName>
<MiddleName>M.</MiddleName>
<FamilyName>Kane</FamilyName>
<Gender>M</Gender>
<Certifications>
<CertificationName>CFA</CertificationName>
</Certifications>
<CollegeEducations>
<CollegeEducation>
<School>University of Chicago</School>
<Year>1990</Year>
<Degree>M.B.A.</Degree>
</CollegeEducation>
<CollegeEducation>
<School>University of California - Berkeley</School>
<Year>1985</Year>
<Degree>B.S.</Degree>
<Major>Business</Major>
</CollegeEducation>
</CollegeEducations>
</ManagerDetail>
</ManagerList>
</MutualFund>
C# Class
I've created a class that is called within a BackgroundWorker instance in another form. This class places the above data into the following table:
public static DataTable dtManagersEducation = new DataTable();
dtManagersEducation.Columns.Add("ManagerId");
dtManagersEducation.Columns.Add("Institution");
dtManagersEducation.Columns.Add("DegreeType");
dtManagersEducation.Columns.Add("Emphasis");
dtManagersEducation.Columns.Add("Year");
The method that places the XML data is set up like this. Basically, I have certain points where DataRows are created and completed, and certain XML data is to be placed into the available row as the data is read.
public static void Read(MainForm mf, XmlReader xml)
{
mainForm = mf;
xmlReader = xml;
while (xmlReader.Read() && mainForm.continueProcess)
{
if (xmlReader.Name == "CollegeEducation")
{
if (nodeIsElement())
{
drManagersEducation = dtManagersEducation.NewRow();
drManagersEducation["ManagerId"] = currentManager.morningstarManagerId;
}
else if (nodeIsEndElement())
{
dtManagersEducation.Rows.Add(drManagersEducation);
drManagersEducation = null;
}
}
else if (xmlReader.Name == "School")
{
if (nodeIsElement() && drManagersEducation != null)
{
string value = xmlReader.ReadElementContentAsString();
drManagersEducation["Institution"] = value;
}
}
else if (xmlReader.Name == "Year")
{
if (nodeIsElement() && drManagersEducation != null)
{
string value = xmlReader.ReadElementContentAsString();
drManagersEducation["Year"] = value;
}
}
else if (xmlReader.Name == "Degree")
{
if (nodeIsElement() && drManagersEducation != null)
{
string value = xmlReader.ReadElementContentAsString();
drManagersEducation["DegreeType"] = value;
}
}
else if (xmlReader.Name == "Major")
{
if (nodeIsElement() && drManagersEducation != null)
{
string value = xmlReader.ReadElementContentAsString();
drManagersEducation["Emphasis"] = value;
}
}
}
}
private static bool nodeIsElement()
{
return xmlReader.NodeType == XmlNodeType.Element;
}
private static bool nodeIsEndElement()
{
return xmlReader.NodeType == XmlNodeType.EndElement;
}
The result ends up with no data in the Emphasis or Year columns, which as you can see above, there are instances (plenty) that have data in these fields.
ManagerId Institution DegreeType Emphasis Year
5807 Yale University M.S.
9336 Yale University
7227 Yale University M.S.
Would you all happen to have some insight into what is going on?
Thanks
Edit: Answer
My sample XML data listed above has indented spaces, but the actual data that I was running through the XmlReader did not. As dbc has shown below, adding a variable bool readNext fixed my issues. As I understand it, if readNext is set to false when ReadElementContentAsString() is called, the XmlReader will not call Read() since my while loop condition now contains (!readNext || xmlReader.Read()). This prevents the two methods ReadElementContentAsString() and Read() to be called right after another, and thus, it will not skip over data.
Thanks to dbc!
The problem you are seeing is that the method XmlReader.ReadElementContentAsString moves the reader past the end element tag. If you then do xmlReader.Read() unconditionally right afterwards, the node immediately after the end element tag will be skipped. In the XML shown in your question, the node immediately after your end element tags is whitespace, so the bug isn't reproducible with your question. But if I strip the indentation (and hopefully your 2+GB XML file has no indentation), the bug becomes reproducible.
Also, in your question, I don't see where you actually read the <ManagerId>7394</ManagerId> tag. Instead you just take it from currentManager.morningstarManagerId (an undefined global variable). I reckon that's a typo in your question, and in your actual code you read this somewhere.
Here's a version of your method that fixes these problems and can be compiled and tested standalone:
public static DataTable Read(XmlReader xmlReader, Func<bool> continueProcess)
{
DataTable dtManagersEducation = new DataTable();
dtManagersEducation.TableName = "ManagersEducation";
dtManagersEducation.Columns.Add("ManagerId");
dtManagersEducation.Columns.Add("Institution");
dtManagersEducation.Columns.Add("DegreeType");
dtManagersEducation.Columns.Add("Emphasis");
dtManagersEducation.Columns.Add("Year");
bool inManagerDetail = false;
string managerId = null;
DataRow drManagersEducation = null;
bool readNext = true;
while ((!readNext || xmlReader.Read()) && continueProcess())
{
readNext = true;
if (xmlReader.NodeType == XmlNodeType.Element)
{
if (!xmlReader.IsEmptyElement)
{
if (xmlReader.Name == "ManagerDetail")
{
inManagerDetail = true;
}
else if (xmlReader.Name == "ManagerId")
{
var value = xmlReader.ReadElementContentAsString();
readNext = false;
if (inManagerDetail)
managerId = value;
}
else if (xmlReader.Name == "School")
{
var value = xmlReader.ReadElementContentAsString();
readNext = false;
if (drManagersEducation != null)
drManagersEducation["Institution"] = value;
}
else if (xmlReader.Name == "Year")
{
var value = xmlReader.ReadElementContentAsString();
readNext = false;
if (drManagersEducation != null)
drManagersEducation["Year"] = value;
}
else if (xmlReader.Name == "Degree")
{
var value = xmlReader.ReadElementContentAsString();
readNext = false;
if (drManagersEducation != null)
drManagersEducation["DegreeType"] = value;
}
else if (xmlReader.Name == "Major")
{
var value = xmlReader.ReadElementContentAsString();
readNext = false;
if (drManagersEducation != null)
drManagersEducation["Emphasis"] = value;
}
else if (xmlReader.Name == "CollegeEducation")
{
if (managerId != null)
{
drManagersEducation = dtManagersEducation.NewRow();
drManagersEducation["ManagerId"] = managerId;
}
}
}
}
else if (xmlReader.NodeType == XmlNodeType.EndElement)
{
if (xmlReader.Name == "ManagerDetail")
{
inManagerDetail = false;
managerId = null;
}
else if (xmlReader.Name == "CollegeEducation")
{
if (drManagersEducation != null)
dtManagersEducation.Rows.Add(drManagersEducation);
drManagersEducation = null;
}
}
}
return dtManagersEducation;
}
Related
I have a tree search algorithm that uses multiple keys and each node has a sub tree which is searched with the next key in the list of keys until the keys run out and the data being searched is in that end node.
my code is working but I don't know what to call it. I thought it was a Ternary Tree until I read Wicki page and that doesn't seem to be it. So I just don't know what to call it.
here's my class. works like a binary tree with no limit on the number of keys where the set of keys are sent to the search/Insert functions as a list. Each time one key finds a node, the next key is stripped off the list and that node's "Next key Tree" repeats the process until it runs out of keys and sends back the data. My thinking is I can use it to mark exact searches of categories like "first name", "second name", "occupation", "city". they are entered in the same sequence and any sub-tree can be traversed. still not sure how much better this is than a regular binary-tree for strings. I have another exact version that has Integer keys that might come in more handy.
public class classTernaryTree_StringKey
{
classTernaryTree_StringKey_Node cRoot = null;
public void Insert(ref object data, List<string> lstKeys)
{
classTernaryTree_StringKey cMyRef = this;
_Insert(ref cMyRef, ref data, lstKeys);
}
static void _Insert(ref classTernaryTree_StringKey cTree, ref object data, List<string> lstKeys)
{
if (cTree.cRoot == null)
{
cTree.cRoot = new classTernaryTree_StringKey_Node();
cTree.cRoot.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cTree.cRoot.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cTree.cRoot.data = data;
return;
}
}
classTernaryTree_StringKey_Node cNode = cTree.cRoot;
do
{
int intComparison = string.Compare(lstKeys[0], cNode.key);
if (intComparison > 0)
{
if (cNode.Right != null)
cNode = cNode.Right;
else
{
cNode.Right = new classTernaryTree_StringKey_Node();
cNode.Right.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.Right.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.Right.data = data;
return;
}
}
}
else if (intComparison < 0)
{
if (cNode.Left != null)
cNode = cNode.Left;
else
{
cNode.Left = new classTernaryTree_StringKey_Node();
cNode.Left.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.Left.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.Left.data = data;
return;
}
}
}
else
{
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.data = data;
return;
}
}
} while (true);
}
public object Search(List<string> lstKeys)
{
classTernaryTree_StringKey cMyRef = this;
return _Search(ref cMyRef, lstKeys);
}
static object _Search(ref classTernaryTree_StringKey cTree, List<string> lstKeys)
{
if (cTree.cRoot == null) return null;
classTernaryTree_StringKey_Node cNode = cTree.cRoot;
do
{
int intComparison = string.Compare(lstKeys[0], cNode.key);
if (intComparison > 0)
{
if (cNode.Right != null)
cNode = cNode.Right;
else
return null;
}
else if (intComparison < 0)
{
if (cNode.Left != null)
cNode = cNode.Left;
else
return null;
}
else
{
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
return cNode.NextTree.Search(lstKeys);
else
return cNode.data;
}
} while (true);
}
}
public class classTernaryTree_StringKey_Node
{
public string key = "";
public classTernaryTree_StringKey_Node Left = null;
public classTernaryTree_StringKey_Node Right = null;
public classTernaryTree_StringKey NextTree = new classTernaryTree_StringKey();
public object data = null;
}
The data structure you have here does actually look pretty similar to a ternary search tree, just with strings labeling the nodes rather than individual characters.
A ternary search tree is a way of encoding a trie data structure. Tries are trees representing sequences of elements. Usually, those elements are individual characters, but they could in principle be anything. Your data structure is essentially a ternary search tree where you store strings rather than characters at each node.
For some reason, my code doesn't seem to reach the last few lines.
I've added the stop point and the return point in the code.
I don't see anything wrong with the data source I use on that row whenever I check it out in debug.
The code seems to set the value there and jumps back to the top of the foreach loop.
foreach (DataGridViewRow row in dataGridView1.Rows) {
//We check if the user has already filled in some info
return point if (row.Cells[7].Value != null && row.Cells[6].Value != null && !askedTheUser)
{
//trigger for message if you want to replace them
Message m = new Message("There is already info present in the category and or size dropdown. Would you like to overwrite this?", "Overwrite", "YESNO");
if (m.Awnser) {
overwrite = true;
}
askedTheUser = true;
}
DataGridViewComboBoxCell cat = (DataGridViewComboBoxCell)row.Cells[6];
string toMatch = row.Cells[3].Value.ToString();
//Now we will start matching
//First we try to match with the package ( if that is filled in )
if (row.Cells[5].Value != null && (string)row.Cells[5].Value != "") {
toMatch = row.Cells[5].Value.ToString();
matchWithPackage = true;
}
Regex re = new Regex(#"([a-zA-Z]+)(\d+)");
Match result = re.Match(toMatch);
string alphaPart = result.Groups[1].Value;
string numberPart = result.Groups[2].Value;
Datagridview dgv = new Datagridview();
if (numberPart.Length < 4) {
numberPart = numberPart.PadLeft(4, '0');
}
#if DEBUG
Console.WriteLine(numberPart);
#endif
//Matching the category
if (CHIP != null && CHIP.Contains(alphaPart))
{
cat.Value = "CHIP";
}
else if (SOJ != null && SOJ.Contains(alphaPart))
{
cat.Value = "SOJ";
}
else if (PLCC != null && PLCC.Contains(alphaPart))
{
cat.Value = "PLCC";
}
else if (QFP != null && QFP.Contains(alphaPart))
{
cat.Value = "QFP";
}
else if (SOP != null && SOP.Contains(alphaPart))
{
cat.Value = "SOP";
}
else {
if (cat.Value != "") {
cat.Value = "";
}
cat.FlatStyle = FlatStyle.Flat;
cat.Style.BackColor = Color.DarkRed;
continue;
}
//Setting the matched color
cat.FlatStyle = FlatStyle.Flat;
cat.Style.BackColor = Color.SpringGreen;
//Adding the dropdownlist to the size cb
(row.Cells[7] as DataGridViewComboBoxCell).DataSource = dgv.AddSeconderyCombobox(dataGridView1, row.Cells[6].Value.ToString());
if (!matchWithPackage) {
continue;
}
//Matching the size
Stop Point List<string> sizeList = (List<string>)(row.Cells[7] as DataGridViewComboBoxCell).DataSource;
Doesn't reach this and beyond List<string> matchedList = sizeList.FindAll(stringToCheck => stringToCheck.Contains(numberPart));
if (matchedList.Count > 0) {
(row.Cells[7] as DataGridViewComboBoxCell).DataSource = matchedList;
}
}
So i fixt my own problem and i'm going to awnser my own question i case somebody comes across a similar problem.
if (sizeList != null) {
List<string> matchedList = sizeList.FindAll(stringToCheck => stringToCheck.Contains(numberPart));
if (matchedList.Count > 0)
{
(row.Cells[7] as DataGridViewComboBoxCell).DataSource = matchedList;
}
}
After adding the few last lines in an extra check that needed to be done I noticed that the code was being executed, but the matchedlist.count was always 0.
So what was happening is that the code after that was redundant as the matchList was 0 and the debugger just skipped jumping to these lines ( a bit confusing if you ask me ).
I've implemented Singly linked list using C# . Can anyone please look into the following code and suggest where I'm wrong?
public int RemoveLast()
{
if (Head != null)
{
var curNode = Head;
while (curNode.Next != null)
{
curNode = curNode.Next;
}
var lastNodeValue = curNode.Value;
curNode = null;
Size--;
return lastNodeValue;
}
return -1;
}
This function does not remove the last node. I'm unable to figure out what's wrong. When while loop ends, we have the reference of node in curNode whose next is null. It means this is the last node. At the end, I'm setting this node to null. But when I use Display function. It displays the last node as well. This is not deleting the last node.
Here is my display function:
public string Display()
{
if (Head == null)
{
return string.Empty;
}
var curNode = Head;
var builder = new StringBuilder();
while (curNode.Next != null)
{
builder.Append($"{curNode.Value} ");
curNode = curNode.Next;
}
builder.Append($"{curNode.Value} ");
return builder.ToString();
}
You need to go to the last-but-one node, and change its Next to null:
public int RemoveLast()
{
if (Head != null)
{
var curNode = Head;
while (curNode.Next?.Next != null)
{
curNode = curNode.Next;
}
var lastNodeValue = curNode.Next?.Value ?? -1;
curNode.Next = null;
Size--;
return lastNodeValue;
}
return -1;
}
Note that if you also want Head to be set to null if its the only node, then you can do that like so:
public int RemoveLast()
{
if (Head != null)
{
var curNode = Head;
while (curNode.Next?.Next != null)
{
curNode = curNode.Next;
}
int lastNodeValue;
if (Head.Next == null)
{
lastNodeValue = Head.Value;
Head = null;
}
else
{
lastNodeValue = curNode.Next?.Value ?? -1;
}
curNode.Next = null;
Size--;
return lastNodeValue;
}
return -1;
}
I have to say though, this Head property looks a bit dubious - it should perhaps belong to a different class.
[x] -> [x] -> [x] -> null
^
curNode (becomes null)
^
this reference still exists
When doing curNode = null you do not change any reference in the list. curNode variable is changed only, it is pointing to the last element before operation and becomes null afterwards.
Try always keep reference to the node before last:
public int RemoveLast()
{
if (Head != null)
{
var curNode = Head;
// Corner case when there is only one node in the list
if (Head.Next == null)
{
Head = null;
Size--;
return curNode.value;
}
var beforeLastNode = curNode;
curNode = curNode.Next;
while (curNode.Next != null)
{
beforeLastNode = curNode;
curNode = curNode.Next;
}
var lastNodeValue = curNode.Value;
beforeLastNode.Next = null;
Size--;
return lastNodeValue;
}
return -1;
}
You need to make the curNode.Next value null on the previous node.
'curNode' is a local variable, setting it null doesn't do anything except maybe extend its GC life.
public int RemoveLast()
{
if (Head != null)
{
var curNode = Head;
var previousNode = null;
while (curNode.Next != null)
{
previousNode = curNode;
curNode = curNode.Next;
}
var lastNodeValue = curNode.Value;
if (previousNode == null)
Head = null;
else
previousNode.Next = null;
Size--;
return lastNodeValue;
}
return -1;
}
okay guys, after your help, I've rewritten this method that meets all the requirements and set to HeadNode null if there is only one node in linked list. So here we go:
public int RemoveLast()
{
if (HeadNode != null)
{
var currNode = HeadNode;
var prevNode = HeadNode;
if (HeadNode.Next == null)
{
HeadNode = null;
Size--;
return currNode.Value;
}
while (currNode.Next != null)
{
prevNode = currNode;
currNode = currNode.Next;
}
prevNode.Next = null;
Size--;
return currNode.Value;
}
return -1;
}
Thank you everyone who contributed in this thread. Happy Coding :)
My program is doing weird stuff if i delete one of my Attributes because it is not able to handle Siblings without Attributes. Now i googled for a while but i am not able to find a good way to check for attributes.
Whats the way you prefer checking for attributes?
while (FXMLNode != null)
{
if (FXMLNode.Name.ToLower() == "datei")
{
xmlInformationen oInfo = new xmlInformationen();
oInfo.Dateipfad = FXMLNode.InnerText;
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
else if(FXMLNode == null)
oInfo.CheckBox = true;
else
oInfo.CheckBox = true;
lstAttribute.Add(oInfo);
iCounter++;
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
else
{
if (FXMLNode.NextSibling == null)
{
FXMLNode = FXMLNode.FirstChild;
}
else
{
FXMLNode = FXMLNode.NextSibling;
}
}
}
You are accessing the value of an attribute without knowing if the attribute exists or not. Rewrite your code to check for the attribute first:
oInfo.CheckBox = true;
if(FXMLNode == null) oInfo.CheckBox = true; //not sure why you set it to true here
else if (FXMLNode.HasAttribute("checked"))
{
if (FXMLNode.Attributes["checked"].Value.ToString() == "true")
oInfo.CheckBox = true;
else if (FXMLNode.Attributes["checked"].Value.ToString() == "false")
oInfo.CheckBox = false;
}
Please note that checking if the Xml element is null should be the first thing you do. If it's null then it surely won't have any attributes but you'll have an exception.
you can just check thats the attribute in not null like this
if (FXMLNode.Attributes["checked"]!=null)
and then check the value
I am writing a single linked list in C#, could you please suggest to me if there any way to write a better remove method than the one I have:
using System;
class Node
{
public int data = int.MinValue;
public Node m_NextNode;
public Node(int data_in)
{
data = data_in;
}
public Node()
{
}
}
class LinkedList
{
private Node m_HeadNode;
public void InsertData(int data)
{
Node aCurrentNode = m_HeadNode;
if(m_HeadNode == null)
{
m_HeadNode = new Node();
m_HeadNode.data = data;
}
else
{
while(aCurrentNode.m_NextNode != null)
aCurrentNode = aCurrentNode.m_NextNode;
aCurrentNode.m_NextNode = new Node();
aCurrentNode.m_NextNode.data = data;
}
}
public void DisplayData()
{
Node aCurrentNode = m_HeadNode;
while (aCurrentNode != null)
{
Console.WriteLine("the value is {0}", aCurrentNode.data);
aCurrentNode = aCurrentNode.m_NextNode;
}
}
public void RemoveData(int data)
{
Node aCurrentNode = m_HeadNode;
while (aCurrentNode != null)
{
//if the data is present in head
//remove the head and reset the head
if (m_HeadNode.data == data)
{
m_HeadNode = null;
m_HeadNode = aCurrentNode.m_NextNode;
}
else
{
//else save the previous node
Node previousNode = aCurrentNode;
if (aCurrentNode != null)
{
//store the current node
Node NextNode = aCurrentNode.m_NextNode;
if (NextNode != null)
{
//store the next node
Node tempNode = NextNode.m_NextNode;
if (NextNode.data == data)
{
previousNode.m_NextNode = tempNode;
NextNode = null;
}
}
aCurrentNode = aCurrentNode.m_NextNode;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
LinkedList aLinkedList = new LinkedList();
aLinkedList.InsertData(10);
aLinkedList.InsertData(20);
aLinkedList.InsertData(30);
aLinkedList.InsertData(40);
aLinkedList.DisplayData();
aLinkedList.RemoveData(10);
aLinkedList.RemoveData(40);
aLinkedList.RemoveData(20);
aLinkedList.RemoveData(30);
aLinkedList.DisplayData();
Console.Read();
}
}
I would pull the 'if removing the head node' out of the while loop, and make the while loop simpler. Just keep track of the current and previous nodes, and switch the reference when you find the node to remove.
public void RemoveData(int data)
{
if (m_HeadNode == null)
return;
if (m_HeadNode.data == data)
{
m_HeadNode = m_HeadNode.m_NextNode;
}
else
{
Node previous = m_HeadNode;
Node current = m_HeadNode.m_NextNode;
while (current != null)
{
if (current.data == data)
{
// If we're removing the last entry in the list, current.Next
// will be null. That's OK, because setting previous.Next to
// null is the proper way to set the end of the list.
previous.m_NextNode = current.m_NextNode;
break;
}
previous = current;
current = current.m_NextNode;
}
}
}
Is RemoveData supposed to remove one instance of that integer from the list, or all instances? The previous method removes just the first one, here's one that removes all of them.
public void RemoveAllData(int data)
{
while (m_HeadNode != null && m_HeadNode.data == data)
{
m_HeadNode = m_HeadNode.m_NextNode;
}
if(m_HeadNode != null)
{
Node previous = m_HeadNode;
Node current = m_HeadNode.m_NextNode;
while (current != null)
{
if (current.data == data)
{
// If we're removing the last entry in the list, current.Next
// will be null. That's OK, because setting previous.Next to
// null is the proper way to set the end of the list.
previous.m_NextNode = current.m_NextNode;
// If we remove the current node, then we don't need to move
// forward in the list. The reference to previous.Next, below,
// will now point one element forward than it did before.
}
else
{
// Only move forward in the list if we actually need to,
// if we didn't remove an item.
previous = current;
}
current = previous.m_NextNode;
}
}
}
You have a line you do not need:
m_HeadNode = null;
m_HeadNode = aCurrentNode.m_NextNode;
You don't need to set m_HeadNode to null before you set it to something else.
public bool RemoveNode(int data) {
Node prev = null;
for (Node node = head; node != null; node = node.next) {
if (node.data == data) {
if (prev == null) head = node.next;
else prev.next = node.next;
return true;
}
prev = node;
}
return false;
}
public void RemoveData(int data)
{
if(null == m_HeadNode) return;
if(m_HeadNode.data == data)
{
// remove first node
}
else
{
Node current = m_HeadNode;
while((null != current.m_NextNode) && (current.m_NextNode.data != data))
current = current.m_NextNode;
if(null != current.m_NextNode)
{
// do remove node (since this sounds like homework, I'll leave that to you)
}
}
}
with only one local variable.
There is a bug in you code, imagine you have list that has 3 elements with data = 5 and you want to remove 5, you codes stores the head in aCurrentNode and starts the loop. There the condition is true so you move the head to the next. but aCurrentNode is not updated so in the next iteration it's pointing to the previous Head and aCurrentNode.m_NextNod would be your current Head, Hence you got yourself in a never ending loop!
public void Remove(int data)
{
for(var cur = new Node {Next = Head}; cur.Next != null; cur = cur.Next)
{
if (cur.Next.Data != data) continue;
if (cur.Next == Head)
Head = Head.Next;
else
cur.Next = cur.Next.Next;
}
}
a trick to make you loop simpler is to start with a fake node that points to head. This way you don't need to place an If to check head differently, however you need an if to set the head. the other trick is to check for next nodes data. that way you don't need to keep a previous Node.
public SLElement Remove(int index)
{
SLElement prev = _root;
if (prev == null) return null;
SLElement curr = _root._next;
for (int i = 1; i < index; i++)
{
if (curr == null) return null;
prev = curr;
curr = curr._next;
}
prev._next = curr._next;
curr._next = null;
return curr;
}
This deletes the element with a specific index, not with a specific value.
To Make really simple. Please follow the link. Below is a code snippet to remove item from a single link list.
public void DeleteNode(int nodeIndex)
{
int indexCounter = 0;
Node TempNode = StartNode;
Node PreviousNode = null;
while (TempNode.AddressHolder != null)
{
if (indexCounter == nodeIndex)
{
PreviousNode.AddressHolder = TempNode.AddressHolder;
break;
}
indexCounter++;
PreviousNode = TempNode;
TempNode = TempNode.AddressHolder;
}
}