#include<bits/stdc++.h>
using namespace std;
//Node using class
class Node{
public:
int data;
Node* next;
Node(int data, Node* next = nullptr){
this->data = data;
this->next = next;
}
};
int main(){
//Create Nodes
Node* node1 = new Node(5);
Node* node2 = new Node(6);
Node* node3 = new Node(7);
Node* node4 = new Node(8);
//Create links
Node* head = node1;
node1->next=node2;
node2->next=node3;
node3->next=node4;
//print the list
cout<<"First List: "<<endl;
while(head != NULL){
cout<<head->data<<endl;
head = head->next;
}
//Create a list dynamically
Node* newNode = new Node(1);
Node* newList = newNode;
Node* newhead = newNode;
int i=2;
while(i<20){
Node* temp = new Node(i);
newList->next = temp;
newList = newList->next;
i++;
}
cout<<"Second List:"<<endl;
//Print the list
while(newhead != nullptr){
cout<<newhead->data<<endl;
newhead= newhead->next;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vTm9kZSB1c2luZyBjbGFzcwoKY2xhc3MgTm9kZXsKCXB1YmxpYzoKICBpbnQgZGF0YTsKICBOb2RlKiBuZXh0OwoKICBOb2RlKGludCBkYXRhLCBOb2RlKiBuZXh0ID0gbnVsbHB0cil7Cgl0aGlzLT5kYXRhID0gZGF0YTsKCXRoaXMtPm5leHQgPSBuZXh0OwogIH0KfTsKCgppbnQgbWFpbigpewoJLy9DcmVhdGUgTm9kZXMKCU5vZGUqIG5vZGUxID0gbmV3IE5vZGUoNSk7CglOb2RlKiBub2RlMiA9IG5ldyBOb2RlKDYpOwoJTm9kZSogbm9kZTMgPSBuZXcgTm9kZSg3KTsKCU5vZGUqIG5vZGU0ID0gbmV3IE5vZGUoOCk7CgkKCS8vQ3JlYXRlIGxpbmtzCgoJTm9kZSogaGVhZCA9IG5vZGUxOwoJbm9kZTEtPm5leHQ9bm9kZTI7Cglub2RlMi0+bmV4dD1ub2RlMzsKCW5vZGUzLT5uZXh0PW5vZGU0OwoKCgkvL3ByaW50IHRoZSBsaXN0Cgljb3V0PDwiRmlyc3QgTGlz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