Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2005, 01:10 PM
Registered User
 
Join Date: Jul 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Object Linked List

Can anyone guide me in the right direction on use of linked lists in C#. Is there a .NET object to handle this now. I have developed linked lists in C / Visual C++. Typically, I have an object containing a next pointer and a previous pointer to the same type of object. A linked list manager object exists to provide a interface for the list. The user can add and delete dynamically.

Do I need to look at unsafe objects due to the pointers ? What is the Collection object in .NET ? Is that similair to my linked lists in C ??

Any help would be appreciated !

BMM
 
Old July 11th, 2005, 08:30 PM
Authorized User
 
Join Date: Feb 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There's no built-in .NET class for this, but you can do this with nested classes without pointers:

public class LinkedList
{
    public class Node
    {
        internal object MyData;
        internal int Next;
        internal int Previous;
    }
    private ArrayList MyList = new ArrayList();
    //methods to add, remove, etc.
}



 
Old July 12th, 2005, 08:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just use the ArrayList; I am pretty sure that it basically is implemented as a linked list, and there are methods for moving around in the list so therefore you should not need the next and previous pointers.

There are Add and Remove methods in the ArrayList class. Here is some example code...
Code:
ArrayList list = new ArrayList();

list.Add("something");
list.Add("somethingelse");

IEnumerator i = list.GetEnumerator();
while(i.MoveNext())
    Console.WriteLine(i.Current);

if(list.Count > 0)
    Console.WriteLine(list[0]);
    Hope it helps, Jacob.
 
Old July 12th, 2005, 11:04 AM
Authorized User
 
Join Date: Feb 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

D'oh...

You're right Jacob - here I was getting too complicated - ArrayList does indeed seem to stand on its own as a LinkedList.


 
Old July 12th, 2005, 02:28 PM
Registered User
 
Join Date: Jul 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How does ArrayList handle a delete of an object in the middle of the list ? It appears to still be an array ? Am I still chewing up memory for the array, even tough I may have deleted objects . Is it dynamic in memory use ?
 
Old July 12th, 2005, 04:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey, I just did some experiments with ArrayList, but I didn't know how to see if the memory is deallocated. However, I am quite sure that memory is deallocated, since when an item is removed in the list the items shift index; i.e. it is reduced in size. In C# you do not have to think about allocation/deallocation directly as in C.

Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
Linked LIst Progam in C Peter_APIIT C++ Programming 1 December 1st, 2007 05:16 AM
2D Linked List sjf905 C# 0 October 8th, 2006 11:02 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.