Last active
October 11, 2022 16:29
-
-
Save thelinuxpoint/0529d2ac74ebf4cbda919d9e43aecf7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Creating Linked List in C++ */ | |
#include <bits/stdc++.h> | |
// Node | |
class Node { | |
public: | |
int data; | |
Node* next; | |
Node(int data=0){ | |
this->data = data; | |
} | |
}; | |
class NodeUtil { | |
public: | |
static Node *BuildNode(std::initializer_list<int> init){ | |
Node *start = new Node; | |
Node *h = (new Node); | |
start = h; | |
for (int x: init){ | |
h->next = new Node(x); | |
h = h->next; | |
} | |
return start->next; | |
} | |
static void printll(Node *head){ | |
while(head!=NULL and head->next!=NULL){ | |
std::cout<<head->data<<", "; | |
head = head->next; | |
} | |
std::cout<<head->data<<std::endl; | |
} | |
}; | |
int main() { | |
Node *head = NodeUtil::BuildNode({1,1,2,3,11,14,5,6,7,8,11}); | |
NodeUtil::printll(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment