Created
July 14, 2015 21:21
-
-
Save tomkowz/81e514284ed982251e38 to your computer and use it in GitHub Desktop.
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
// | |
// main.cpp | |
// ctci-3-3 | |
// | |
// Created by Tomasz Szulc on 14/07/15. | |
// Copyright (c) 2015 Tomasz Szulc. All rights reserved. | |
// | |
#include <iostream> | |
template <typename T> | |
class Stack { | |
private: | |
class Node { | |
public: | |
T value; | |
Node* next; | |
Node(T value) { | |
this->value = value; | |
this->next = nullptr; | |
}; | |
~Node() { | |
this->next = nullptr; | |
} | |
}; | |
Node* top = nullptr; | |
public: | |
void push(T value) { | |
Node* newNode = new Node(value); | |
newNode->next = top; | |
top = newNode; | |
} | |
~Stack() { | |
Node* current = top; | |
while (current != NULL) { | |
Node* next = current->next; | |
delete current; | |
current = next; | |
} | |
top = nullptr; | |
} | |
void pop() { | |
if (top == nullptr) { return; } | |
Node* oldNode = top; | |
top = top->next; | |
delete oldNode; | |
} | |
void print() { | |
Node* n = top; | |
while (n != NULL) { | |
std::cout << n->value << std::endl; | |
n = n->next; | |
} | |
} | |
}; | |
template <typename T> | |
class SetOfStacks { | |
private: | |
int maxCapacity; | |
int numberOfStacks; | |
// elements on current stack | |
int numberOfElements; | |
Stack<T>* stacks; | |
public: | |
SetOfStacks(int capacity) { | |
maxCapacity = capacity; | |
numberOfStacks = 0; | |
stacks = nullptr; | |
numberOfElements = 0; | |
} | |
void push(T value) { | |
// Create new stack and add it to array if elements on current stack | |
// reach capacity or if there is not stacks yet. | |
if (numberOfStacks == 0 || (numberOfElements + 1) > maxCapacity) { | |
// delete all the current stacks from `stacks` field | |
// keep it in `tmp` property | |
Stack<T>* tmp = stacks; | |
// initialize new bigger array of stacks | |
stacks = new Stack<T>[numberOfStacks + 1]; | |
// assign stacks from old to new stacks array | |
for (int i = 0; i < numberOfStacks; i++) { | |
stacks[i] = tmp[i]; | |
} | |
delete[] tmp; | |
tmp = nullptr; | |
// create new stack at the end of the array | |
stacks[numberOfStacks] = Stack<T>(); | |
// update counters | |
numberOfElements = 0; | |
numberOfStacks++; | |
} | |
// add element to current stack | |
stacks[numberOfStacks-1].push(value); | |
numberOfElements++; | |
} | |
void print() { | |
for (int i = 0; i < numberOfStacks; i++) { | |
Stack<T> stack = stacks[i]; | |
stack.print(); | |
} | |
} | |
}; | |
int main(int argc, const char * argv[]) { | |
SetOfStacks<int> s = SetOfStacks<int>(3); | |
s.push(11); | |
s.push(12); | |
s.push(13); | |
s.push(21); | |
s.push(22); | |
s.push(23); | |
s.push(31); | |
s.push(32); | |
s.push(33); | |
s.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment