-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bceaaa
commit ff751e5
Showing
7 changed files
with
120 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 67 additions & 67 deletions
134
Data-Structures/Linear-Data-Structures/Linked-List/temp.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
Data-Structures/Linear-Data-Structures/Linked-List/temp2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |