Skip to content

Instantly share code, notes, and snippets.

@reddragon
Created Dec 8, 2016
Embed
What would you like to do?
A util file for misc algo problems
#pragma once
#include <iostream>
#include <cassert>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstdio>
#include <limits>
#include <bitset>
#include <string>
#include <iterator>
#include <cmath>
#include <queue>
#include <numeric>
using namespace std;
#define EXPECT_EQ(X, Y) \
{ \
if (X != Y) { \
cerr << "Expected " << X << ", but received " << Y << endl; \
} \
assert(X == Y); \
}
string toBinaryStr(uint64_t n) {
string r;
do {
r.push_back('0' + n % 2);
n /= 2;
} while (n != 0);
reverse(r.begin(), r.end());
return r;
}
// Linked List Related Methods
template<typename T>
struct LNode {
T data;
LNode<T> *next;
};
template <typename T>
LNode<T> *createLNode(T data) {
LNode<T> *newNode = new LNode<T>();
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
template <typename T>
LNode<T> *appendTo(LNode<T> *n, LNode<T> *next) {
n->next = next;
return next;
}
template <typename T>
LNode<T> *appendTo(LNode<T> *n, T data) {
LNode<T> *newNode = createLNode(data);
return appendTo(n, newNode);
}
template <typename T>
int length(LNode<T> *n) {
if (n == nullptr) {
return 0;
}
return 1 + length(n->next);
}
template <typename T>
void iterate(LNode<T> *n) {
if (n == nullptr) {
cout << endl;
return;
}
cout << n->data << " ";
iterate(n->next);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment