I having some doubt with struct.
struct node
{
int data;
node* next;
}node; <---- what does this actually do?
Thanks.
add on::
Hi, trying to fix this error..
Line 11: error: expected constructor, destructor, or type conversion before '*' token
compilation terminated due to -Wfatal-errors.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
}node;
node* nodeNew(int newData, node* newNext) // line 11
{
node* n= new node;
n->data= newData;
n->next= newNext;
return n;
}
void listPrint(node* p)
{
while( p!=NULL )
{
cout << p->data << " "; p= p->next;
}
}
int main()
{
}
Is happens when i add that "node" in the struct.
The final line:
}node;
creates a variable with the type struct node, named node. It's equivalent to:
struct node {
int data;
node* next;
};
struct node node;
EDIT: In response to your edit, the line:
node* nodeNew(int newData, node* newNext)
is erroring because node isn't a type. Either change it to:
struct node* nodeNew(int newData, struct node* newNext)
or change the structure declaration to:
typedef struct node node;
struct node {
int data;
node* next;
};
To be exact, it creates an object from given struct in given scope. Word 'variable' is a too generic term.
Related
Here is a simple code that I tried to use a self-reference type and using alias at the same time.
#include <iostream>
class List {
private:
struct node {
int data;
struct node* next;
node(const int& d=0, struct node* n=nullptr) {
data = d; next = n;
}
~node() {};
};
using pNode = struct node*;
pNode head;
public:
List();
~List();
void print() const { std::cout << head->data; }
};
List::List() {
head = new node{55};
}
int main() {
List *a = new List;
a->print();
}
This above works fine. However, I'd rather start the code as shown below:
class List {
private:
using pNode = struct node*;
struct node {
int data;
pNode next;
...
I'd like to place using pNode = struct node* before the struct node definition such that I can use it inside struct node definition as well. I believe that this style of code works fine if I don't use class.
Don't hide pointer semantics in an alias. It's the one "never" advice I always get behind.
And if you agree to only ever use node* in your code, then you can just write
struct node {
int data;
node* next;
// ..
};
C++ introduces a type named node with struct node, unlike C. So we can use natural syntax.
To use the latter you need to forward declare the struct node like so:
struct node;
using pNode = node*;
struct node {
int data;
pNode next;
};
the following code crashes at runtime but works perfectly fine if struct node* a[10] is declared globally.Where does the problem lie.Any insight would be appreciated.
Thank you!
#include<bits/stdc++.h>
using namespace std;
int rear=-1;
int front=-1;
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newnode(int d){
struct node* node1=new node;
node1->data=d;
node1->left=NULL;
node1->right=NULL;
return(node1);
}
void enqueue(struct node* a[],struct node* tempnode){
rear++;
a[rear]=tempnode;
}
struct node* dequeue(struct node* a[]){
front++;
return a[front];
}
void bfs(struct node* root,struct node* a[]){
struct node *tempnode=root;
while(tempnode){
cout<<tempnode->data;
if(tempnode->left)
enqueue(a,tempnode->left);
if(tempnode->right)
enqueue(a,tempnode->right);
tempnode=dequeue(a);
}
}
main() {
struct node* a[10];
struct node* root=newnode(1);
root->left=newnode(2);
root->right=newnode(3);
root->left->left=newnode(-1);
root->left->right=newnode(0);
bfs(root,a);
}
http://www.geeksforgeeks.org/level-order-tree-traversal/
Initialize the array "a" -
int main() {
struct node* a[10] = {NULL};
The problem is not occurring when struct node* a[10] is declared globally because global variables are initialized automatically.
You forget to initialize a:
struct node* a[10]{};
so your dequeue will indeed return nullptr once your queue is empty.
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;
}
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;
};
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;