Blue Theme Orange Theme Green Theme Red Theme
 
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » allocating objects in C#
       
Author Reply
Marco
posted 35 posts
since Sep 16, 2007 
from

 allocating objects in C#
  Posted on: 30 Oct 2007       
Hello, I'm a newbie C#er...
I have just one question: I'm trying to code my own linked list;
I tried to write in C# and after I'll explain my doubt:

class Node {
    public Node (int v) { next = null; val =n; }
    Node next;   
    int val;
   
}

class LinkedList {
     LinkedList () {start=null;}
     Node start;
     public  addNode( Node n) { if (start = null) start = n; else ......}
}
//main
LinkedList l = new LinkedList();
l.add (new Node (10));
...........................

Now: we focus on insert of first Node..... start node will take '10' but I don't initialized anywhere 'start' object. Is it right? Shouldn't be something like start = new Node () anywhere?
I hope you'll understand....
thanks...
Alan
posted  1600 posts
since  Jun 02, 2007 
from 

Re: allocating objects in C#
  Posted on: 31 Oct 2007      0 0    

Yes, I think it would be more natural and convenient to initialize your 'start' node when you create a new LinkedList:

class LinkedList
{
     public LinkedList() {start = new Node(10);}
     Node start;
     public addNode(Node n) { // code to add new nodes}
}

Marco
posted  35 posts
since  Sep 16, 2007 
from 

Re: allocating objects in C#
  Posted on: 31 Oct 2007      0 0    
I wonder only if it won't crash.........
I insert the element from out LinkedList......and inside linkedlist I don't know how Node must be initialized.

Question : Listd.Add(node), does it copy the node doens't it? thanks
Alan
posted  1600 posts
since  Jun 02, 2007 
from 

Re: allocating objects in C#
  Posted on: 31 Oct 2007      0 0    

When you add a Node as an argument to the addNode() method,  it's a reference to the Node object that gets copied, not the Node itself.

As well as a start node, you also need a 'tail' node to hold a reference to the last node added, plus a 'count' variable to hold the number of nodes added.

The following code seems to work OK and should give you a more solid base on which to develop your LinkedList class: 

using System;

class Test
{
    static void Main()
    {
      LinkedList li = new LinkedList();
      Node n = new Node(11);
      li.addNode(n);
      n = new Node(12);
      li.addNode(n);
      n = new Node(13);
      li.addNode(n);
      n = new Node(14);
      li.addNode(n);
      li.listNodes();
      Console.ReadKey();
    }

}

class Node
{

    Node next = null;   
    int val;


    public Node (int v)
    {
      val = v;
    }


    public Node Next
    {
        get { return next; }
        set { next = value;}   
    }

    public int Value
    {
        get { return val; }
    } 
}


class LinkedList
{

     Node start;
     Node tail;
     int count = 1;

     public LinkedList()
     {
        start = new Node(10);
     }

     public void addNode(Node n)
     {
         if (count == 1)
         {
            start.Next = n;
            tail = n;
         }
         else
         {
            tail.Next = n;
            tail  = n;
         }
         count++;
     }

     public void listNodes()
     {
         int num = 1;
         Node n = start;
         while (num <= count)
         {
           Console.WriteLine("Value of Node {0} = {1}", num, n.Value);
           n = n.Next;
           num++;
         }
     }            
}

Alan
posted  1600 posts
since  Jun 02, 2007 
from 

Re: allocating objects in C#
  Posted on: 01 Nov 2007      0 0    

I was thinking some more about this and you don't in fact need a 'count' variable as tail will be null when the list has just been created and tail.Next will be null when you reach the end of the list. So you only need to test for null when adding new nodes or traversing the list.

You could therefore replace the code in the LinkedList class with the following:

class LinkedList
{

     Node start;
     Node tail;    

     public LinkedList()
     {
        start = new Node(10);
     }

     public void addNode(Node n)
     {
         if (tail == null)
         {
            start.Next = n;
         }
         else
         {
            tail.Next= n;
         }
         tail = n;
     }

     public void listNodes()
     {
         int num = 1;
         Node n = start;
         do
         {
           Console.WriteLine("Value of Node {0} = {1}", num, n.Value);
           n = n.Next;
           num++;
         }
         while (n != null);
     }            
}

       
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 3.2009.8.27
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved