Skip to content

Instantly share code, notes, and snippets.

View bottomupmergesort's full-sized avatar
💭
Coding

Max Goren bottomupmergesort

💭
Coding
View GitHub Profile
@bottomupmergesort
bottomupmergesort / cointoss.c
Created February 15, 2022 18:38
Coin Toss!
unsigned int cointToss()
{
if (rand() % 2 == 0)
return 1;
return 0;
}
//this can be slimmed down even further:
unsigned int cointToss() { return (rand() % 2 ==0); }
@bottomupmergesort
bottomupmergesort / otherqueueoperations.h
Created February 12, 2022 17:05
size() empty() and front()
void* queueFront(QUEUE* q)
{
return q->head->next->element;
}
int queueEmpty(QUEUE* q)
{
return (q->head->next == q->z);
}
void queuePop(QUEUE** q)
{
qnode* t = (*q)->head->next;
(*q)->head->next = (*q)->head->next->next;
(*q)->head->next->prev = (*q)->head;
free(t);
(*q)->size--;
}
@bottomupmergesort
bottomupmergesort / queuepush.h
Created February 12, 2022 16:56
queue push operation
void queuePush(QUEUE** q, void* element)
{
qnode* n = new_qnode(element);
n->prev = (*q)->z->prev;
n->next = (*q)->z;
(*q)->z->prev->next = n;
(*q)->z->prev = n;
(*q)->size++;
}
@bottomupmergesort
bottomupmergesort / initializequeue.h
Created February 12, 2022 16:53
allocating nodes and the queue itsself
qnode* new_qnode(void* element)
{
qnode* nqn = (struct qnode*)malloc(sizeof(qnode));
nqn->element = element;
nqn->prev = NULL;
nqn->next = NULL;
return nqn;
}
QUEUE* newQueue()
@bottomupmergesort
bottomupmergesort / queueds.h
Created February 12, 2022 16:50
node and queue definitions
typedef struct qnode {
void* element;
struct qnode* next;
struct qnode* prev;
}qnode;
typedef struct QUEUE {
qnode* head;
qnode* z;
int size;
@bottomupmergesort
bottomupmergesort / ds_test.cpp
Created January 30, 2022 19:09
testing insert/search on different data structures
#include <iostream>
#include <chrono>
#include "sorted_array_dict.hpp"
#include "list_dict.hpp"
#include "bst_dict.hpp"
using namespace std;
void usinglist()
{
@bottomupmergesort
bottomupmergesort / array_search.cpp
Created January 30, 2022 19:00
binary search in an array
T2 find(T key)
{
int h = N;
int l = 0;
while (l < h)
{
int mid = (l+h) / 2;
if (key == arr[mid].key)
return arr[mid].value;
if (key < arr[mid].key) h = mid - 1;
@bottomupmergesort
bottomupmergesort / linklist_search.hpp
Created January 30, 2022 18:59
Searching a linked list
@bottomupmergesort
bottomupmergesort / dict_iface.hpp
Created January 30, 2022 18:51
Dictionary interface
#ifndef DICT_HPP
#define DICT_HPP
//pure virtual class defining the dictionary
//abstract data type interface
template <typename T, typename T2>
class Dict {
public:
virtual void insert(T key, T2 value) = 0;
virtual void remove(T key) = 0;