#include <iostream>
usingnamespace std;
class Node{
public:
int data;
Node *next;
Node(int num, Node *nxt=nullptr) : data(num), next(nxt) {}
staticvoid prepend(Node*& head, int numbers){
head = new Node(numbers, head);
}
void display() {
cout << data;
if (next) {
cout << "->";
next->display();
}
}
};
int main() {
Node *head=nullptr; // or 0 (not NULL) if pre-C++11
for (int i = 0; i < 7; i++)
Node::prepend(head, i);
head->display();
cout << '\n';
return 0;
}
But usually in C++ you take advantage of classes and make a List object to hold the head pointer (and maybe the tail pointer for fast insertion at the end, and maybe the size).
im getting an error saying prepend was not declared in the scope.
Have a look at the line your compiler points out: prepend() is declared inside ‘Node’, so you can’t access it the way you do. You need an object to call methods on (or follow tpb’s advice and make the method static, but it can have its downside).