-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackController.cs
More file actions
105 lines (96 loc) · 3.27 KB
/
StackController.cs
File metadata and controls
105 lines (96 loc) · 3.27 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace StackQueueDictionary.Controllers
{
public class StackController : Controller
{
public static Stack<string> PanStack = new Stack<string>(); //static means it lives until the program dies
// GET: Stack
public ActionResult Index()
{
ViewBag.PanStack = PanStack; //Allows stack to exist before running other items
return View();
}
public ActionResult AddOne()
{
//Adds (pushes) a single item to the stack
PanStack.Push("New Entry " + (PanStack.Count + 1));
ViewBag.PanStack = PanStack;
return View("Index");
}
public ActionResult AddHuge()
{
//Clear data structure
PanStack.Clear();
//Adds 2,000 items to the data structure
for (int i = 0; i < 2000; i++)
{
PanStack.Push("New Entry " + (PanStack.Count + 1));
}
ViewBag.PanStack = PanStack;
return View("Index");
}
public ActionResult DisplayEntries()
{
//Displays all items in the stack. If the stack is empty, a message will inform the user.
if (PanStack.Count == 0)
{
ViewBag.Comments = "No values found in pancake stack";
}
else
{
ViewBag.PanStack = PanStack;
}
return View("Index");
}
public ActionResult DeleteOne()
{
//Deletes (pops) one item from the stack
if (PanStack.Count != 0)
{
PanStack.Pop();
ViewBag.PanStack = PanStack;
}
else
{
ViewBag.PanStack = PanStack;
ViewBag.Comments = "Deletion action could not be completed. Stack is empty.";
}
return View("Index");
}
public ActionResult ClearEntries()
{
//Clears the stack
PanStack.Clear();
ViewBag.PanStack = PanStack;
return View("Index");
}
public ActionResult Search()
{
//Creates random number generator, stopwatch object, timespan object, and a random variable to hold the generated number.
Random rnd = new Random();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
TimeSpan ts;
int rndEntry = rnd.Next(0, 2001);
//Searches the pancake stack to find an entry matching the randomly generated number.
sw.Start();
if (PanStack.Contains("New Entry " + rndEntry))
{
sw.Stop();
ts = sw.Elapsed;
ViewBag.Comments = "Entry " + rndEntry + " found. Time: " + ts;
}
else
{
sw.Stop();
ts = sw.Elapsed;
ViewBag.Comments = "Entry " + rndEntry + " not found. Time to search pancake stack: " + ts;
}
ViewBag.PanStack = PanStack; //Updates the ViewBag to display in the View
return View("Index");
}
}
}