?
Solved

Quickie Dynamic memory

Posted on 2003-03-15
5
Medium Priority
?
247 Views
Last Modified: 2010-04-17
Hello Friends,
this is my sample code.
#include <iostream.h>

class Node
{
public:
Node(int);
private:
     int data;
     Node * next;
};

Node::Node(int n)
{
     int x;
     x=n;
}

void main()
{
     int nod;
     cout << "Enter node";
     cin >> nod;

     Node *Head;
     Head = new Node(nod);***********************

}

the line marked with ***'s
after i compile and run this say i enter 3 for nod.
does this mean something like Head[3].

i'm trying to ask the user the number of nodes he wants and then create them dynamically.
is this correct?
Thank you .
Wong
0
Comment
Question by:wong83
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8142549
based on what you wrote 3 will be assigned to a var x that is created in your constructor n that'll be it. I guess the right thing would be Node::Node(int n) : data(n) {}. In this case then you would be new-ing a Node which contains the data 3.
So it would not be correct for Head[3]. I guess you're trying to build a linked-list-like structure???
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 80 total points
ID: 8142584
This code would create n nodes for you, but they are not linked together.

#include <iostream.h>

class Node {
   int dat;
   Node* next;
  public:
   Node(int n) : dat(n) {}
   int disp() { return dat; }
};

int main() {
   int n;
   cin >> n;
   Node** Arr;
   Arr = new Node*[n];
   for(int i(0); i < n; ++i) {
      Arr[i] = new Node(i);
      cout << Arr[i]->disp();
   }
}

If you want to have a linked list, you may add a function into the class to set the pointer next to something (pseudo):

void setNext(Node* ptr) { next = ptr; }
but this is aliasing I'm not sure it's good for what you're doing.
0
 

Author Comment

by:wong83
ID: 8145234
Thanks n_fortynine.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8145646
You're welcome. Does 83 in your username imply anything?  It's my year of birth =).
0
 

Author Comment

by:wong83
ID: 8150380
It was mine too about 4 yrs ago . i've had that(usrname) for a long time so i decided to keep it probably because i don't have to waste a lot of my memory blocks. Thank you for your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Progress
Starting up a Project

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question