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;
};
Related
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;
}
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.
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);
}
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;