C# - LinkedList Issues

------------------------

I encountered a few problems when trying to execute a few operations for LinkedList implementaions using C#-Sharp programming codes.

Hereby, i am posting the codes that i have written and hopefully some programmers could provide me with some pointers as to how the problems can be resolve or highlights to me what went wrong or provide with some sample solutions.

When i tried to rxecute the operations for btnAddRear and btnAddPosition, there are no effect at all. Hence, i am suspecting the codes have went wrong somewhere in the entire list.

It would be very much appreciated that any programmers gurus could highlight to me what actually went wrong.
Thanx in advance. Looking forward to the solutions to unravel the mystery puzzles.

Below are the main highlights that have yet to be resolve.

1. Delete a node at the end
2. Insert and delete a node at a particular position (if the user enters 5 the node should be inserted at the fifth position in the LinkedList.
3. Insert and delete nodes containing even numbers only.
4. Insert and delete nodes of numbers specified by users. (including min and max numbers).

This is the class node for the variables:
using System;

namespace LinkedList
{
public class Node
{

private object data;
private Node link;
private Node cursor;
private int pos;
private Node next;
private Node prev;

public Node (object d, Node l, Node c)
{
data = d;
link = l;
cursor = c;
}

public object Data
{
get
{
return data;
}
set
{
data = value;
}
}
public Node Link
{
get
{
return link;
}
set
{
link = value;
}
}
public Node Cursor
{
get
{
return cursor;
}
set
{
cursor = value;
}
}
}
}

Formed LinkedList.cs
rivate void btnAddFront_Click(object sender, System.EventArgs e)
{
Node n = new Node(this.tbxData.Text, null, cursor);
n.Link = head;

if (head == null) // linked list is initially empty
{
tail = n;
}
head = n;
size++; // size = size + 1
this.lblSize.Text = size.ToString();
this.Visualize(head);

}

private void btnDeleteFront_Click(object sender, System.EventArgs e)
{
if(head == null) // the linked list is empty
{
MessageBox.Show("Linked List is empty!");
return;
}
head = head.Link;
size--;
this.lblSize.Text = size.ToString();
this.Visualize(head);

}

private void btnAddRear_Click(object sender, System.EventArgs e)
{
Node n = new Node(this.tbxData.Text, null, null);
if(head == null)// the LinkedList is empty
{
head = n;
tail = n;
}
else
{
// connect the last node to the new node
tail.Link = n;
// make tail points to the last node
tail = n;

}
size++; // size = size + 1
this.lblSize.Text = size.ToString();
this.Visualize(head);

}

private void btnDeleteRear_Click(object sender, System.EventArgs e)
{
if(head == null || index <= 0 || index > size)
{
return;

Node prev = null;
Node current = head;

for(int i = 0 ; i < index ; i++)
{
prev = current;
current = current.Link;
}
object temp = current.Data;
if(prev != null)
{
prev.Link = current.Link;
}
else
{
head = current.Link;
}

if(prev.Link == null)
{
tail = prev;
}
size--;
this.lblSize.Text = size.ToString();
this.Visualize(head);

}

/**object temp = null;
if(tail != null) // the linked list is empty
{
temp = tail.Data;
tail = tail.Link;
size--;
this.lblSize.Text = size.ToString();
this.Visualize(head);
}**/

}

private void btnAddPosition_Click(object sender, System.EventArgs e)
{
cursor = head;
for (int i = 1; (i<pos) && (cursor!= null); i++)
{
cursor = cursor.Link;
this.lblPosNum.Text = pos.ToString();
this.Visualize(cursor);

}

}
[4244 byte] By [technocraze] at [2007-11-19 11:07:13]
# 1 Re: C# - LinkedList Issues
I would suggest you do some rigourous debugging of your code. And then point to the locations where you are facing the issues

Secondly, use CODE tags.
exterminator at 2007-11-9 1:49:47 >
# 2 Re: C# - LinkedList Issues
Why you want to implement another collection class? There are so many implementations available in the .NET framework. You could use an ArrayList which works like a LinkedList. But there are LIFO, FIFO implementations too.

No need to implement your own. But I had a look at your code and a few questions.

1. Why there are event handling methods? Do you combined GUI with logic?
2. What goes wrong in your code? Give a little bit more informations at the relevant positions.
3. Why do you not use an existent implementation?
torrud at 2007-11-9 1:50:58 >
# 3 Re: C# - LinkedList Issues
Also your namespace should be "Company-name.Group.Sub-group" or something like. For instance the examples in my articles are always "Darwen.<article name>.<group>.<sub group>".

There's no point to having a "LinkedList.LinkedList" class if you see my point.

Also I agree : why is your linked list form based by the looks of things ?

And you should have an INode interface like this :

namespace TechnoCraze.Collections
{

public interface INode

{
INode Next { get; }
object Payload { get; set; }
}

public class LinkedList
{
private class Node : INode
{
object m_payload = null;
INode m_next = null;

public Node(object payload);
{
m_payload = payload;
}

publc INode Next
{
get
{
return m_next;
}

set
{
m_next = value;
}
}

public object Payload
{
get
{
return m_payload;
}

set
{
m_payload = value;
}
}
}

public INode AddNode(INode insertAfter, object payload)
{
Node node = new Node(payload);
insertAfter.Next = node;
return node;
}
}
}

Get the idea ? This gives the ability to call Next on the node, whilst hiding the implementation from the outside world.

Darwen.
darwen at 2007-11-9 1:51:50 >