TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
357
120.7k
Indexes with specific values not working!?
Aug 14 2013 5:05 PM
Hi
I have a Node class as follows:-
namespace LinkedListDS
{
public class Node
{
public object data;
public Node next;
public Node(object data, Node next)
{
this.data = data;
this.next = next;
}
public object Data
{
get { return this.data; }
set { this.data = value; }
}
public Node Next
{
get { return this.next; }
set { this.next = value; }
}
}
}
My LinkedList class is as follows:-
namespace LinkedListDS
{
public class LinkedList
{
private Node head;
private int count;
public LinkedList()
{
this.head = null;
this.count = 0;
}
public bool Empty
{
get { return this.count == 0; }
}
public int Count
{
get { return this.count; }
}
public object Add(int index, object o)
{
if (index < 0)
throw new ArgumentOutOfRangeException();
if (index > count)
index = count;
Node current = this.head;
if (this.Empty || index == 0)
{
this.head = new Node(o, this.head);
}
else
{
for (int i = 0; i < index - 1; i++)
current = current.Next;
current.Next = new Node(o, current.Next);
}
count++;
return o;
}
}
}
When using the Add method as follows:-
class Program
{
static void Main(string[] args)
{
LinkedList a = new LinkedList();
a.Add(2, "first");
a.Add(77, "second");
a.Add(66, "third");
a.Add(32, "fourth");
Console.WriteLine();
Console.ReadLine();
}
}
The ints im passing in for the index are showing as
2 = 0
77 = 1
66 = 2
32 = 3
So its using the zero based index rather than the values Im passing in?
Also im not sure how the Next property is working to show the next item?
Regards
Reply
Answers (
15
)
Reference Error
about multi threading