diff --git a/.vscode/settings.json b/.vscode/settings.json index c4a4516..f477738 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 + } } \ No newline at end of file diff --git a/11. Linked List/03SimpleLinkedListImplementationinC++.cpp b/11. Linked List/03SimpleLinkedListImplementationinC++.cpp index 282cf7a..03b8966 100644 --- a/11. Linked List/03SimpleLinkedListImplementationinC++.cpp +++ b/11. Linked List/03SimpleLinkedListImplementationinC++.cpp @@ -2,54 +2,67 @@ using namespace std; #include -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); @@ -65,9 +78,6 @@ int main() { return 0; } - - - /* Iss function ka naam hai insert aur yeh linked list mein naya node insert karta hai. @@ -89,7 +99,7 @@ Linked List structure: nullptr -then +then - insert function call hota hai with value 10, linked list ab aise dikhai degi: @@ -139,7 +149,7 @@ Linked List structure: | | | 20 | -> nullptr +-------+ +-------+ -then +then - display function call hota hai aur output hota hai: @@ -180,7 +190,7 @@ Linked List structure: | | | 20 | -> nullptr +-------+ +-------+ -then +then - cleanup function call hota hai aur saari nodes ki memory free hoti hai. diff --git a/11. Linked List/04TraversingaLinkedListinC++.cpp b/11. Linked List/04TraversingaLinkedListinC++.cpp index e76a9ac..48dfb43 100644 --- a/11. Linked List/04TraversingaLinkedListinC++.cpp +++ b/11. Linked List/04TraversingaLinkedListinC++.cpp @@ -1,7 +1,139 @@ #include using namespace std; +#include + +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 +*/ diff --git a/11. Linked List/04TraversingaLinkedListinC++.exe b/11. Linked List/04TraversingaLinkedListinC++.exe new file mode 100644 index 0000000..bdf40a9 Binary files /dev/null and b/11. Linked List/04TraversingaLinkedListinC++.exe differ diff --git a/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.cpp b/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.cpp index e76a9ac..50ce239 100644 --- a/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.cpp +++ b/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.cpp @@ -1,7 +1,134 @@ #include using namespace std; +#include +using namespace std; + +struct Node { + int data; + Node* next; +}; + +void insert(Node** head, int data) { + Node* newNode = new Node(); + newNode->data = data; + newNode->next = nullptr; + + if (*head == nullptr) { + *head = newNode; + } else { + Node* temp = *head; + while (temp->next != nullptr) { + temp = temp->next; + } + temp->next = newNode; + } +} + +void reverseTraversal(Node* head) { + if (head == nullptr) { + return; + } + reverseTraversal(head->next); + cout << head->data << " "; +} + int main() { - // Your code here + Node* head = nullptr; + int n, value; + + cout << "Enter the number of nodes: "; + cin >> n; + + for (int i = 0; i < n; ++i) { + cout << "Enter value for node " << i + 1 << ": "; + cin >> value; + insert(&head, value); + } + + cout << "Reverse Traversal of the list: "; + reverseTraversal(head); + cout << endl; + return 0; } + + + + + + + +/* +Iss function ka naam hai reverseTraversal aur yeh function singly linked list ko reverse order mein traverse karta hai aur nodes ke data ko print karta hai. + +- Function ka logic: + 1. reverseTraversal(Node* head) ek recursive function hai jo singly linked list ke nodes ko reverse order mein traverse karta hai. + 2. Jab head nullptr hota hai (yaani list ka end aa gaya), function wapas return kar jata hai bina kuch print kiye. + 3. Jaise hi base case se return hota hai, har node ka data wapas recursion ke stack se pop hone par print hota hai. + +Process: +- Pehle, function last node tak traverse karta hai. +- Uske baad, jaise jaise function wapas recursive stack se pop hota hai, data reverse order mein print hota hai. + +Example: +- Agar linked list hai: + + 10 -> 20 -> 30 -> 40 -> nullptr + +then + +reverseTraversal call pehle 40 par aayega, phir wapas 30, phir 20, aur aakhir mein 10 print karega. + +- Explanation of example with function calls and outputs: + reverseTraversal(10) + -> reverseTraversal(20) + -> reverseTraversal(30) + -> reverseTraversal(40) + -> reverseTraversal(nullptr) + <- print 40 + <- print 30 + <- print 20 + <- print 10 + +Three Iterations: +1. First recursive call: + - Function last node tak pahuchta hai: + + 10 -> 20 -> 30 -> 40 -> nullptr + + - Stack: + reverseTraversal(10) + reverseTraversal(20) + reverseTraversal(30) + reverseTraversal(40) + +2. Pop from recursion: + - reverseTraversal stack se wapas aata hai aur reverse mein nodes ke data ko print karta hai. + + Pehle 40 print hoga, fir 30, fir 20, fir 10. + + Output so far: 40 30 20 10 + +3. Final output: + - Sabhi nodes print hone ke baad recursion end hota hai, aur final output: + + 40 30 20 10 + +Dry Run: +1. Input: + Enter the number of nodes: 4 + Enter value for node 1: 10 + Enter value for node 2: 20 + Enter value for node 3: 30 + Enter value for node 4: 40 + +2. Step-by-step execution: + - Node values insert hote hain: + 10 -> 20 -> 30 -> 40 -> nullptr + - reverseTraversal(10) call hota hai aur recursively last node (40) tak jaata hai. + - Stack se pop hone par values reverse mein print hoti hain: 40 30 20 10 + +3. Final output: + Reverse Traversal of the list: 40 30 20 10 +*/ diff --git a/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.exe b/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.exe new file mode 100644 index 0000000..bc27b62 Binary files /dev/null and b/11. Linked List/05RecrusiveTraversalofSinglyLinkedList.exe differ diff --git a/11. Linked List/06InsertatBeginofSinglyLinkedList.cpp b/11. Linked List/06InsertatBeginofSinglyLinkedList.cpp index e76a9ac..87ea50a 100644 --- a/11. Linked List/06InsertatBeginofSinglyLinkedList.cpp +++ b/11. Linked List/06InsertatBeginofSinglyLinkedList.cpp @@ -1,7 +1,150 @@ -#include -using namespace std; +#include + +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 insertAtBeginning(Node *&head, int value) +{ + Node *newNode = createNode(value); + newNode->next = head; + head = newNode; +} + +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; + head = head->next; + delete temp; + } +} + +int main() +{ + Node *head = nullptr; + int value; + char choice; + + do + { + std::cout << "Enter a value to insert at the end: "; + std::cin >> value; + insert(head, value); + + std::cout << "Do you want to insert another value at the end? (y/n): "; + std::cin >> choice; + } while (choice == 'y' || choice == 'Y'); + + std::cout << "Do you want to insert a value at the beginning? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') + { + std::cout << "Enter a value to insert at the beginning: "; + std::cin >> value; + insertAtBeginning(head, value); + } + + std::cout << "Linked List: "; + display(head); + + cleanup(head); -int main() { - // Your code here return 0; } + + + + +/* +Iss function ka naam hai createNode aur yeh ek naya node create karta hai jo linked list mein use hoga. + +- Function ka logic: + 1. Yeh function ek integer value leta hai aur ek naya Node object create karta hai. + 2. Naye node ka data member is value se initialize hota hai aur uska next pointer nullptr par set hota hai. + +Iss function ka naam hai insert aur yeh linked list mein naya node add karta hai. + +- Function ka logic: + 1. Yeh function ek reference head pointer aur ek integer value leta hai. + 2. Pehle yeh ek naya node create karta hai using createNode function. + 3. Agar head pointer nullptr hai, to naya node head pointer ban jaata hai. + 4. Agar list khali nahi hai, to yeh last node tak traverse karta hai aur naye node ko last node ke next pointer par set karta hai. + +Iss function ka naam hai insertAtBeginning aur yeh linked list ke beginning mein naya node add karta hai. + +- Function ka logic: + 1. Yeh function ek reference head pointer aur ek integer value leta hai. + 2. Pehle yeh ek naya node create karta hai. + 3. Naya node ka next pointer current head pointer par set hota hai. + 4. Head pointer ko naye node par point karne ke liye update karta hai. + +Iss function ka naam hai display aur yeh linked list ke nodes ke data ko print karta hai. + +- Function ka logic: + 1. Yeh function head pointer leta hai aur linked list ko traverse karta hai. + 2. Har node ka data print karta hai jab tak wo nullptr nahi ho jaata. + +Iss function ka naam hai cleanup aur yeh linked list ke saare nodes ko delete karta hai. + +- Function ka logic: + 1. Yeh function head pointer ko leta hai aur list ke har node ko delete karta hai jab tak head pointer nullptr nahi ho jaata. + +Dry Run: +1. Initial setup: Linked list khali hai, head pointer nullptr hai. +2. User input lete hain: Pehle user value enter karta hai (e.g., 10). + - insert function call hota hai, naya node create hota hai aur head pointer ab naya node ban jaata hai. + - List ab 10 -> nullptr hai. + +3. User phir se input le raha hai: dusri value enter karta hai (e.g., 20). + - Dusra insert function call hota hai, naya node create hota hai, last node ke next pointer par set hota hai. + - List ab 10 -> 20 -> nullptr hai. + +4. User ek value beginning mein insert karna chahta hai: (e.g., 5). + - insertAtBeginning function call hota hai, naya node create hota hai aur head pointer update hota hai. + - List ab 5 -> 10 -> 20 -> nullptr hai. + +Output: +Linked List: 5 -> 10 -> 20 -> nullptr +*/ diff --git a/11. Linked List/06InsertatBeginofSinglyLinkedList.exe b/11. Linked List/06InsertatBeginofSinglyLinkedList.exe new file mode 100644 index 0000000..727ff77 Binary files /dev/null and b/11. Linked List/06InsertatBeginofSinglyLinkedList.exe differ diff --git a/11. Linked List/07InsertattheendofSinglyLinkedList.cpp b/11. Linked List/07InsertattheendofSinglyLinkedList.cpp index e76a9ac..767853f 100644 --- a/11. Linked List/07InsertattheendofSinglyLinkedList.cpp +++ b/11. Linked List/07InsertattheendofSinglyLinkedList.cpp @@ -1,7 +1,128 @@ -#include -using namespace std; +#include + +struct Node { + int data; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->next = nullptr; + return newNode; +} + +void insertAtEnd(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 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; + head = head->next; + delete temp; + } +} int main() { - // Your code here + Node* head = nullptr; + head = createNode(10); + head->next = createNode(20); + head->next->next = createNode(30); + head->next->next->next = createNode(40); + + std::cout << "Initial Linked List: "; + display(head); + + char choice; + std::cout << "Do you want to insert a value at the end? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') { + int value; + std::cout << "Enter a value to insert at the end: "; + std::cin >> value; + insertAtEnd(head, value); + } + + std::cout << "Updated Linked List: "; + display(head); + + cleanup(head); return 0; } + +/* +Iss function ka naam hai createNode aur yeh ek naya node create karta hai jismein specified value hoti hai. + +- Function ka logic: + 1. Yeh function ek naya Node banata hai aur usmein value set karta hai. + 2. Yeh function naya node ka next pointer null set karta hai. + +Iss function ka naam hai insertAtEnd aur yeh linked list ke end mein naya node insert karta hai. + +- Function ka logic: + 1. Yeh function naya node banata hai. + 2. Agar head null hai, toh yeh naya node ko head set karta hai. + 3. Agar head null nahi hai, toh yeh list ko traverse karke last node tak pahunchta hai aur naya node ko wahan insert karta hai. + +Process: +- Pehle, linked list ko manually create kiya gaya hai jismein nodes 10, 20, 30 aur 40 hain. +- Uske baad, linked list ko display kiya gaya hai. + +Example: +- Agar linked list hai: + +10 -> 20 -> 30 -> 40 -> nullptr + +then + +- User se poocha jayega ki kya wo end mein ek naya value insert karna chahta hai. + +Three Iterations: +- Iteration 1: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: y + - User inserts value 50. + - Updated linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + +- Iteration 2: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + - User input: y + - User inserts value 60. + - Updated linked list: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> nullptr + +- Iteration 3: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> nullptr + - User input: n + - Updated linked list remains: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> nullptr + +Dry Run: +1. Initial setup and input: Manually created linked list with nodes 10, 20, 30, and 40. +2. User inputs 'y' to insert a value, then inputs 50. +3. Final output after insertion: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr. + +Output: +Initial Linked List: 10 -> 20 -> 30 -> 40 -> nullptr +Do you want to insert a value at the end? (y/n): y +Enter a value to insert at the end: 50 +Updated Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr +*/ diff --git a/11. Linked List/07InsertattheendofSinglyLinkedList.exe b/11. Linked List/07InsertattheendofSinglyLinkedList.exe new file mode 100644 index 0000000..6b5fd90 Binary files /dev/null and b/11. Linked List/07InsertattheendofSinglyLinkedList.exe differ diff --git a/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.cpp b/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.cpp index e76a9ac..d3612c0 100644 --- a/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.cpp +++ b/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.cpp @@ -1,7 +1,142 @@ -#include -using namespace std; +#include + +struct Node { + int data; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->next = nullptr; + return newNode; +} + +void insertAtEnd(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 deleteFirstNode(Node*& head) { + if (head) { + Node* temp = head; + head = head->next; + delete 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; + head = head->next; + delete temp; + } +} int main() { - // Your code here + Node* head = nullptr; + head = createNode(10); + head->next = createNode(20); + head->next->next = createNode(30); + head->next->next->next = createNode(40); + + std::cout << "Initial Linked List: "; + display(head); + + char choice; + std::cout << "Do you want to delete the first node? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') { + deleteFirstNode(head); + } + + std::cout << "Linked List after deletion: "; + display(head); + + choice = 'n'; // Reset choice for further operations + std::cout << "Do you want to insert a value at the end? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') { + int value; + std::cout << "Enter a value to insert at the end: "; + std::cin >> value; + insertAtEnd(head, value); + } + + std::cout << "Final Linked List: "; + display(head); + + cleanup(head); return 0; } + +/* +Iss function ka naam hai deleteFirstNode aur yeh linked list ke pehle node ko delete karta hai. + +- Function ka logic: + 1. Agar head null nahi hai, toh yeh pehle node ko temporary variable mein store karta hai. + 2. Head ko next node par set karta hai, phir temporary variable ko delete karta hai. + +Process: +- Pehle, linked list ko manually create kiya gaya hai jismein nodes 10, 20, 30 aur 40 hain. +- Uske baad, user se poocha jayega ki kya wo pehla node delete karna chahta hai. + +Example: +- Agar linked list hai: + +10 -> 20 -> 30 -> 40 -> nullptr + +then + +- User se poocha jayega ki kya wo pehla node delete karna chahta hai. + +Three Iterations: +- Iteration 1: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: y + - Pahla node (10) delete hota hai. + - Updated linked list: 20 -> 30 -> 40 -> nullptr + +- Iteration 2: + - Initial linked list: 20 -> 30 -> 40 -> nullptr + - User input: y + - Pahla node (20) delete hota hai. + - Updated linked list: 30 -> 40 -> nullptr + +- Iteration 3: + - Initial linked list: 30 -> 40 -> nullptr + - User input: n + - Updated linked list remains: 30 -> 40 -> nullptr + +Dry Run: +1. Initial setup and input: Manually created linked list with nodes 10, 20, 30, and 40. +2. User inputs 'y' to delete the first node, which is 10. +3. Final output after deletion: 20 -> 30 -> 40 -> nullptr. + +Output: +Initial Linked List: 10 -> 20 -> 30 -> 40 -> nullptr +Do you want to delete the first node? (y/n): y +Linked List after deletion: 20 -> 30 -> 40 -> nullptr +Do you want to insert a value at the end? (y/n): y +Enter a value to insert at the end: 50 +Final Linked List: 20 -> 30 -> 40 -> 50 -> nullptr +*/ diff --git a/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.exe b/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.exe new file mode 100644 index 0000000..4c9c453 Binary files /dev/null and b/11. Linked List/08DeleteFirstNodeofSinglyLinkedList.exe differ diff --git a/11. Linked List/09DeleteLastofSinglyLinkedList.cpp b/11. Linked List/09DeleteLastofSinglyLinkedList.cpp index e76a9ac..c9007a4 100644 --- a/11. Linked List/09DeleteLastofSinglyLinkedList.cpp +++ b/11. Linked List/09DeleteLastofSinglyLinkedList.cpp @@ -1,7 +1,151 @@ -#include -using namespace std; +#include + +struct Node { + int data; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->next = nullptr; + return newNode; +} + +void insertAtEnd(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 deleteLastNode(Node*& head) { + if (!head) return; // If the list is empty, do nothing + if (!head->next) { // If there's only one node + delete head; + head = nullptr; + return; + } + Node* temp = head; + while (temp->next && temp->next->next) { + temp = temp->next; + } + delete temp->next; // Delete the last node + temp->next = nullptr; // Set the new last node's next to nullptr +} + +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; + head = head->next; + delete temp; + } +} int main() { - // Your code here + Node* head = nullptr; + head = createNode(10); + head->next = createNode(20); + head->next->next = createNode(30); + head->next->next->next = createNode(40); + + std::cout << "Initial Linked List: "; + display(head); + + char choice; + std::cout << "Do you want to delete the last node? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') { + deleteLastNode(head); + } + + std::cout << "Linked List after deletion: "; + display(head); + + choice = 'n'; // Reset choice for further operations + std::cout << "Do you want to insert a value at the end? (y/n): "; + std::cin >> choice; + + if (choice == 'y' || choice == 'Y') { + int value; + std::cout << "Enter a value to insert at the end: "; + std::cin >> value; + insertAtEnd(head, value); + } + + std::cout << "Final Linked List: "; + display(head); + + cleanup(head); return 0; } + +/* +Iss function ka naam hai deleteLastNode aur yeh linked list ke aakhri node ko delete karta hai. + +- Function ka logic: + 1. Agar head null hai, toh kuch nahi kiya jata. + 2. Agar sirf ek node hai, toh usko delete karte hain aur head ko null set karte hain. + 3. Agar do ya usse zyada nodes hain, toh list ke aakhri node tak pahunchne ke liye loop chalayenge. + 4. Aakhri node ko delete karne ke baad, second last node ka next pointer null set karte hain. + +Process: +- Pehle, linked list ko manually create kiya gaya hai jismein nodes 10, 20, 30 aur 40 hain. +- Uske baad, user se poocha jayega ki kya wo aakhri node delete karna chahta hai. + +Example: +- Agar linked list hai: + +10 -> 20 -> 30 -> 40 -> nullptr + +then + +- User se poocha jayega ki kya wo aakhri node delete karna chahta hai. + +Three Iterations: +- Iteration 1: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: y + - Aakhri node (40) delete hota hai. + - Updated linked list: 10 -> 20 -> 30 -> nullptr + +- Iteration 2: + - Initial linked list: 10 -> 20 -> 30 -> nullptr + - User input: y + - Aakhri node (30) delete hota hai. + - Updated linked list: 10 -> 20 -> nullptr + +- Iteration 3: + - Initial linked list: 10 -> 20 -> nullptr + - User input: n + - Updated linked list remains: 10 -> 20 -> nullptr + +Dry Run: +1. Initial setup and input: Manually created linked list with nodes 10, 20, 30, and 40. +2. User inputs 'y' to delete the last node, which is 40. +3. Final output after deletion: 10 -> 20 -> 30 -> nullptr. + +Output: +Initial Linked List: 10 -> 20 -> 30 -> 40 -> nullptr +Do you want to delete the last node? (y/n): y +Linked List after deletion: 10 -> 20 -> 30 -> nullptr +Do you want to insert a value at the end? (y/n): y +Enter a value to insert at the end: 50 +Final Linked List: 10 -> 20 -> 50 -> nullptr +*/ diff --git a/11. Linked List/09DeleteLastofSinglyLinkedList.exe b/11. Linked List/09DeleteLastofSinglyLinkedList.exe new file mode 100644 index 0000000..f94e59a Binary files /dev/null and b/11. Linked List/09DeleteLastofSinglyLinkedList.exe differ diff --git a/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.cpp b/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.cpp index e76a9ac..be6d382 100644 --- a/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.cpp +++ b/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.cpp @@ -1,7 +1,147 @@ -#include -using namespace std; +#include + +struct Node { + int data; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->next = nullptr; + return newNode; +} + +void insertAtPosition(Node*& head, int value, int position) { + Node* newNode = createNode(value); + if (position == 0) { // Insert at the beginning + newNode->next = head; + head = newNode; + return; + } + + Node* temp = head; + for (int i = 0; i < position - 1 && temp != nullptr; i++) { + temp = temp->next; + } + + if (temp == nullptr) { // If position is greater than the length of the list + std::cout << "Position out of bounds. Inserting at the end instead." << std::endl; + delete newNode; // Clean up the new node since we won't use it + void insertAtEnd(Node*&head, int value); + return; + } + + newNode->next = temp->next; // Link new node to the next node + temp->next = newNode; // Link the previous node to the new node +} + +void insertAtEnd(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 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; + head = head->next; + delete temp; + } +} int main() { - // Your code here + Node* head = nullptr; + // Manually creating the linked list + insertAtEnd(head, 10); + insertAtEnd(head, 20); + insertAtEnd(head, 30); + insertAtEnd(head, 40); + + std::cout << "Initial Linked List: "; + display(head); + + int value, position; + std::cout << "Enter value to insert: "; + std::cin >> value; + std::cout << "Enter position to insert (0 for beginning, 1 for second, etc.): "; + std::cin >> position; + + insertAtPosition(head, value, position); + + std::cout << "Updated Linked List: "; + display(head); + + cleanup(head); return 0; } + +/* +Iss function ka naam hai insertAtPosition aur yeh linked list mein kisi bhi desired position par node insert karta hai. + +- Function ka logic: + 1. Naya node create kiya jata hai. + 2. Agar position 0 hai, toh naya node ko head bana diya jata hai. + 3. Agar position 0 nahi hai, toh hum linked list traverse karte hain takki hum us position par pahunch sakein. + 4. Agar position valid hai, toh naya node ko us position par insert kiya jata hai. + 5. Agar position out of bounds hai, toh node ko end mein insert kiya jata hai. + +Process: +- Pehle, linked list ko manually create kiya gaya hai jismein nodes 10, 20, 30 aur 40 hain. +- Uske baad, user se naya value aur desired position input liya jata hai. + +Example: +- Agar linked list hai: + +10 -> 20 -> 30 -> 40 -> nullptr + +then + +- User enters value = 25 aur position = 2, toh naya linked list hoga: + +10 -> 20 -> 25 -> 30 -> 40 -> nullptr + +Three Iterations: +- Iteration 1: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: value = 25, position = 2 + - Updated linked list: 10 -> 20 -> 25 -> 30 -> 40 -> nullptr + +- Iteration 2: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: value = 5, position = 0 + - Updated linked list: 5 -> 10 -> 20 -> 30 -> 40 -> nullptr + +- Iteration 3: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> nullptr + - User input: value = 50, position = 5 (out of bounds) + - Updated linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + +Dry Run: +1. Initial setup and input: Manually created linked list with nodes 10, 20, 30, and 40. +2. User inputs value = 25 and position = 2. +3. New node with value 25 is inserted at position 2. +4. Final output after insertion: 10 -> 20 -> 25 -> 30 -> 40 -> nullptr. + +Output: +Initial Linked List: 10 -> 20 -> 30 -> 40 -> nullptr +Enter value to insert: 25 +Enter position to insert (0 for beginning, 1 for second, etc.): 2 +Updated Linked List: 10 -> 20 -> 25 -> 30 -> 40 -> nullptr +*/ diff --git a/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.exe b/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.exe new file mode 100644 index 0000000..3cb84ab Binary files /dev/null and b/11. Linked List/10InsertatgivenpositioninSinglyLinkedList.exe differ diff --git a/11. Linked List/11SearchinaLinkedList(IterativeandRecursive).cpp b/11. Linked List/11SearchinaLinkedList(IterativeandRecursive).cpp deleted file mode 100644 index e76a9ac..0000000 --- a/11. Linked List/11SearchinaLinkedList(IterativeandRecursive).cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -using namespace std; - -int main() { - // Your code here - return 0; -} diff --git a/11. Linked List/11SearchinaLinkedList.cpp b/11. Linked List/11SearchinaLinkedList.cpp new file mode 100644 index 0000000..ff8a3ca --- /dev/null +++ b/11. Linked List/11SearchinaLinkedList.cpp @@ -0,0 +1,138 @@ +#include + +struct Node { + int data; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->next = nullptr; + return newNode; +} + +void insertAtEnd(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; + } +} + +int searchNode(Node* head, int value) { + Node* temp = head; + int position = 0; + + while (temp) { + if (temp->data == value) { + return position; // Return the position if the node is found + } + temp = temp->next; + position++; + } + + return -1; // Return -1 if the node is not found +} + +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; + head = head->next; + delete temp; + } +} + +int main() { + Node* head = nullptr; + + // Manually creating the linked list + insertAtEnd(head, 10); + insertAtEnd(head, 20); + insertAtEnd(head, 30); + insertAtEnd(head, 40); + insertAtEnd(head, 50); + + std::cout << "Linked List: "; + display(head); + + int value; + std::cout << "Enter value to search: "; + std::cin >> value; + + int position = searchNode(head, value); + + if (position != -1) { + std::cout << "Value " << value << " found at position: " << position << std::endl; + } else { + std::cout << "Value " << value << " not found in the linked list." << std::endl; + } + + cleanup(head); + return 0; +} + +/* +Iss function ka naam hai searchNode aur yeh linked list mein kisi node ko search karta hai aur uska position return karta hai. + +- Function ka logic: + 1. Linked list ke head se start karke har node ka data check kiya jata hai. + 2. Agar data milta hai, toh position return kiya jata hai. + 3. Agar linked list ke end tak search karne par data nahi milta, toh -1 return kiya jata hai. + +Process: +- Pehle, linked list ko manually create kiya gaya hai jismein nodes 10, 20, 30, 40, aur 50 hain. +- Uske baad, user se search karne ke liye value input liya jata hai. + +Example: +- Agar linked list hai: + +10 -> 20 -> 30 -> 40 -> 50 -> nullptr + +then + +- User enters value = 30, toh output hoga: + +Value 30 found at position: 2 + +Three Iterations: +- Iteration 1: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + - User input: 30 + - Value 30 found at position: 2 + +- Iteration 2: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + - User input: 15 + - Value 15 not found in the linked list. + +- Iteration 3: + - Initial linked list: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr + - User input: 50 + - Value 50 found at position: 4 + +Dry Run: +1. Initial setup and input: Manually created linked list with nodes 10, 20, 30, 40, and 50. +2. User inputs value = 30. +3. Function searches through the linked list, finds value 30 at position 2. +4. Final output: Value 30 found at position: 2. + +Output: +Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> nullptr +Enter value to search: 30 +Value 30 found at position: 2 +*/ diff --git a/11. Linked List/11SearchinaLinkedList.exe b/11. Linked List/11SearchinaLinkedList.exe new file mode 100644 index 0000000..86db6b7 Binary files /dev/null and b/11. Linked List/11SearchinaLinkedList.exe differ diff --git a/11. Linked List/14InsertatBeginofDoublyLinkedList.cpp b/11. Linked List/14InsertatBeginofDoublyLinkedList.cpp index e76a9ac..45fec9b 100644 --- a/11. Linked List/14InsertatBeginofDoublyLinkedList.cpp +++ b/11. Linked List/14InsertatBeginofDoublyLinkedList.cpp @@ -1,7 +1,95 @@ -#include -using namespace std; +#include + +struct Node { + int data; + Node* prev; + Node* next; +}; + +Node* createNode(int value) { + Node* newNode = new Node; + newNode->data = value; + newNode->prev = nullptr; + newNode->next = nullptr; + return newNode; +} + +void insertAtBeginning(Node*& head, int value) { + Node* newNode = createNode(value); + if (head == nullptr) { + head = newNode; + } else { + newNode->next = head; + head->prev = newNode; + head = newNode; + } +} + +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; + head = head->next; + delete temp; + } +} int main() { - // Your code here + Node* head = nullptr; + insertAtBeginning(head, 10); + insertAtBeginning(head, 20); + insertAtBeginning(head, 30); + + std::cout << "Doubly Linked List after inserting nodes at the beginning: "; + display(head); + + cleanup(head); return 0; } + +/* +Iss function ka naam hai insertAtBeginning aur yeh doubly linked list ke starting mein node ko insert karta hai. + +- Function ka logic: + 1. Pehle ek naya node create hota hai jisme user ka input value hota hai. + 2. Agar linked list khali hai (head == nullptr), toh yeh node head ban jata hai. + 3. Agar linked list khali nahi hai, toh naya node head ke pehle attach ho jata hai aur uske next pointer ko purane head par point kar diya jata hai. + 4. Head ke prev pointer ko nayi node pe point kar diya jata hai, aur naya node list ka head ban jata hai. + +Process: +- Pehle, hum manually nodes ko beginning mein insert kar rahe hain. +- Phir, doubly linked list ko display kar rahe hain. + +Example: +- Agar doubly linked list initially empty hai aur hum nodes 30, 20, aur 10 ko insert karte hain: + +30 <-> nullptr (First insert) +20 <-> 30 <-> nullptr (Second insert) +10 <-> 20 <-> 30 <-> nullptr (Third insert) + +Three Iterations: +- Iteration 1: + - Node 30 insert hota hai, list: 30 <-> nullptr +- Iteration 2: + - Node 20 insert hota hai, list: 20 <-> 30 <-> nullptr +- Iteration 3: + - Node 10 insert hota hai, list: 10 <-> 20 <-> 30 <-> nullptr + +Dry Run: +1. Initial setup: Doubly linked list is empty. +2. Insert node 30 -> Doubly linked list becomes 30 <-> nullptr. +3. Insert node 20 -> Doubly linked list becomes 20 <-> 30 <-> nullptr. +4. Insert node 10 -> Doubly linked list becomes 10 <-> 20 <-> 30 <-> nullptr. + +Output: +Doubly Linked List after inserting nodes at the beginning: +10 <-> 20 <-> 30 <-> nullptr +*/ diff --git a/11. Linked List/14InsertatBeginofDoublyLinkedList.exe b/11. Linked List/14InsertatBeginofDoublyLinkedList.exe new file mode 100644 index 0000000..555ce3d Binary files /dev/null and b/11. Linked List/14InsertatBeginofDoublyLinkedList.exe differ