简体   繁体   English

我的教授“实现静态抽象数据类型”是什么意思?

[英]What does my professor mean by “Implementing a static Abstract Data Type”?

In my class for our assignment we are making two different Abstract Data Types, Double Stack and Leaky Stack . 在我的课堂上,我们做两种不同的抽象数据类型, Double StackLeaky Stack I have no problem creating these, but my professor put in the assignment details for both of these ADTs to "Give an efficient static implementation of the ADT". 创建它们没有问题,但是我的教授为这两个ADT输入了作业详细信息,以“提供ADT的有效静态实现”。 But what the hell does that mean? 但是,这到底是什么意思? I could ask him tomorrow, but I want to get this assignment done today. 我明天可以问他,但是我想今天完成这项任务。 Anyone have any idea what he means by this? 有人知道他的意思是什么吗?

One possible interpretation is that the solutions are to use a fixed size "static" underlying structure (such as an array), rather than using a dynamic growing amount. 一种可能的解释是,解决方案是使用固定大小的“静态”底层结构(例如数组),而不是使用动态增长量。 Each stack, therefore, would have a pre-assigned maximum capacity. 因此,每个堆栈将具有预先分配的最大容量。 Therefore, I would expect an exception to be thrown on a push(...) operation that would exceed the capacity of the stack (just as a pop() operation would throw on an empty stack). 因此,我希望在push(...)操作上引发一个异常,该异常将超出堆栈的容量(就像pop()操作将在空堆栈上引发的异常)。

An example of a static implementation (though it allows setting the total capacity), might be like the following. 静态实现的示例(尽管它允许设置总容量),可能类似于以下示例。 Here the access will always be O(1) as an index is directly used, there is no traversal of the data structure, and no memory re-allocation. 由于直接使用索引,因此访问将始终为O(1),没有数据结构的遍历,也没有内存重新分配。 Note the code is example, and has not been tested. 请注意,该代码是示例,尚未经过测试。 The use of the Generic could be removed if the approach in question specifies the specific type of stack (such as int or char). 如果所讨论的方法指定了特定的堆栈类型(例如int或char),则可以删除对Generic的使用。

public class AnotherStack<T>
{
  private final T[] values;
  private int loc = 0;

  // must use the suppress, as we are using a raw Object array
  //   which is necessitated as cannot make a generic array  
  // See Effective Java
  @SuppressWarnings("unchecked")
  public AnotherStack(int size)
  {
    values = (T[])new Object[size];
  }


  public void push(T val)
  {
      if (loc < values.length) {
          values[loc++] = val;
      }
      else {
          throw new IllegalStateException("Stack full");
      }
  }

  public T pop()
  {
      if (loc == 0) {
          throw new IllegalStateException("Stack empty");
      }
      return (values[--loc]);
  }

  // other methods
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM