数据结构学习(4):栈

栈:
using System;

namespace DataStructure.Stack
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class StackNode
    
{
        Object item;
        StackNode link;
    }

    
class Stack
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
        }


        
class Stack
        
{
            
private StackNode topNode;

            
public boolean empty()
            
{
                
return (topNode==null);
            }


            
public void push(Object X)
            
{
                StackNode newNode
=new StackNode();
                newNode.item
=X;
                newNode.link
=topNode;
                topNode
=newNode;
            }


            
public Object pop()
            
{
                
if(topNode==null)
                
{
                    
return null;
                }

                
else
                
{
                    StackNode tempNode
=topNode;
                    topNode
=topNode.link;
                    
return tempNode.item;
                }

            }

            
public Object peek()
            
{
                
if(topNode==null)
                
{
                    
return null;
                }

                
else
                
{
                    
return topNode.item;
                }

            }

        }

    }

}



posted @ 2006-12-14 19:05  大天使泰瑞尔  阅读(202)  评论(0编辑  收藏  举报