Skip to content

Instantly share code, notes, and snippets.

View simonayzman's full-sized avatar

Simon Ayzman simonayzman

View GitHub Profile
@simonayzman
simonayzman / main.cpp
Last active March 27, 2016 06:01
Comparators, Polymorphism, and Storing Objects in (Sorted) Data Structures
#include <iostream>
#include "SortedVector.h" // Found in https://github.com/CSCI235-Ayzman/SortedStructures
#include "SortedLinkedList.h" // Found in https://github.com/CSCI235-Ayzman/SortedStructures
using namespace std;
class Residence
{
public:
Residence(double newPrice = 1.0) : price(newPrice) {}
@simonayzman
simonayzman / main.cpp
Last active September 21, 2016 17:12
Polymorphism with Residences (with Abstract Classes)
#include <iostream>
#include <vector>
using namespace std;
class Residence {
public:
Residence(double newPrice = 0.0) : price(newPrice) {}
virtual ~Residence() {}
double getPrice() { return price; }
@simonayzman
simonayzman / main.cpp
Last active September 21, 2016 17:05
Polymorphism with Residences (without Abstract Classes)
#include <iostream>
#include <vector>
using namespace std;
class Residence {
public:
Residence(double newPrice = 1.0) : price(newPrice) {}
virtual ~Residence() {}
double getPrice() { return price; }
@simonayzman
simonayzman / main.cpp
Created September 20, 2016 19:56
Templated Linear Search
template<typename T>
int find(const T& item, T* array, unsigned int size)
{
for (unsigned int i = 0; i < size; ++i)
{
// Assume T has properly overloaded operator==
if (array[i] == item)
{
return i;
}
@simonayzman
simonayzman / main.cpp
Created September 20, 2016 19:57
Templated Pair Class
template <typename T1, typename T2>
struct Pair{
T1 first;
T2 second;
};
@simonayzman
simonayzman / main.cpp
Created September 20, 2016 20:00
Polymorphism with Shapes
class Polygon{
public:
virtual ~Polygon() {}
int perimeter(){
double sum = 0;
for(int i = 0; i < sides.size(); i++){
sum += sides[i];
}
return sum;
@simonayzman
simonayzman / main.cpp
Last active October 4, 2016 17:24
Polymorphism with Instructors
template<class T>
string toString(T type) {
std::ostringstream strs;
strs << type;
return strs.str();
}
class Instructor {
public:
Instructor(string theName) : name(theName) {}
@simonayzman
simonayzman / main.cpp
Last active October 6, 2016 18:52
Deleting N nodes after every M nodes
void LinkedList::skipAndRemove(int skipNum, int removeNum){
// There is either currently nothing in the list
// or you're instructed not to remove anything
if (head == NULL || removeNum == 0) {
return;
}
// Skip nothing; therefore, remove everything
if (skipNum == 0) {
this->clear(); // Assume this exists
@simonayzman
simonayzman / main.cpp
Created November 3, 2016 19:29
Determing if a binary tree is balanced
// Note that this is an interesting kind of tree, where there is no overall structure that contains nodes.
// Instead the tree is itself a node.
template<class ItemType>
class Tree {
private:
Tree<ItemType> * left;
Tree<ItemType> * right;
ItemType data;
public:
@simonayzman
simonayzman / main.cpp
Created November 16, 2016 04:32
Generate a Balanced BST
#include <iostream>
#include <vector>
using namespace std;
template<class T>
struct TreeNode{
T data;
TreeNode<T> * right;
TreeNode<T> * left;
};