Question

In c++, what alternative to malloc line? Such as struct Node* newNode(int data) { struct Node*...

In c++, what alternative to malloc line?

Such as

struct Node* newNode(int data)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
  
return node;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

malloc() function dynamically allocates the memory in C++. But in C++, there is also anoter way to dynamically allocate the memory. We can do this using the new keyword.

So, the code will be

#include<iostream>

using namespace std;

struct Node{

    int data;

    struct Node *left;

    struct Node *right;

};

struct Node* newNode(int data)

{

    // dynamically allocate memory

    struct Node* node = new( struct Node ); //(struct Node*)malloc(sizeof(struct Node));

    node->data = data;

    node->left = NULL;

    node->right = NULL;

     

    return node;

}

int main()

{

   

}

Add a comment
Know the answer?
Add Answer to:
In c++, what alternative to malloc line? Such as struct Node* newNode(int data) { struct Node*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ā€˜^ā€™ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

  • struct Node * new_11( void ) { // return a new node to be the list...

    struct Node * new_11( void ) { // return a new node to be the list anchor struct Node * node = (struct Node *) malloc(sizeof(struct Node)); memset(node, o, sizeof(struct Node)); return node ; struct Node * find_l1(struct Node * anchor, char * word) { // given a pointer to the anchor of the list, and a word, search // the list for the word. return the pointer to the with the word, if found, 7/ or NULL if not...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle ā€¢ Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • #include <stdlib.h> int main() { struct fruit Z; food X = (struct fruit *)malloc(sizeof(struct fruit)); X->taste[4]...

    #include <stdlib.h> int main() { struct fruit Z; food X = (struct fruit *)malloc(sizeof(struct fruit)); X->taste[4] = (struct flavour *)malloc(sizeof(struct flavour)); X->taste[4]->score = 5; X->link = &Z; return 0; } Given the code above, reverse engineer the necessary data structure and other definitions not shown above. Give your answer as C code.

  • C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

  • Please explain step by step what is going on each step.please clearly explain line by line...

    Please explain step by step what is going on each step.please clearly explain line by line the working of code . Also provide the output. I would rate positively. Thank you so much . #include <stdio.h> #include <stdlib.h> struct  node  {       int data;       struct  node  *next;   };   struct  node  *head = NULL; void  printList() {       struct  node  *ptr  = head;       printf("\nhead:");            while(ptr !=  NULL) {             printf("node  addr:%p   \tdata:%d   \tnext addr:%p\nā€, ptr,ptr->data,ptr->next);                            ptr = ptr->next;         }       printf("  [null]\n"); } void  insert(int  data) {       struct  node  *link = (struct node*)        malloc(sizeof(struct  node));       link->data  = data;       link->next  = head;       head  = link; } int main()  {...

  • Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p,...

    Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p, q, *newNode; newNode = new node Type; 1. Write C++ statement to store 50 in the info field of the newNode. 2. Write C++ statement to set the link field of the newNode to NULL. 3. Write C++ statement to make head pointer points to newNode. 4. Write C++ statement to delete the first node in the linked list. (the first an the only...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT