Skip to content

Instantly share code, notes, and snippets.

@alpersilistre
Last active December 17, 2015 13:00
Show Gist options
  • Save alpersilistre/17ff611a9d0dd43b28ec to your computer and use it in GitHub Desktop.
Save alpersilistre/17ff611a9d0dd43b28ec to your computer and use it in GitHub Desktop.
C# Stack Implementation (Not Finished)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackTest
{
public class Stack
{
private int position;
object[] data = new object[10];
public void Push(object obj)
{
if (position >= 10)
{
Console.WriteLine("You can't exceed limit...");
}
data[position++] = obj;
}
public object Pop()
{
if (position == 0)
{
Console.WriteLine("No more item in stack...");
return data[position];
}
data[--position] = null;
return data[position];
}
public void Print()
{
foreach (var o in data)
{
if (o == null)
{
Console.WriteLine("Null");
}
else
{
Console.WriteLine(o);
}
}
}
}
class ObjectType
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.Push("sausage");
stack.Print();
Console.WriteLine("------");
stack.Push("salam");
stack.Print();
Console.WriteLine("------");
stack.Pop();
stack.Print();
Console.WriteLine("------");
stack.Push("pizza");
stack.Print();
Console.WriteLine("------");
stack.Pop();
stack.Print();
Console.WriteLine("------");
stack.Pop();
stack.Print();
Console.WriteLine("------");
stack.Pop();
stack.Print();
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment