Data Structures -- Linked lists / Trees in vb.net

M

Martin

Hi,

I would like to ask if it is possible for a vb.net class to implement a
linked list or a tree.
Basically, I believe VB.net dows not support pointers so therefore I find it
hard to see how VB.net would support
any of these datastructure.

my back ground is in C++, where obviouly this is possible, although recently
I have been looking at VB.net as another developer uses this.

should I even be considering vb.net for these data structure or should I
just go with C# straight away.

any advise is welcome and appreciated.

cheers

martin.
Data Structures -- Linked lists / Trees in vb.net
 
S

Sriram Krishnan

The assumption that you need pointers to do these data structures is wrong.
Plus, since VB.NET and C# are different in terms of sytax only, going with
C# won't help you.
 
K

Kaushik Srenevasan

Thats right, you dont need pointers (in the C sense) to do data structures.
However the notion of pointers still exists but with a slightly different
name called references. Both pointers and references point to areas in
memory (i.e they are stack allocated variables that point to some area of
the heap, in the .NET world, the managed heap), so in terms of manipulating
data structure nodes you should be able to do what you did with pointers,
with references in .NET

for example here is a simple List implementation in C#

namespace DataStructuresDemo{

using System;

class ListNode{
public Object data;
public ListNode next;

public ListNode(Object data, ListNode next){
this.data = data;
this.next = next;
}
}

class List{
private ListNode listHead;
private ListNode listTail;

public void Add(Object data){
ListNode listNode = new ListNode(data,null);
if(listHead == null){
listHead = listTail = listNode;
}
else{
listTail.next = listNode;
listTail = listNode;
}
}

public void Print(){
for(ListNode ln = listHead;ln!=null;ln = ln.next){
Console.WriteLine(ln.data);
}
}
}

class ListDriver{
public static void Main(){
List l = new List();
l.Add(10);
l.Add(20);
l.Add(30);
l.Add(40);
l.Print();
}
}
}
 
K

Kaushik Srenevasan

Thats right, you dont need pointers (in the C sense) to do data structures.
However the notion of pointers still exists but with a slightly different
name called references. Both pointers and references point to areas in
memory (i.e they are stack allocated variables that point to some area of
the heap, in the .NET world, the managed heap), so in terms of manipulating
data structure nodes you should be able to do what you did with pointers,
with references in .NET

for example here is a simple List implementation in C#

namespace DataStructuresDemo{

using System;

class ListNode{
public Object data;
public ListNode next;

public ListNode(Object data, ListNode next){
this.data = data;
this.next = next;
}
}

class List{
private ListNode listHead;
private ListNode listTail;

public void Add(Object data){
ListNode listNode = new ListNode(data,null);
if(listHead == null){
listHead = listTail = listNode;
}
else{
listTail.next = listNode;
listTail = listNode;
}
}

public void Print(){
for(ListNode ln = listHead;ln!=null;ln = ln.next){
Console.WriteLine(ln.data);
}
}
}

class ListDriver{
public static void Main(){
List l = new List();
l.Add(10);
l.Add(20);
l.Add(30);
l.Add(40);
l.Print();
}
}
}
 
D

Daniel O'Connell [C# MVP]

Sriram Krishnan said:
The assumption that you need pointers to do these data structures is
wrong. Plus, since VB.NET and C# are different in terms of sytax only,
going with C# won't help you.

Thats not entirely true. C# supports a few features VB doesn't, and vice
versa. There is more than just syntax differentiaing the two(unless, of
course, you consider teh difference between VB6 compiled to native code and
C++ compiled to native code just syntax differences, especially if they both
use com libraries), but most explicitly C# does support pointers, to a much
more limited extent than C++, but there is support.


However, they have no real bearings on a linked list or other data
structure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top