-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0da712d
commit 4e5858e
Showing
5 changed files
with
513 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class node { | ||
public: | ||
int data; | ||
node* next; | ||
node* child; | ||
node() { | ||
data = 0; | ||
child = NULL; | ||
next = NULL; | ||
} | ||
node(int val) { | ||
data = val; | ||
next = NULL; | ||
child = NULL; | ||
} | ||
}; | ||
|
||
node* FlatList(node* head) { | ||
if (head == NULL) { | ||
return NULL; | ||
} | ||
node* current = head; | ||
while (current) { | ||
if (current->child) { | ||
|
||
node* nextNode = current->next; | ||
current->next = FlatList(current->child); | ||
current->child = nullptr; | ||
while (current->next) { | ||
current = current->next; | ||
} | ||
current->next = nextNode; | ||
if (nextNode) { | ||
nextNode->child = nullptr; | ||
} | ||
} | ||
current = current->next; | ||
} | ||
|
||
return head; | ||
} | ||
|
||
void Print(node* head) { | ||
node* current = head; | ||
while (current) { | ||
std::cout << current->data << "->"; | ||
current = current->next; | ||
} | ||
cout << "NULL" << std::endl; | ||
} | ||
|
||
int main() { | ||
node* head = new node(1); | ||
head->next = new node(2); | ||
head->next->next = new node(3); | ||
head->next->child = new node(4); | ||
head->next->child->next = new node(5); | ||
head->next->child->next->child = new node(6); | ||
head->next->next->child = new node(7); | ||
head->next->next->child->next = new node(8); | ||
node* List = FlatList(head); | ||
Print(List); | ||
|
||
return 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#include<iostream> | ||
using namespace std; | ||
template<class T> | ||
class DoublyLinked { | ||
public: | ||
class node { | ||
public: | ||
T data; | ||
node* next; | ||
node* prev; | ||
node(T val) { | ||
data = val; | ||
next = NULL; | ||
prev = NULL; | ||
} | ||
}; | ||
DoublyLinked() | ||
{ | ||
head = NULL; | ||
tail = NULL; | ||
} | ||
void InsertAtHead(T const element) { | ||
node* newNode = new node(element); | ||
if (head == NULL) { | ||
head = newNode; | ||
tail = newNode; | ||
} | ||
else { | ||
newNode->next = head; | ||
head->prev = newNode; | ||
head = newNode; | ||
} | ||
} | ||
void InsertAtTail(T const element) { | ||
node* newNode = new node(element); | ||
if (head == NULL) { | ||
head = newNode; | ||
tail = newNode; | ||
} | ||
else { | ||
node* temp = head; | ||
while (temp->next != NULL) { | ||
temp = temp->next; | ||
} | ||
temp->next = newNode; | ||
newNode->prev = temp; | ||
tail = newNode; | ||
} | ||
} | ||
void print() { | ||
node* temp = head; | ||
while (temp != NULL) { | ||
cout << temp->data << "->"; | ||
temp = temp->next; | ||
} | ||
cout << "NULL" << endl; | ||
} | ||
void reversePrint() { | ||
node* temp = head; | ||
while (temp != NULL) { | ||
temp = temp->next; | ||
} | ||
temp=tail; | ||
while (temp != NULL) { | ||
cout << temp->data << "->"; | ||
temp = temp->prev; | ||
} | ||
cout << "NULL" << endl; | ||
} | ||
void DeleteAtHead() { | ||
if (head == NULL) { | ||
return; | ||
} | ||
node* temp = head; | ||
if (head->next != NULL) { | ||
head = temp->next; | ||
head->prev = NULL; | ||
} | ||
else { | ||
head = NULL; | ||
tail = NULL; | ||
} | ||
delete temp; | ||
} | ||
void DeleteAtTail() { | ||
if (head == NULL) { | ||
return; | ||
} | ||
node* temp = head; | ||
while (temp->next->next != NULL) { | ||
temp = temp->next; | ||
} | ||
temp->next= NULL; | ||
tail = temp; | ||
delete temp->next; | ||
} | ||
void deleteAtPosition(int k) { | ||
int SIZE = 0; | ||
node* size = head; | ||
node* temp=NULL, * current = head; | ||
while (size != NULL) { | ||
size = size->next; | ||
SIZE++; | ||
} | ||
if (k == 0 || k > SIZE) { | ||
return; | ||
} | ||
else if (k == 1) { | ||
current = head; | ||
head = head->next; | ||
} | ||
else if (k == size) { | ||
while (current->next->next != NULL) { | ||
current = current->next; | ||
} | ||
current->next = NULL; | ||
} | ||
else { | ||
SIZE = 1; | ||
while (current != NULL && SIZE != k) { | ||
temp = current; | ||
current = current->next; | ||
SIZE++; | ||
} | ||
temp->next = current->next; | ||
current->next->prev = current->prev; | ||
} | ||
delete current; | ||
} | ||
~DoublyLinked() { | ||
while (head != nullptr) { | ||
node* temp = head; | ||
head = head->next; | ||
delete temp; | ||
} | ||
} | ||
private: | ||
node* head; | ||
node* tail; | ||
}; | ||
int main() { | ||
DoublyLinked<int> L1; | ||
int k; | ||
cout << "Inserting At Head : " << endl; | ||
L1.InsertAtHead(5); | ||
L1.InsertAtHead(4); | ||
L1.InsertAtHead(3); | ||
L1.InsertAtHead(2); | ||
L1.InsertAtHead(1); | ||
L1.print(); | ||
cout << "Inserting At Tail : " << endl; | ||
L1.InsertAtTail(6); | ||
L1.InsertAtTail(7); | ||
L1.InsertAtTail(8); | ||
L1.InsertAtTail(9); | ||
L1.print(); | ||
cout << "Reverse Linked List : " << endl; | ||
L1.reversePrint(); | ||
cout << "Delete At Start : "<<endl; | ||
L1.DeleteAtHead(); | ||
L1.print(); | ||
cout << "Delete At Tail : " << endl; | ||
L1.DeleteAtTail(); | ||
L1.print(); | ||
cout << "Delete At Postion : "; | ||
cin >> k; | ||
L1.deleteAtPosition(k); | ||
L1.print(); | ||
return 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include<iostream> | ||
#include<algorithm> | ||
using namespace std; | ||
template <class T> | ||
class SortedSet | ||
{ | ||
public: | ||
class node | ||
{ | ||
public: | ||
T data; | ||
node* next; | ||
node() { | ||
data = 0; | ||
next = NULL; | ||
} | ||
node(T val) { | ||
data = val; | ||
next = NULL; | ||
} | ||
}; | ||
SortedSet() { | ||
head = NULL; | ||
tail = NULL; | ||
} | ||
void insert(T const data) { | ||
node* newNode = new node(data); | ||
if (head == NULL) { | ||
head = newNode; | ||
} | ||
else { | ||
node* temp = head; | ||
while (temp->next != NULL) { | ||
temp = temp->next; | ||
} | ||
temp->next= newNode; | ||
} | ||
} | ||
void Delete(int const index) { | ||
node* temp = head; | ||
node* previous; | ||
int count = 0; | ||
if (index == 0) | ||
{ | ||
node* temp = head; | ||
head = head->next; | ||
delete temp; | ||
return; | ||
} | ||
while (temp != NULL && count < index-1) { | ||
temp = temp->next; | ||
count++; | ||
} | ||
|
||
previous = temp->next; | ||
temp->next = previous->next; | ||
delete previous; | ||
} | ||
void Union(SortedSet<T> const& otherSet) { | ||
node* temp1 = head; | ||
node* temp2 = otherSet.head; | ||
SortedSet<T> result; | ||
|
||
while (temp1 != nullptr && temp2 != nullptr) { | ||
if (temp1->data < temp2->data) { | ||
result.insert(temp1->data); | ||
temp1 = temp1->next; | ||
} | ||
else if (temp1->data > temp2->data) { | ||
result.insert(temp2->data); | ||
temp2 = temp2->next; | ||
} | ||
else { | ||
result.insert(temp1->data); | ||
temp1 = temp1->next; | ||
temp2 = temp2->next; | ||
} | ||
} | ||
while (temp1 != nullptr) { | ||
result.insert(temp1->data); | ||
temp1 = temp1->next; | ||
} | ||
|
||
while (temp2 != nullptr) { | ||
result.insert(temp2->data); | ||
temp2 = temp2->next; | ||
} | ||
tail = result.tail; | ||
head = result.head; | ||
} | ||
void print() const { | ||
node* temp = head; | ||
while (temp != NULL) { | ||
cout << temp->data << " "; | ||
temp = temp->next; | ||
} | ||
cout << endl; | ||
} | ||
private: | ||
node* head; | ||
node* tail; | ||
}; | ||
int main() { | ||
SortedSet<int>a; | ||
cout << "Sorted Set a : " << endl; | ||
a.insert(1); | ||
|
||
a.print(); | ||
|
||
a.print(); | ||
SortedSet<int>b; | ||
cout << "Sorted Set b : " << endl; | ||
b.insert(6); | ||
b.insert(10); | ||
b.insert(11); | ||
b.print(); | ||
cout << "Union : " << endl; | ||
b.Union(a); | ||
b.print(); | ||
return 0; | ||
} |
Oops, something went wrong.