自己构造CStack类,C#实现栈。
栈后进先出(LIFO)
进栈Push()
出栈Pop()
读取栈顶Peek()
清空Clear()
class CStack
{
//索引
private int p_index;
private ArrayList list;
//构造函数
public CStack()
{
list = new ArrayList();
p_index = -1;
}
//只读栈中的长度
public int count
{
get
{
return list.Count;
}
}
//加入栈
public void Push(object item)
{
list.Add(item);
p_index++;
}
//移除顶数据
public object Pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
//清空
public void Clear()
{
list.Clear();
p_index = -1;
}
//查看顶数据
public object Peek()
{
return list[p_index];
}
}
//运用
class Program
{
static void Main(string[] args)
{
CStack cstack = new CStack();
string str;
string word = "madam";
bool isok = true;
for (int i = 0; i < word.Length; i++)
{
cstack.Push(word.Substring(i,1));
}
int pos = 0;
while (cstack.count >0 )
{
str = cstack.Pop().ToString();
if(str != word.Substring(pos, 1))
{
isok = false;
break;
}
pos++;
}
if(isok)
Console.WriteLine(word + "是回文");
else
Console.WriteLine(word + "不是回文");
//C#自身的Stack类,栈的实现
Console.WriteLine("C#--Stack类");
Stack stack = new Stack();
for(int i = 0; i < word.Length; i++)
stack.Push(word.Substring(i, 1));
int index = 0;
bool isokstack = true;
while (stack.Count > 0)
{
if (stack.Pop().ToString() != word.Substring(index, 1))
{
isokstack = false;
break;
}
index++;
}
if (isokstack)
Console.WriteLine(word + "是回文");
else
Console.WriteLine(word + "不是回文");
Console.ReadKey();
}
}
栈后进先出(LIFO)
进栈Push()
出栈Pop()
读取栈顶Peek()
清空Clear()
class CStack
{
//索引
private int p_index;
private ArrayList list;
//构造函数
public CStack()
{
list = new ArrayList();
p_index = -1;
}
//只读栈中的长度
public int count
{
get
{
return list.Count;
}
}
//加入栈
public void Push(object item)
{
list.Add(item);
p_index++;
}
//移除顶数据
public object Pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
//清空
public void Clear()
{
list.Clear();
p_index = -1;
}
//查看顶数据
public object Peek()
{
return list[p_index];
}
}
//运用
class Program
{
static void Main(string[] args)
{
CStack cstack = new CStack();
string str;
string word = "madam";
bool isok = true;
for (int i = 0; i < word.Length; i++)
{
cstack.Push(word.Substring(i,1));
}
int pos = 0;
while (cstack.count >0 )
{
str = cstack.Pop().ToString();
if(str != word.Substring(pos, 1))
{
isok = false;
break;
}
pos++;
}
if(isok)
Console.WriteLine(word + "是回文");
else
Console.WriteLine(word + "不是回文");
//C#自身的Stack类,栈的实现
Console.WriteLine("C#--Stack类");
Stack stack = new Stack();
for(int i = 0; i < word.Length; i++)
stack.Push(word.Substring(i, 1));
int index = 0;
bool isokstack = true;
while (stack.Count > 0)
{
if (stack.Pop().ToString() != word.Substring(index, 1))
{
isokstack = false;
break;
}
index++;
}
if (isokstack)
Console.WriteLine(word + "是回文");
else
Console.WriteLine(word + "不是回文");
Console.ReadKey();
}
}