Skip to content

Commit

Permalink
fixes: did the necessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh672003 committed Dec 3, 2023
1 parent 7bceaaa commit ff751e5
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Linked Lists.

<!--toc:start-->

**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)
<!--toc:end-->

---

### 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.
Expand Down Expand Up @@ -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.

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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<int> 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;
}
```

Expand Down
20 changes: 9 additions & 11 deletions Data-Structures/Linear-Data-Structures/Linked-List/03-traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
```
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
### Imlementation :

```cpp
#include <bits/stdc++.h>
using namespace std;
#include <iostream>

using std::cout, std::cin, std::endl;

template <typename T> class LinkedListNode {
public:
Expand Down
134 changes: 67 additions & 67 deletions Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
#include <bits/stdc++.h>
#include <cstddef>
using namespace std;
#include <iostream>

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;
}
cout << "List after insertion at 3rd position : ";
printLinkedList(head);
return 0;
}
34 changes: 18 additions & 16 deletions Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#include <bits/stdc++.h>
using namespace std;
#include <iostream>
#include <vector>

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<int> arr = {2, 3, 4, 6, 3};
Node *n1 = new Node(arr[1]);
cout << n1->data << endl;
cout << n1->next << endl;
return 0;
vector<int> arr = {2, 3, 4, 6, 3};
const Node* n1 = new Node(arr[1]);
cout << n1->data << endl;
cout << n1->next << endl;
return 0;
}

0 comments on commit ff751e5

Please sign in to comment.