Below is the code please help I am new to c
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
} *first = NULL;
void Create(int A[], int n)
{
int i;
struct node *t, *last;
first = new struct node *(sizeof(struct node));
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < n; i )
{
t = new struct node *(sizeof(struct node));
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(int *p)
{
while (p != NULL)
{
cout << p.data;
p = p.next;
}
}
int main()
{
int A[] = {3, 5, 7, 9, 15}
// struct node n1;
Create(A, 5);
Display(first);
return 0;
}
The errors are
- invalid conversion from 'unsigned int' to 'node*' [-fpermissive] first = new struct node* (sizeof(struct node));
- 14:50: error: cannot convert 'node**' to 'node*' in assignment first = new struct node* (sizeof(struct node));
CodePudding user response:
First off, this looks like you're trying to convert C code to C .
I'm assuming the original C code looks something like this:
first = (struct node*) malloc(sizeof(struct node));
C's malloc accepts a size in bytes. C 's new
will know the size of the node struct automatically:
first = new node();
To explain the error message: new struct node *(sizeof(struct node))
is trying to allocate a pointer to node node*
using an unsigned int sizeof(struct node)
.
Also keep in mind that many things considered normal in C are weird in C . Here are some suggestions:
- Use
nullptr
instead ofNULL
. NULL can easily turn into an integer 0 accidentally. - You don't need
struct
when declaring variables,struct node *t, *last;
can be changed tonode *t, *last;
- You can initialize the whole struct all at the same time, instead of setting the members afterwards:
first = new node{A[0], nullptr};
- Arrays are NOT null-terminated, so the loop inside Display will glitch out. A better way to do this is to use std::array, std::vector, or just pass a count into the function. Also, this function tries to use ints as nodes? Assuming it isn't complete yet.
- Read about 'smart pointers' as it will lead to safer code that doesn't leak memory.
CodePudding user response:
The problem is that you are using new
incorrectly. You are asking it to create a new node*
pointer initialized with the return value of sizeof
, which is wrong. You should instead be asking it to create a new node
object.
Also, your Create()
can be simplified. And your Display()
is coded wrong. And you are leaking the list.
Try this instead:
#include <iostream>
using namespace std;
struct node
{
int data;
node *next = nullptr;
};
void Create(node* &first, int A[], int n)
{
node **curr = &first;
for (int i = 0; i < n; i)
{
node *t = new node;
t->data = A[i];
*curr = t;
curr = &(t->next);
}
}
void Destroy(node *p)
{
while (p)
{
node *n = p->next;
delete p;
p = n;
}
}
void Display(node *p)
{
while (p)
{
cout << p->data << ' ';
p = p->next;
}
}
int main()
{
int A[] = {3, 5, 7, 9, 15}
node *n1;
Create(n1, A, 5);
Display(n1);
Destroy(n1);
return 0;
}
That being said, you really should use std::list
instead:
#include <iostream>
#include <list>
using namespace std;
void Display(list<int> &l)
{
for (int val : l)
{
cout << val << ' ';
}
}
int main()
{
int A[] = {3, 5, 7, 9, 15};
list<int> n1(begin(A), end(A));
Display(n1);
return 0;
}