From 9ba6ef0b9746ec6c7d4e635eda27d480cb8772c8 Mon Sep 17 00:00:00 2001 From: Sunil-Kumar56 <83457906+Sunil-Kumar56@users.noreply.github.com> Date: Wed, 20 Oct 2021 14:45:57 +0530 Subject: [PATCH] Create dsa_c++ --- dsa_c++ | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dsa_c++ diff --git a/dsa_c++ b/dsa_c++ new file mode 100644 index 0000000..8ebd244 --- /dev/null +++ b/dsa_c++ @@ -0,0 +1,48 @@ +this file contain c++ +// A simple C++ program for traversal of a linked list +#include +using namespace std; + +class Node { +public: + int data; + Node* next; +}; + +// This function prints contents of linked list +// starting from the given node +void printList(Node* n) +{ + while (n != NULL) { + cout << n->data << " "; + n = n->next; + } +} + +// Driver code +int main() +{ + Node* head = NULL; + Node* second = NULL; + Node* third = NULL; + + // allocate 3 nodes in the heap + head = new Node(); + second = new Node(); + third = new Node(); + + head->data = 1; // assign data in first node + head->next = second; // Link first node with second + + second->data = 2; // assign data to second node + second->next = third; + + third->data = 3; // assign data to third node + third->next = NULL; + + printList(head); + + return 0; +} + +// This is code is contributed by rathbhupendra