Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Node {
- public:
- int data;
- Node *next;
- Node() {
- cout << "Node object is being created" << endl;
- }
- };
- void printList(Node *node) {
- while(node != NULL) {
- cout << node->data << ",";
- node = node->next;
- }
- cout << endl;
- }
- void push(Node **node, int data) {
- // // working V1 start
- // Node *newNode = new Node();
- // newNode->data = data;
- // newNode->next = *node;
- // *node = newNode;
- // // working V1 end
- // not working V2 start
- Node newNode;
- newNode.data = data;
- newNode.next = *node;
- *node = &newNode;
- // not working V2 end
- }
- int main() {
- Node *a = NULL;
- push(&a, 15);
- push(&a, 10);
- printList(a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement