2011-04-08 44 views
1

现在我正在创建一个堆栈类。主要程序是:创建一个将数字推到数组长度的构造函数

class Program 
{ 
    static void Main(string[] args) 
    { 
     Queue myQue = new Queue(5); 
     Stack myStack = new Stack(5); 

     myStack.Push(1); 
     myStack.Push(2); 
     myStack.Push(3); 
     myStack.Push(4); 
     myStack.Push(5); 
     myStack.Push(6); 

     while (!myStack.IsEmpty) 
     { 
      Console.WriteLine(myStack.Pop()); 
     } 

     Console.WriteLine(myStack.Pop()); 

     Console.WriteLine("End of Stack"); 
    } 
} 

然后堆栈类如下:

class Stack 
{ 

    private int top; 

    private int[] anArray; 

    public bool IsFull 
    { 
     get 
     {   
      return top == anArray.Length - 1; 
     } 
    } 

    public bool IsEmpty 
    { 
     get 
     { 
      return top == -1; 
     } 
    } 

    public void Push(int valueToPush) 
    { 

     if (IsFull) 
     { 
      //do nothing 
     } 
     else 
     { 
      anArray[top] = valueToPush; 
      top = top + 1; 
     } 
    } 
    public int Pop() 
    { 
     if (IsEmpty) 
     { 
      //do nothing 
      return 
     } 
     else 
     { 
      int pop = anArray[top]; 
      top = top -1; 
      return pop; 
     } 
    } 
} 

我遇到的问题是,我需要返回什么,如果它是空的,但它不会让我由于int类型而返回NULL。

然后我想我要么跳过/不明白“构造函数”是什么。我明白,当我实例化“Stack myStack = new Stack(5);”它发送的是堆栈类“5”,但是如何将堆栈类中的5个数据发送到数组中?

+0

提供的Stack类没有一个带整数的构造函数。你确定那是你用的吗? System.Collections.Stack具有一个整数,并且它设置了堆栈的初始大小,它不会向其中推入任何内容。 – asawyer 2011-04-08 21:08:40

+1

这听起来很像一个班级作业? – 2011-04-08 21:11:28

+0

我创建一个堆栈类,不使用由C#本身定义的。我想我需要在Stack类中创建一个构造函数来获取5,然后将其设置为数组长度。 – Nogg 2011-04-08 21:11:33

回答

0

您没有构造函数。

添加这样的事情您的Stack类:

public Stack(int num) 
{ 
    Push(num); 
} 

刚刚看了你的评论,你想使用的号码创建数组的大小,从而以这种方式,你可以这样做:

int arrayLength; 
public Stack(int num) 
{ 
    arrayLength = num; 
    //doSomething() -> call a method or just create the array 
} 
0

您必须返回null的一个选项是将返回类型更改为int?但那么你将使用nullable type而不是直接使用int。

public int? Pop() 
    { 
     if (IsEmpty) 
     { 
      //do nothing 
      return null; 
     } 
... 

就构造函数来说,这是你将如何设置你的类。 5是否应该确定堆栈的大小,还是应该将其添加到堆栈中?

例如,如果构造函数被设计为设置堆栈的大小,您将执行以下操作。

class Stack 
{ 

    private int top; 

    private int[] anArray; 

    //This is your constructor. It will guarantee that your anArray will be initialized 
    public Stack(int size) 
    { 
     anArray = new int[size]; 
    } 

    ... 
+0

我想你的意思是anArray = new int [size]; – Pete 2011-04-08 21:14:50

+0

我在编辑,然后我看到你的评论:) – Joe 2011-04-08 21:15:20

+0

那么,5是什么arraylength需要。推(1)是数组中的第一个数字。 – Nogg 2011-04-08 21:29:01

0

在当你创建一个堆栈大多数情况下(新堆(5)),你传递值5,它是用来用来确定堆栈的大小(见http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=65)。

在您当前的堆栈实现中,您不指定构造函数。您需要创建的线沿线的东西:

public Stack(int x) { 
    // initialize your array (anArray) that represents a stack to size 5 
} 
0

1)试图Pop从空栈中的项目可以被视为无效操作,所以只要你允许检查用户是否堆栈是空的(你看到了这个),它在那里是完全正确的throw new InvalidOperationException("The stack is empty.")

2)构造函数问题 - 你的代码中没有构造函数。构造函数看起来像一个方法,但它没有返回值,并且与您的类具有相同的名称。它由new运算符调用,并且可以采用每种方法的参数。所以你可以采取那样的5

public Stack(int depth) 
{ 
    // do something with depth 
} 
相关问题