数据结构学习(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;
                }

            }

        }

    }

}



你可能感兴趣的:(数据结构)