Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 4
Captain X The "Node *ptr" is local to the function "insertbeg" you can't access it outside in "print" function
Declare it global or pass pointer to function as parameter.
Solution using global declaration:
#include <iostream>
using namespace std;
struct Node{
int data;
Node *link;
};
Node *head = NULL;
Node *ptr;
void insertbeg (int x){
ptr = new Node();
ptr ->data =x;
ptr ->link=head;
head = ptr;
}
void print(){
cout << ptr->data;
ptr=ptr->link;
}
int main() {
insertbeg (2);
insertbeg (3);
print ();
return 0;
}