Stack implementation in C#
public class Stack
{
readonly int _capacity;
int top;
int[] _stack ;
public int Top
{
get { return top; }
}
public Stack()
{
_capacity = 1000;
top = -1;
}
public Stack( int capacity)
{
_capacity = capacity;
_stack = new int[_capacity];
top = -1;
}
public void Push(int item)
{
if (top < _capacity - 1)
{
_stack[top + 1] = item;
top++;
}
else
{
Console.WriteLine("Stack is full");
}
}
public int Pop()
{
int itemToReturn=-1;
if (top == -1)
{
Console.WriteLine("Stack empty!!");
}
else
{
itemToReturn= _stack[top];
top--;
}
return itemToReturn;
}
public int Peek()
{
if (top != -1)
{
return _stack[top];
}
else
{
Console.Write("Stack is empty!!!");
return -1;
}
}
}
Test :
class StackTest
{
public static void Main()
{
Stack s = new Stack(5);
Console.WriteLine("Stack top=" + s.Top);
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
Console.WriteLine("Stack item "+ s.Pop() +" top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
// ************** Test 2 **************************
s.Push(7);
s.Push(8);
s.Push(9);
s.Push(10);
s.Push(11);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.Read();
}
}
public class Stack
{
readonly int _capacity;
int top;
int[] _stack ;
public int Top
{
get { return top; }
}
public Stack()
{
_capacity = 1000;
top = -1;
}
public Stack( int capacity)
{
_capacity = capacity;
_stack = new int[_capacity];
top = -1;
}
public void Push(int item)
{
if (top < _capacity - 1)
{
_stack[top + 1] = item;
top++;
}
else
{
Console.WriteLine("Stack is full");
}
}
public int Pop()
{
int itemToReturn=-1;
if (top == -1)
{
Console.WriteLine("Stack empty!!");
}
else
{
itemToReturn= _stack[top];
top--;
}
return itemToReturn;
}
public int Peek()
{
if (top != -1)
{
return _stack[top];
}
else
{
Console.Write("Stack is empty!!!");
return -1;
}
}
}
Test :
class StackTest
{
public static void Main()
{
Stack s = new Stack(5);
Console.WriteLine("Stack top=" + s.Top);
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(5);
s.Push(6);
Console.WriteLine("Stack item "+ s.Pop() +" top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
// ************** Test 2 **************************
s.Push(7);
s.Push(8);
s.Push(9);
s.Push(10);
s.Push(11);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.WriteLine("Stack item " + s.Pop() + " top=" + s.Top);
Console.Read();
}
}
No comments:
Post a Comment