From f82170857033e16d2a26dd52d459ff61a9082dca Mon Sep 17 00:00:00 2001 From: mohitcharkha Date: Wed, 12 Oct 2022 12:57:06 +0530 Subject: [PATCH] Added Linked list in c++ --- CPP/DataStructures/linked-list.cpp | 130 +++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 CPP/DataStructures/linked-list.cpp diff --git a/CPP/DataStructures/linked-list.cpp b/CPP/DataStructures/linked-list.cpp new file mode 100644 index 0000000..58cf6fe --- /dev/null +++ b/CPP/DataStructures/linked-list.cpp @@ -0,0 +1,130 @@ +#include +using namespace std; + +class Node { +public: + int data; + Node* next; + + Node() + { + data = 0; + next = NULL; + } + + Node(int data) + { + this->data = data; + this->next = NULL; + } +}; + +class Linkedlist { + Node* head; + +public: + Linkedlist() { head = NULL; } + + void insertNode(int); + + void printList(); + + void deleteNode(int); +}; + +void Linkedlist::deleteNode(int nodeOffset) +{ + Node *temp1 = head, *temp2 = NULL; + int ListLen = 0; + + if (head == NULL) { + cout << "List empty." << endl; + return; + } + + while (temp1 != NULL) { + temp1 = temp1->next; + ListLen++; + } + + if (ListLen < nodeOffset) { + cout << "Index out of range" + << endl; + return; + } + + temp1 = head; + + if (nodeOffset == 1) { + + head = head->next; + delete temp1; + return; + } + + while (nodeOffset-- > 1) { + + temp2 = temp1; + + temp1 = temp1->next; + } + + temp2->next = temp1->next; + + delete temp1; +} + +void Linkedlist::insertNode(int data) +{ + Node* newNode = new Node(data); + + if (head == NULL) { + head = newNode; + return; + } + + Node* temp = head; + while (temp->next != NULL) { + + temp = temp->next; + } + + temp->next = newNode; +} + +void Linkedlist::printList() +{ + Node* temp = head; + + if (head == NULL) { + cout << "List empty" << endl; + return; + } + + while (temp != NULL) { + cout << temp->data << " "; + temp = temp->next; + } +} + +int main() +{ + Linkedlist list; + + list.insertNode(1); + list.insertNode(2); + list.insertNode(3); + list.insertNode(4); + + cout << "Elements of the list are: "; + + list.printList(); + cout << endl; + + list.deleteNode(2); + + cout << "Elements of the list are: "; + list.printList(); + cout << endl; + return 0; +} \ No newline at end of file