Skip to content

Instantly share code, notes, and snippets.

@hadrizi
Created October 14, 2021 23:42
Show Gist options
  • Save hadrizi/50f75eb8025990539d95b6710d647616 to your computer and use it in GitHub Desktop.
Save hadrizi/50f75eb8025990539d95b6710d647616 to your computer and use it in GitHub Desktop.
memory safe linked list and merge sort
#include <iostream>
#include <string>
class node {
public:
int data;
node *next;
bool empty;
node(int v, bool e=false){
data = v;
empty = e;
if (!e)
next = new node(0, true);
else
next = nullptr;
}
void operator++() {
node* next_ = this->next;
this->data = next_->data;
this->next = next_->next;
this->empty = next_->empty;
}
bool operator!=(node &n) {return ((n.data != data) && (n.next != next)) || (n.empty != empty);}
bool operator==(node &n) {return ((n.data == data) && (n.next == next)) || (n.empty == empty);}
const node& operator*() const { return *this; }
friend std::ostream& operator<<(std::ostream& outs, const node& p);
};
class linked_list {
node *head, *tail;
public:
int length;
linked_list(){
head = tail = nullptr;
length = 0;
}
~linked_list(){
node* cur = head;
while(cur) {
node* next = cur->next;
delete cur;
cur = next;
}
}
void push(int v){
if (!head)
{
head = new node(v);
tail = head;
length++;
return;
}
delete tail->next;
tail->next = new node(v);
tail = tail->next;
length++;
}
void remove(int v){
if (!head->empty){
if (head->data == v){
node* tmp = head;
head = head->next;
delete tmp;
length--;
return;
}
node* prev = head;
node* cur = head->next;
while(!cur->empty) {
if(cur->data == v)
{
node* tmp = cur;
prev->next = cur->next;
if(cur == tail)
tail = prev;
delete tmp;
length--;
break;
}
cur = cur->next;
prev = prev->next;
}
}
}
int pop_min(){
int n = head->data;
for(const node &i: *this){
n = n < i.data ? n : i.data;
}
remove(n);
return n;
}
bool is_empty(){return length == 0;}
static linked_list* append(linked_list& l1, linked_list& l2){
linked_list* l3 = new linked_list();
for(const node &i: l1){
l3->push(i.data);
}
for(const node &i: l2){
l3->push(i.data);
}
return l3;
}
// iterators
const node& begin() const {return *head;}
const node& end() const {return *(tail->next);}
// overrides
friend std::ostream& operator<<(std::ostream& outs, const linked_list& p);
};
std::ostream& operator<<(std::ostream& outs, const linked_list& p) {
std::string s = "";
for(const node &i: p){
s += std::to_string(i.data) + " ";
}
return outs << s;
};
std::ostream& operator<<(std::ostream& outs, const node& p) {
return outs << "value >> " << p.data << std::endl << "is_empty >>" << p.empty << std::endl;
};
linked_list* merge_sort(linked_list* l1, linked_list* l2){
linked_list* l3 = linked_list::append(*l1, *l2);
linked_list* l_sorted = new linked_list();
while (!l3->is_empty())
l_sorted->push(l3->pop_min());
delete l3;
return l_sorted;
}
int main(){
linked_list* l1 = new linked_list();
linked_list* l2 = new linked_list();
int n1, n2;
std::cin >> n1 >> n2;
for (; n1; n1--)
{
int d;
std::cin >> d;
l1->push(d);
}
for (; n2; n2--)
{
int d;
std::cin >> d;
l2->push(d);
}
linked_list* l3 = merge_sort(l1, l2);
std::cout << *l3 << std::endl;
delete l1;
delete l2;
delete l3;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment