From a3034d759e51d841d5ce31db8749d5ce95069a03 Mon Sep 17 00:00:00 2001 From: Jose Langarica Date: Tue, 28 Feb 2023 21:22:48 +0700 Subject: [PATCH] Add files via upload added simple LinkedList.cpp --- data-structures/linkedList.cpp | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 data-structures/linkedList.cpp diff --git a/data-structures/linkedList.cpp b/data-structures/linkedList.cpp new file mode 100644 index 00000000..33ef17bc --- /dev/null +++ b/data-structures/linkedList.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +class Node { +public: + int data; + Node* next; + + Node(int data) { + this->data = data; + this->next = NULL; + } +}; + +class LinkedList { +public: + Node* head; + + LinkedList() { + this->head = NULL; + } + + void addNode(int data) { + Node* newNode = new Node(data); + if (this->head == NULL) { + this->head = newNode; + return; + } + Node* currentNode = this->head; + while (currentNode->next != NULL) { + currentNode = currentNode->next; + } + currentNode->next = newNode; + } + + void printList() { + Node* currentNode = this->head; + while (currentNode != NULL) { + cout << currentNode->data << " "; + currentNode = currentNode->next; + } + cout << endl; + } +}; + +int main() { + LinkedList list; + list.addNode(1); + list.addNode(2); + list.addNode(3); + list.addNode(4); + list.addNode(5); + list.printList(); + return 0; +} \ No newline at end of file