Last active
August 29, 2015 14:17
-
-
Save danalexilewis/bd82aa16b2d0a8f7f186 to your computer and use it in GitHub Desktop.
Demo of C# Diy Stack
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
using System; | |
using System.Collections; | |
namespace StackQueue | |
{ | |
public class MyStack | |
{ | |
object[] store = new object[0]; | |
public void Push(object x) | |
{ | |
object[] new_store = new object[store.Length +1]; | |
new_store[new_store.Length -1] = x; | |
store = new_store; | |
} | |
public object Pop() | |
{ | |
object last = store[store.Length - 1]; | |
object[] new_store = new object[store.Length -1]; | |
for (int i = 0; i < new_store.Length; i++) | |
{ | |
new_store[i] = store[i]; | |
} | |
store = new_store; | |
return last; | |
} | |
public object Peek() | |
{ | |
return store.Last(); | |
} | |
public bool Empty() | |
{ | |
if (store.Length > 0) | |
{ | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment