-3

好吧,我正在做作业,但不确定我的问题是什么

这是我的任务

说明 这个作业有两个部分。这些部分是相关的,但它们的实现方式不同。为了更好地理解赋值本身,回顾书籍、幻灯片、笔记等并执行基于数组和基于链表的常规堆栈以及堆栈 ADT 可能会有所帮助。

第一部分 堆栈的一个广泛用途是提供撤消操作,我们在许多不同的应用程序中都很熟悉。虽然可以使用无界堆栈(只要内存允许,堆栈就会不断增长)来实现对撤消的支持,但许多应用程序仅对此类撤消历史记录提供有限的支持。换句话说,堆栈是固定容量的。当这样的堆栈已满并调用 push 时,而不是抛出异常,更典型的方法是在顶部接受已推送的元素,同时从堆栈底部移除最旧的元素以腾出空间。这被称为“泄漏”。请注意,这并不意味着 ADT 公开了一种允许直接从底部移除的方法。这仅在堆栈变满时执行。对于这一部分,您将使用一些基于数组的实现来实现这样的 LeakyStack 抽象。请注意,您必须创建一个 Leaky Stack 接口,然后使用 C++ : 运算符通过您的 LeakyArrayStack 实现来实现该接口(使用公共继承)。请参阅分配说明末尾附近指定的接口。

第二部分 重复第一部分,但使用单链表而不是数组来存储实际数据,并允许将最大容量指定为构造函数的参数。

注意: • 基于数组和基于链表的泄漏堆栈都应该使用相同的 LeakyStackInterface,如下所述。记住——这是一个 LeakyStack ADT。它指定了 LeakyStack 做什么,而不是如何。因此,为了提供实现,接口不应有所不同。• 在两个部分中使用公共继承 • 在尝试执行第二部分之前,您应该首先编写一个 SinglyLinkedList 类 o 然后,使用包含(聚合或组合,has-a 关系)来实现第二部分

我必须使用图片中的界面

这是我的代码

    #include <iostream>
     #ifndef LEAKYStacksINTERFACE
     #define LEAKYStacksINTERFACE
      #define cap 10
     using namespace std;



     template<typename ItemType>
     class LeakyStacksInterface
      {  public:
      //returns whether Stacks is empty or not
     virtual bool isEmpty() const = 0;

     //adds a new entry to the top of the Stacks
     //if the Stacks is full, the bottom item is removed
     //or "leaked" first, and then the new item is set to the top
     //---> If the Stacks was full when the push was attempted, return false
     //---> If the Stacks was not full when the push was attempted, return true
    virtual bool push(const ItemType& newEntry) = 0;

     //remove the top item
     //if the Stacks is empty, return false to indicate failure
     virtual bool pop() = 0;

      //return a copy of the top of the Stacks
   virtual ItemType peek() const = 0;

  //destroys the Stacks and frees up memory
   //that was allocated
    // virtual ~StacksInterface() {}
   };

template<typename ItemType>
struct node
{
    int data;
    struct node *next;
};




template<typename ItemType>
class Stacks : public LeakyStacksInterface<ItemType>
{

    struct node<ItemType> *top;

    public:
            int size;
            ItemType *myArray;

    Stacks()
    {
        top=NULL;
        size = 0;
        myArray = new ItemType[cap];
    }

    ~Stacks() {
        size = 0;
    }

    public:
   // pure virtual function providing interface framework.
    bool isEmpty() const {

        return(size == 0);

    }
 bool push(const ItemType& newEntry) {
        if(size == cap) {
            for(int i = 0; i < size-1; i++) {
                myArray[i] = myArray[i+1];
            }
            myArray[size-1] = newEntry;
            return false;
        }
 }
    ItemType peek() const {
        return myArray[size-1];
    }
     void display()
   {
      cout<<"Stacks: [ ";
      for(int i=size-1; i>=0; i--)
      {
         cout<<myArray[i]<<" ";
      }
      cout<<" ] "<<endl;
   }
};


int main()
{
    Stacks s;

   int choice;

    while(1)

    {

        cout<<"n-----------------------------------------------------------";

        cout<<"nttSTACK USING LINKED LISTnn";

        cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";

        cout<<"nEnter your choice(1-4): ";

        cin>>choice;

        switch(choice)

        {

            case 1:

                s.push();

                break;

            case 2:

                s.pop();

                break;

            case 3:

                s.show();

                break;

            case 4:

                return 0;

                break;

            default:

                cout<<"Please enter correct choice(1-4)!!";

                break;

        }

    }

    return 0;
}

#endif

这是我的错误:错误:在's'之前缺少模板参数错误:预期';' before 's' ERROR:'s' is not delcared in this scope

请帮忙!谢谢你!

界面图片

4

1 回答 1

2

Stacks是一个类模板,所以要使用它,你必须提供一个模板参数,比如

Stacks<int> s;
于 2017-04-23T19:59:45.187 回答