namespace Stack
{
public partial class Form1 : Form
{
Random rand = new Random();
Stack<int> intstack = new Stack<int>();
public Form1()
{
InitializeComponent();
}
private void Display(IEnumerable<int> stack)
{
listBox1.Items.Clear();
foreach (int num in stack)
{
listBox1.Items.Add(num);
}
}
private void btnPush_Click(object sender, EventArgs e)
{
//Generate random number
int num = rand.Next(100);
//add it to the stack (adds it to the top of the stack)
intstack.Push(num);
//display
Display(intstack);
}
private void btnPop_Click(object sender, EventArgs e)
{
if (intstack.Count > 0)
{
//Remove the item from the top of the stack
int num = intstack.Pop();
MessageBox.Show(string.Format("{0} was popped from top of the stack", num));
Display(intstack);
}
}
private void btnPeek_Click(object sender, EventArgs e)
{
//This Peek method returns the item at the head of the queue
//without removing it.
//Who is on first?
//make sure to check that the queue is not empty to avoid an
//InvalidOeprationException from the system
if (intstack.Count > 0)
{
int num = intstack.Peek();
MessageBox.Show(String.Format("{0} is on first", num));
//display to prove that the Peek does not remove an item
Display(intstack);
}
else
MessageBox.Show("Sorry!.. the stack is empty");
}
private void btnCopyToArray_Click(object sender, EventArgs e)
{
}
}
}
//Lab Assignment _ Due MONDAY [Nov 17, 2014]
//==============
//New Project
//Create a queue and populate it with some numbers
//Reverse the content of the queue (hint: with the help of a stack)
//
//Create two stacks, populate them with some values. Write code to merge them into
//a single stack.
//(stack2 should be stacked on top of stack1, keep the same ordering)
//
//*****Challenge****
//Create a two dimensional array of (12 by 12)
//Initialize the starting cell (starting cell should be on the edge of the array, should be of type Point
//because each cell is described by its row# and col# or i and j.
//March down the array either going left, right or down (assuming you're starting from the top)
//March one cell at a time (no double hopping).
//The decision to go right, left, or down should be random
//Each time you move to a cell, save it to the stack
//Stop when you reach any other cell on the edge
//Display all the cells you marched on.

No comments:
Post a Comment