-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
70 lines (62 loc) · 1.41 KB
/
Stack.cpp
File metadata and controls
70 lines (62 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Team H08: William O'Rosky, Akshay Jagadeesh, Tyler Nardecchia
//CSCE 121-509
//Due: December 2, 2015
//Stack.cpp
#include "Stack.h"
//creates the stack of pancakes from 1 to the size of the stack
//and then shuffles the stack
void Stack::create_stack(int num_pancakes)
{
for (int i = 1; i <= num_pancakes; ++i)
{
Pancake p{i};
add(p);
}
unsigned seed = time(NULL);
if(num_pancakes != 2)
shuffle(stack.begin(), stack.end(), std::default_random_engine(seed));
}
//adds a pancake to the stack
void Stack::add(Pancake p)
{
stack.push_back(p);
}
//flips the stack from the selected pancake to the top
//using iterators moving up and down the stack which
//swap the pancakes until the iterators are at the same place
void Stack::flip(int n)
{
if(n > stack.size())
error("flip size greater than number of pancakes");
else
{
int d = stack.size()-1-n;
for(int start = stack.size()-1;start>0;start--)
{
if (start <= d)
return;
else
{
Pancake temp = stack[d];
stack[d] = stack[start];
stack[start] = temp;
d++;
}
}
}
}
//returns the size of the stack
int Stack::size()
{
return stack.size();
}
//returns a specific pancake in the stack
Pancake Stack::get(int n)
{
return stack[n];
}
//returns the entires Stack object
vector<Pancake> Stack::get_stack()
{
return stack;
}