棧是一種線性的邏輯結構,可依賴陣列和連結串列這兩種物理結構實現。
棧包含入棧、出棧的操作,是一種FILO的結構。using System;
namespace Test01
{
public class MyStack
public int size = 0;
int[] stack;
public MyStack(int capacity)
stack = new int[capacity];
}
public int Pop()
if (size <= 0)
Console.WriteLine("棧為空");
return -1;
int value = stack[size - 1];
return value;
public bool Push(int value)
if (size > stack.Length)
Console.WriteLine("棧已滿");
return false;
stack[size] = value;
size++;
return true;
public void Println()
for (int i = 0; i < size; i++)
Console.WriteLine(stack[i]);
class Program
static void Main(string[] args)
MyStack myStack = new MyStack(10);
myStack.Push(111);
int value = myStack.Pop();
myStack.Println();
棧是一種線性的邏輯結構,可依賴陣列和連結串列這兩種物理結構實現。
棧包含入棧、出棧的操作,是一種FILO的結構。using System;
namespace Test01
{
public class MyStack
{
public int size = 0;
int[] stack;
public MyStack(int capacity)
{
stack = new int[capacity];
}
public int Pop()
{
if (size <= 0)
{
Console.WriteLine("棧為空");
return -1;
}
int value = stack[size - 1];
return value;
}
public bool Push(int value)
{
if (size > stack.Length)
{
Console.WriteLine("棧已滿");
return false;
}
stack[size] = value;
size++;
return true;
}
public void Println()
{
for (int i = 0; i < size; i++)
{
Console.WriteLine(stack[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
MyStack myStack = new MyStack(10);
myStack.Push(111);
int value = myStack.Pop();
myStack.Println();
}
}