new operator (Error :expected expression before struct) - c++

struct node* NewNode(int data)
{
struct node* node = new(struct node);
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
I am getting this error in first line of the function. Cant figure out whats wrong?
Thanks.

The "new" keyword hints at this being C++. In C++ the "struct TYPENAME" construct is largely obsolete, you can simply use TYPENAME instead.
The C way of typedefing a type name from a named struct is implicit in C++.
node* NewNode(int data)
{
node* pnode = new node;
pnode->data = data;
pnode->left = NULL;
pnode->right = NULL;
return(pnode);
}
should work just fine if this is C++. Please note that using the same name for a type and a variable is not a good idea. Some naming convention (hungarian or anything) helps.

This code compiles perfectly fine under Comeau try-it-out:
#define NULL 0
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* NewNode(int data)
{
struct node* node = new(struct node);
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}

Related

structure not created for Node but at time of initializing a node if we write struct before it ,the code is working fine, No Errors.Why?

class Node
{
public:
int value;
Node *next;
};
class LinkedList
{
private:
Node *head;
public:
LinkedList(void) { head = NULL; }
~LinkedList(void){};
void insertAtHead(int);
void insertAtLocation(int, int);
void Delete(int);
void displayList();
int countList();
};
void LinkedList::insertAtHead(int new_value)
{
struct Node *newNode, *NodePtr = head;
newNode = new Node;
newNode->value = new_value;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
newNode->next = NodePtr;
head = newNode;
}
cout << "Node successfully inserted at the head\n\n";
}
I didn't create any structure for the node, rather I made a class for it. But I don't know why my code is working properly when I make a newNode in the insertAtHead function by writing struct at the start of initialization, What is happening there? no, compile and run time error when the struct is written before Node. What is the concept behind this work?
There is no difference between struct and class that is relevant to this code. So you can switch them around as you wish with no consequences.
This
struct Node *newNode, *NodePtr = head;
is a C-ism. In C++ you would write:
Node *newNode, *NodePtr = head;
Moreover a struct is a class in C++. struct and class are just two different keywords. Only for default access they make a difference when used to define a class. See here for more details on that: When should you use a class vs a struct in C++?.

C++ struct was not declared this scope

Using struct like linked list creates problem.
When I declare 2 values (my struct),
it returns error:
this VALUE_NAME was not declared this scope
This is part of the source.
struct _node
{
int val;
_node* prev;
_node* next;
};
typedef struct _node node;
node* HEAD;
void deleteALLNode()
{
node* node = HEAD;
node* _tmp;
//
// here _tmp value return error
//
}
You cannot name your variable with same name, as type (you can one time maybe, but than you cannot use the type without compilation error anymore). Just change name of node* node = HEAD; variable to something else, like node* pNode = HEAD;
If it's C++, you can use _node directly, no need to typedef. typedef a struct is C style.
struct _node
{
int val;
_node* prev;
_node* next;
};
_node* HEAD;
void deleteALLNode()
{
_node* node = HEAD;
_node* _tmp;
}

Linked List in C

Heres a snippet from the code I am trying to complete for building a linked list. for some reason I keep getting the error "error: expected ‘;’, identifier or ‘(’ before ‘struct’ " when trying to compile the code. Can someone help me out.
struct node;
struct node* buildList(int x);
void push(struct node** headRef, int data);
int findLen(struct node** headRef);
struct node{
int data;
struct node* next;
}
struct node* buildList(int x){
struct node* head = NULL;
head = malloc(sizeof(struct node));
head->data = x;
head->next = NULL;
return head;
}
Try putting a semicolon after the struct declaration
struct node{
int data;
struct node* next;
};

pointer operation in c & c++

struct node {
int data;
struct node* next;
}
void push (struct node **head, int data) {
struct node* newNode = malloc (sizeof (struct node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
//I understand c version well.
C++ version
void Stack::push( void *data ) {
struct node *newNode = new node;
newNode->data = data;
newNode->next = head;
head = newNode;
}
In c++ head is a private or protected member of stack class and is declared as node *head.
Question: why head can preserve its value after push() call in c++.
In c, we need to declare it as ** since we want to change the value of head pointer after the push() function call. In c++ code, won’t changes to head be lost after the call?
The problem here is the C code you're comparing to C++ is not really analogous. A better example would be
typedef struct Node {
int data;
struct Node* pNext;
} Node;
typedef struct Stack {
Node* pHead;
} Stack;
void push(Stack* this, int data) {
Node* newNode = malloc (sizeof (Node));
newNode->data = data;
newNode->next = this->head;
this->head = newNode;
}
In this version we've successfully implemented push without having to take a ** for head. We have in a way because it's double indirected via the Stack*. But this is very similar to how C++ works. It's possible to view C++ as passing this as a hidden argument to the function.
In this case, since Stack::push is non-static, head is a shorthand for this->head. So the head = newNode is the same as:
this->head = newNode;

Linkedlist Node in C+

I am learning a book on data structures, and complied their node in linked list example, and I receive this error:
and Everything.cpp|7|error: expected unqualified-id before "int"|
and Everything.cpp|7|error: expected `)' before "int"|
||=== Build finished: 2 errors, 0 warnings ===|
The code for the node is:
typedef struct Node
{
struct Node(int data) //Compile suggest problem is here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;
I am not familiar with structs and I am using Code::blocks to compile. Does anyone know whats wrong?
The code sample is wrong. There should not be the keyword struct in front of the constructor declaration. It should be:
typedef struct Node
{
Node(int data) // No 'struct' here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;