I am making this program in data-structures and i get the following error that node1 is not declared in this scope also head was not declared in this scope and size also null i dont understand what the problem is
#include<iostream>
#include<conio.h>
using namespace std;
struct node // I have created the struct of node here
{
int data;
struct node *next;
};
int main() {
node1;
node1 *head;
head = (node *) malloc(size of (node1));
if(head=null)
return;
head->data=1;
head->next=Null;
head->next=(node*)malloc(size of(node1));
head->next->data=2;
head->next->next=Null;
node1 *current=head;
while(current!Null)
{
cout<<current->data;
current=current->next;
}
}
struct node
{
/*
*/
};
node1 head;
node isn't node1;
also, it's C, not C++
Where have you defined node1? You have a struct named node. Also if you want to use node in your code instead of struct node everywhere, I recommend you usea typedef as:
typedef struct node {
//your node data members
}node;
Related
I'm currently working on my homework. Below is the program for construction of binary tree which I wrote. I'm getting the error that, "cannot convert 'node' to 'int*' in assignment". Can you please help me out here?**
#include<iostream>
#include<conio.h>
struct node
{
char data;
int *left,*right;
};
int main()
{
node *T; //ROOT of tree
node *p,*q; //address of first node in T
T=new node;
T->left=NULL;
T->right=NULL;
T->data='A';
p=new node;
p->left=NULL;
p->right=NULL;
p->data='B';
T->left=&p;
p=new node;
p->left=NULL;
p->right=NULL;
p->data='C';
T->right=&p;
q=new node;
q->left=NULL;
q->right=NULL;
q->data='D';
p->left=&q;
return 0;
}```
Change your structure definition to
struct node
{
char data;
node *left,*right; // <- node * here
};
Tree node has a recursive definition.
Change your structure as #TarekD suggested, the problem is you are trying to cast a node* into an int*
T->left=&p;
T->right=&p;
p->left=&q;
I ran into this bit of code for a c++ linked list implementation.
struct node
{
int info;
struct node *next;
}*start;
What does this mean to have *start rather than just start??
What happens when it is later used like this? What does s mean it is not referenced anywhere else in the function?
struct node *temp, *s;
temp = new(struct node);
Fragment
struct node
{
int info;
struct node *next;
}*start;
is equivalent to
struct node
{
int info;
struct node *next;
};
struct node *start;
So in your first fragment, you define a structure named node and a variable named start of type struct node * within one statement. That's all.
Note that in C++ (unlike in C), you could also write
struct node
{
int info;
node *next;
};
node *start;
i.e. you can omit the struct-keyword when defining variables of type struct node.
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class sequence{
struct node{
string name;
string data;
struct node * next;};
sequence();
~sequence();
void print(struct node *);
struct node *sortlist(struct node *);
struct node *deletenode(struct node *, string);
struct node *searchnode(struct node *, string);
struct node *insertnode(struct node *, string);};
void print(struct node *st){ //method to print the linked list
while(st!=0){
cout<<st->data<<endl;
st=st->next;}}
struct node *deletenode(struct node *st, string x){//to delete a node containing element x
struct node *s1=st, *t,*ptr;
string m=x;
ptr=st;
if (s1==0)// if linked list is empty {
cout<<"linkedlist empty"<<endl;}
else if(s1->next==0)//if linked list contains only one element
{
if(s1->data==m)
{
free(s1);
s1=0;}
else
cout<<m<<"is not in the list."<<endl;
}
else if(s1->next!=0&&s1->data==m)
{
t=s1->next;
free(s1);
s1=t;
}
else
{
while(s1->data!=m&&s1->next!=0)
{
t=s1;
s1=s1->next;
}
if(s1->data==m)
{
t->next=s1->next;
free(s1);
s1=t;
}
else
cout<<m<<"is not in the list."<<endl;
}
return(ptr);
}
int main(){
sequence obj=new sequence();
struct node *root1, *root2, *root3, *s, *p,*t;
string v;
root1= new node;
root2= new node;
root3= new node;
s=root1;
root1->next=root2;
root2->next=root3;
root3->next=0;
root1->data="man";//data in the nodes of linked list
root2->data="aan";
root3->data="van";
root1->name="1";
root2->name="2";
root3->name="3";
cout<<"enter the string :";
cin>>v;
cout<<endl;
p=obj.deletenode(s, v);// delete node function call
obj.print(p);
return(0);}
Problem 1: when i am running this code, it executes with bug in deletenode method where it is not deleting the first element of linked list but deleting every other element.Please enlighten me where i am getting wrong in the code.
Problem 2: I was trying to create a class with all above mention methods, a constructor and a destructor but when i am running this code i am getting errors like "invalid use of incomplete type 'struct node'".I am new to the concept of classes kindly guide me where i am getting wrong in this code.
Apologies for no proper formatting.
looking for a positive reply.
well you are not supposed to define a whole Structure within the class definition
, instead of doing this define the structure publicly and include its instance within the class . Hope this Helps
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct node{ //structure definition outside the class
string name;
string data;
struct node * next;
};
class sequence{
node node1; //define a node variable
sequence();
~sequence();
void print(struct node *);
struct node *sortlist(struct node *);
struct node *deletenode(struct node *, string);
struct node *searchnode(struct node *, string);
struct node *insertnode(struct node *, string);};enter code here
Wouldn't it be the same to just have an embedded object of the same structure that is not a pointer?
Struct Node
{
int data;
Node next;
};
//vs
Struct Node
{
int data;
Node * next;
};
No!
Having the following struct:
struct Node {
Node other;
};
Is illegal! Node doesn't have a defined size; the compiler can't build it correctly. A Node would contain a Node which would contain a Node.. wait what?
A pointer however is fine, it just points to a section of memory. When defining a pointer, the type that it points to doesn't have to be complete, it just has to defined.
struct Node;
int main() {
Node* a; // Fine, no errors.
Node b; // Incomplete type error
}
Looked at every similar question on this compiler error. The following minimized code reproduces the error and I cannot see what the issue is. From reading here on SO, suspect it's the return type node* (being a struct) is invalid, but what else to specify as the return type? Thank you.
Header file:
#include<cstdio>
#include<cstdlib>
class double_clist {
struct node {
int info;
struct node *next;
struct node *prev;
};
node *start;
node *last;
int counter;
public:
node *create_node(int);
double_clist() {
start = NULL;
last = NULL;
}
};
Implementation File:
#include<cstdio>
#include<cstdlib>
node* double_clist::create_node(int value) { // Error on this line.
counter++;
struct node *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
temp->prev = NULL;
return temp;
}
When it reaches node here, it hasn't seen that it is inside double_clist yet. You need to also preface that with double_clist::.
double_clist::node* double_clist::create_node(int value) {