diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/01-linked-list.md b/Data-Structures/Linear-Data-Structures/Linked-List/01-linked-list.md index 842432f..4ea192b 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/01-linked-list.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/01-linked-list.md @@ -1,19 +1,20 @@ # Linked Lists. + + **Table of content :** -1. Why do we need Linked list ? -2. What is a Linked List ? -3. Structure of a Linked List. -4. Linked List Terminologies. -5. Depiction of a Linked list. +- [Why do we need Linked list ?](#why-do-we-need-linked-list) +- [What is a Linked List ?](#what-is-a-linked-list) +- [Structure of a Linked List](#structure-of-a-linked-list) +- [Linked List Terminologies](#linked-list-terminologies) +- [A depiction of a Linked list in heap memory](#a-depiction-of-a-linked-list-in-heap-memory) + --- ### Why do we need Linked list ? -Before konwing the defination, let's first know its need. - We need a data structure which is dynamic in nature, means it should not have a fixed size. The size could be changed during the run-time. An **array** with size 10 will be allocated a fixed 10 sized in memory and hence it cannot span or contract during the run-time. @@ -52,7 +53,7 @@ Let's call this self defined data type a **NODE**. Now each node will have two h ### Linked List Terminologies -1. **Head node** : The first node of Linked list is the head node and acts as a starting point for most of the opertions. +1. **Head node** : The first node of Linked list is the head node and acts as a starting point for most of the operations. 2. **Tail node** : The last node of Linked list which points to the NULL Pointer 0x0. --- diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/02-node-creation.md b/Data-Structures/Linear-Data-Structures/Linked-List/02-node-creation.md index 59bb94b..567530e 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/02-node-creation.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/02-node-creation.md @@ -9,24 +9,24 @@ The self defined data type to create a node could be a struct or a class. using namespace std; class Node { -public: - // node members - int data; // data - Node *next; // pointer + public: + // node members + int data; // data + Node* next; // pointer - // constructor - Node(int val) { - data = val; - next = nullptr; - } + // constructor + Node(int val) { + data = val; + next = nullptr; + } }; int main() { - vector arr = {2, 3, 4, 6, 3}; - Node *n1 = new Node(arr[1]); // creating a new node - cout << n1->data << endl; // print 3 - cout << n1->next << endl; // prints Null address - return 0; + vector arr = {2, 3, 4, 6, 3}; + const Node* n1 = new Node(arr[1]); // creating a new node + cout << n1->data << endl; // print 3 + cout << n1->next << endl; // prints Null address + return 0; } ``` diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/03-traversal.md b/Data-Structures/Linear-Data-Structures/Linked-List/03-traversal.md index e39d614..ad4de88 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/03-traversal.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/03-traversal.md @@ -8,16 +8,14 @@ - move the temp node ahead by assigning the temp node to its next node. ```cpp -class Solution -{ - public: - void display(Node *head) - { - Node *temp = head; - while(temp != NULL){ - std::cout << temp->data << " "; - temp = temp->next; - } +class Solution { + public: + void display(Node* head) { + Node* temp = head; + while (temp != NULL) { + std::cout << temp->data << " "; + temp = temp->next; + } } }; ``` @@ -26,4 +24,4 @@ class Solution ### Depiction of traversal -![Linked-list-traversal](https://github.com/amitsuthar69/assets/blob/main/linked-lists/linked-list-traversal.png?raw=true) +![linked-list-1](https://github.com/Rishabh672003/Programming-Notes/assets/53911515/98f79c73-5949-4bd7-8001-7201c0f46e44) diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/69-insertion.md b/Data-Structures/Linear-Data-Structures/Linked-List/04-insertion.md similarity index 100% rename from Data-Structures/Linear-Data-Structures/Linked-List/69-insertion.md rename to Data-Structures/Linear-Data-Structures/Linked-List/04-insertion.md diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/reverseLinkedList.md b/Data-Structures/Linear-Data-Structures/Linked-List/reverseLinkedList.md index f7d879c..8bd0ffa 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/reverseLinkedList.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/reverseLinkedList.md @@ -16,8 +16,9 @@ ### Imlementation : ```cpp -#include -using namespace std; +#include + +using std::cout, std::cin, std::endl; template class LinkedListNode { public: diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp b/Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp index d00b4e5..ea5c261 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp +++ b/Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp @@ -1,88 +1,88 @@ -#include -#include -using namespace std; +#include + +using std::cout, std::endl; class Node { -public: - int data; - Node *next; - - Node(int data) { - this->data = data; - this->next = NULL; - } + public: + int data; + Node* next; + + Node(int data) { + this->data = data; + this->next = NULL; + } }; -void insertAtHead(Node *&head, int data) { - Node *temp = new Node(data); - temp->next = head; - head = temp; +void insertAtHead(Node*& head, int data) { + Node* temp = new Node(data); + temp->next = head; + head = temp; } -void insertAtTail(Node *&tail, int data) { - Node *temp = new Node(data); - tail->next = temp; - tail = temp; +void insertAtTail(Node*& tail, int data) { + Node* temp = new Node(data); + tail->next = temp; + tail = temp; } -void insertAtPosition(Node *&head, Node *&tail, int pos, int data) { - Node *temp = head; - int cnt = 1; - - // if inserting at first pos - if (pos == 1) { - insertAtHead(head, data); - return; - } - - // if inserting at last pos - if (temp->next == NULL) { - insertAtTail(tail, data); - return; - } - - while (cnt < pos - 1) { - temp = temp->next; - cnt++; - } - - Node *nodeToInsert = new Node(data); - nodeToInsert->next = temp->next; - temp->next = nodeToInsert; +void insertAtPosition(Node*& head, Node*& tail, int pos, int data) { + Node* temp = head; + int cnt = 1; + + // if inserting at first pos + if (pos == 1) { + insertAtHead(head, data); + return; + } + + // if inserting at last pos + if (temp->next == NULL) { + insertAtTail(tail, data); + return; + } + + while (cnt < pos - 1) { + temp = temp->next; + cnt++; + } + + Node* nodeToInsert = new Node(data); + nodeToInsert->next = temp->next; + temp->next = nodeToInsert; } -void printLinkedList(Node *&head) { - Node *temp = head; - while (temp != NULL) { - cout << temp->data << " "; - temp = temp->next; - } - cout << endl; +void printLinkedList(Node*& head) { + Node* temp = head; + while (temp != NULL) { + cout << temp->data << " "; + temp = temp->next; + } + cout << endl; } int main() { - Node *node1 = new Node(10); + Node* node1 = new Node(10); - // create head and tail pointer. - Node *head = node1; - Node *tail = node1; + // create head and tail pointer. + Node* head = node1; + Node* tail = node1; - cout << "Initial List : "; - printLinkedList(head); + cout << "Initial List : "; + printLinkedList(head); - insertAtHead(head, 12); + insertAtHead(head, 12); - cout << "List after insertion at head : "; - printLinkedList(head); + cout << "List after insertion at head : "; + printLinkedList(head); - insertAtTail(tail, 13); + insertAtTail(tail, 13); - cout << "List after insertion at tail : "; - printLinkedList(head); + cout << "List after insertion at tail : "; + printLinkedList(head); - insertAtPosition(head, tail, 3, 14); + insertAtPosition(head, tail, 3, 14); - cout << "List after insertion at 3rd position : "; - printLinkedList(head); - return 0; -} \ No newline at end of file + cout << "List after insertion at 3rd position : "; + printLinkedList(head); + return 0; +} diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp b/Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp index 2cd0cd8..b323f61 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp +++ b/Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp @@ -1,23 +1,25 @@ -#include -using namespace std; +#include +#include + +using std::cout, std::endl, std::vector; class Node { -public: - // node members - int data; // data - Node *next; // pointer + public: + // node members + int data; // data + Node* next; // pointer - // constructor - Node(int data1) { - data = data1; - next = nullptr; - } + // constructor + Node(int data1) { + data = data1; + next = nullptr; + } }; int main() { - vector arr = {2, 3, 4, 6, 3}; - Node *n1 = new Node(arr[1]); - cout << n1->data << endl; - cout << n1->next << endl; - return 0; + vector arr = {2, 3, 4, 6, 3}; + const Node* n1 = new Node(arr[1]); + cout << n1->data << endl; + cout << n1->next << endl; + return 0; }