?
Solved

Quickie Dynamic memory

Posted on 2003-03-15
5
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans
Suggested Courses

752 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