Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,35 @@
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"editor.mouseWheelZoom": true,
"files.autoSave": "afterDelay",
"workbench.iconTheme": "vscode-icons",
"liveServer.settings.donotShowInfoMsg": true,
"workbench.colorTheme": "Neon City",
"git.autofetch": true,
"code-runner.executorMap": {
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
"code-runner.runInTerminal": true,
"editor.fontSize": 14,
"editor.lineHeight": 1.5,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.wordWrap": "on",
"editor.formatOnSave": true,
"terminal.integrated.fontSize": 12,
"explorer.confirmDelete": false,
"editor.minimap.enabled": true,
"editor.rulers": [
80
],
"[cpp]": {
"editor.insertSpaces": true,
"editor.tabSize": 4
},
"[python]": {
"editor.formatOnSave": true
}
}
60 changes: 35 additions & 25 deletions 11. Linked List/03SimpleLinkedListImplementationinC++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,67 @@
using namespace std;
#include <iostream>

struct Node {
struct Node
{
int data;
Node* next;
Node *next;
};

Node* createNode(int value) {
Node* newNode = new Node;
Node *createNode(int value)
{
Node *newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
return newNode;
}

void insert(Node*& head, int value) {
Node* newNode = createNode(value);
if (!head) {
void insert(Node *&head, int value)
{
Node *newNode = createNode(value);
if (!head)
{
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
}
else
{
Node *temp = head;
while (temp->next)
{
temp = temp->next;
}
temp->next = newNode;
}
}

void display(Node* head) {
Node* temp = head;
while (temp) {
void display(Node *head)
{
Node *temp = head;
while (temp)
{
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "nullptr" << std::endl;
}

void cleanup(Node* head) {
while (head) {
Node* temp = head;
void cleanup(Node *head)
{
while (head)
{
Node *temp = head;
head = head->next;
delete temp;
}
}

int main() {
Node* head = nullptr;
int main()
{
Node *head = nullptr;
int value;
char choice;

do {
do
{
std::cout << "Enter a value to insert: ";
std::cin >> value;
insert(head, value);
Expand All @@ -65,9 +78,6 @@ int main() {
return 0;
}




/*
Iss function ka naam hai insert aur yeh linked list mein naya node insert karta hai.

Expand All @@ -89,7 +99,7 @@ Linked List structure:

nullptr

then
then

- insert function call hota hai with value 10, linked list ab aise dikhai degi:

Expand Down Expand Up @@ -139,7 +149,7 @@ Linked List structure:
| | | 20 | -> nullptr
+-------+ +-------+

then
then

- display function call hota hai aur output hota hai:

Expand Down Expand Up @@ -180,7 +190,7 @@ Linked List structure:
| | | 20 | -> nullptr
+-------+ +-------+

then
then

- cleanup function call hota hai aur saari nodes ki memory free hoti hai.

Expand Down
134 changes: 133 additions & 1 deletion 11. Linked List/04TraversingaLinkedListinC++.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,139 @@
#include <bits/stdc++.h>
using namespace std;

#include <iostream>

struct Node {
int data;
Node *next;
};

Node *createNode(int value) {
Node *newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
return newNode;
}

void insert(Node *&head, int value) {
Node *newNode = createNode(value);
if (!head) {
head = newNode;
} else {
Node *temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}

void traverse(Node *head) {
Node *temp = head;
std::cout << "Linked List: ";
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "nullptr" << std::endl;
}

void cleanup(Node *head) {
while (head) {
Node *temp = head;
head = head->next;
delete temp;
}
}

int main() {
// Your code here
Node *head = nullptr;
int value;
char choice;

do {
std::cout << "Enter a value to insert: ";
std::cin >> value;
insert(head, value);

std::cout << "Do you want to insert another value? (y/n): ";
std::cin >> choice;
} while (choice == 'y' || choice == 'Y');

traverse(head);
cleanup(head);

return 0;
}


/*
Iss function ka naam hai createNode, aur yeh ek naya node create karta hai jismein ek integer value hoti hai.

- Function ka logic:
1. Yeh function ek naya node banata hai aur usmein data ko assign karta hai.
2. Node ke next pointer ko nullptr se initialize karta hai.

Iss function ka naam hai insert, aur yeh linked list mein ek naya node insert karta hai.

- Function ka logic:
1. Agar linked list khaali hai (head = nullptr), toh naya node head ban jaata hai.
2. Agar linked list khaali nahi hai, toh woh last node tak jata hai aur naya node wahan add karta hai.

Iss function ka naam hai traverse, aur yeh linked list ke saare nodes ko traverse karke unka data print karta hai.

- Function ka logic:
1. Yeh function linked list ke head se shuru hota hai aur har node ka data print karta hai.
2. Jab tak current node null nahi hota, woh next node par move karta hai.

Iss function ka naam hai cleanup, aur yeh linked list ke saare nodes ki memory ko free karta hai.

- Function ka logic:
1. Yeh function har node ko deallocate karta hai jab tak linked list khaali nahi ho jaata.

Process:
- Pehle, user values input karta hai aur woh values linked list mein insert hoti hain.
- Uske baad, linked list ko traverse kiya jaata hai aur saare elements print hote hain.

Example:
- Agar user ne values 10, 20, aur 30 enter kiye hain:

Linked List structure:
10 -> 20 -> 30 -> nullptr

then

- Insert function call hota hai jab user value enter karta hai. Pehle 10 insert hota hai, phir 20, aur finally 30.

Three Iterations:
- Pehli Iteration: User 10 enter karta hai.

Linked List: 10 -> nullptr

- Doosri Iteration: User 20 enter karta hai.

Linked List: 10 -> 20 -> nullptr

- Teesri Iteration: User 30 enter karta hai.

Linked List: 10 -> 20 -> 30 -> nullptr

Dry Run:
1. Initial setup: head = nullptr.
2. User enters 10:
- createNode(10) se naya node banta hai.
- insert function mein head ab 10 ho jaata hai.
3. User enters 20:
- createNode(20) se naya node banta hai.
- insert function mein last node (10) ke next mein 20 set hota hai.
4. User enters 30:
- createNode(30) se naya node banta hai.
- insert function mein last node (20) ke next mein 30 set hota hai.
5. Traverse function call hota hai:
- Linked List print hota hai: "Linked List: 10 -> 20 -> 30 -> nullptr".
6. Cleanup function call hota hai:
- Saare nodes ki memory free hoti hai.

Output:
Linked List: 10 -> 20 -> 30 -> nullptr
*/
Binary file added 11. Linked List/04TraversingaLinkedListinC++.exe
Binary file not shown.
Loading