#include "Stack.h"
#include <iostream>
using namespace std;
Stack FillEmptyStackWithIntegersAndReturnIt();
void PopAndPrintStack(Stack Stk);
int main()
{
{
cout << "Creating a new Stack.\n";
Stack IntStack = FillEmptyStackWithIntegersAndReturnIt();
cout << "Making a copy of original stack.\n";
Stack CopyOfIntStack;
CopyOfIntStack = IntStack;
cout << "Topping and Popping the original Stack:\n";
PopAndPrintStack(IntStack);
cout << "Topping and Popping the copy stack.\n";
PopAndPrintStack(CopyOfIntStack);
}
system("pause");
return 0;
}
//----------------------------------------------------------------
void PopAndPrintStack(Stack Stk)
{
cout << "Now printing the integers pushed on the stack.\n";
while (!Stk.isEmpty())
{
cout << Stk.top().Integer << " ";
Stk.pop();
}
cout << endl;
}
//-----------------------------------------------------------------
Stack FillEmptyStackWithIntegersAndReturnIt()
{
Stack Stk;
cout << "We will push some integers on the stack now.\n";
bool done = false;
int val = int();
while (!done)
{
cout << "Please enter an integer to be pushed on stack : ";
cin >> val;
ItemType item;
item.Integer = val;
Stk.push(item);
cout << "The number of items on stack :" << Stk.getNumItems() << endl;
cout << "The capacity of stack array is: " << Stk.getArrayCapacity() << endl;
cout << "More data? Enter 0 to continue and 1 to exit: ";
cin >> done;
}
return Stk;
}
//OUTPUT//
Creating a new Stack.
We will push some integers on the stack now.
Please enter an integer to be pushed on stack : 10
Using the existing array to push item on stack.
The number of items on stack is: 1
The number of items on stack :1
The capacity of stack array is: 1
More data? Enter 0 to continue and 1 to exit: 0
Please enter an integer to be pushed on stack : 9
Expanding the existing array to push item on stack.
Number of items in queue now: 2
The number of items on stack :2
The capacity of stack array is: 2
More data? Enter 0 to continue and 1 to exit: 0
Please enter an integer to be pushed on stack : 8
Expanding the existing array to push item on stack.
Number of items in queue now: 3
The number of items on stack :3
The capacity of stack array is: 3
More data? Enter 0 to continue and 1 to exit: 0
Please enter an integer to be pushed on stack : 7
Expanding the existing array to push item on stack.
Number of items in queue now: 4
The number of items on stack :4
The capacity of stack array is: 4
More data? Enter 0 to continue and 1 to exit: 0
Please enter an integer to be pushed on stack : 6
Expanding the existing array to push item on stack.
Number of items in queue now: 5
The number of items on stack :5
The capacity of stack array is: 5
More data? Enter 0 to continue and 1 to exit: 1
From Stack Copy Constructor.
From Stack Destructor.
Making a copy of original stack.
From Stack assignment operator.
Topping and Popping the original Stack:
From Stack Copy Constructor.
Now printing the integers pushed on the stack.
6 10 9 8 7
From Stack Destructor.
Topping and Popping the copy stack.
From Stack Copy Constructor.
Now printing the integers pushed on the stack.
6 10 9 8 7
From Stack Destructor.
From Stack Destructor.
From Stack Destructor.
Press any key to continue . . .
我已经使用指针和动态内存分配编写了一个自扩展堆栈的类,我无法弄清楚为什么我的堆栈按原样推送它。
#include "StackInterface.h"
class Stack :public StackInterface
{
private:
const static int MAX = 1;
const static int GROWBY = 1;
int top_position;
ItemType * items;
size_t numItems;
size_t arrayCapacity;
bool isFull() const;
protected:
void copy(const Stack& Stk);
public:
Stack();
void push(ItemType item);
void pop();
ItemType top() const;
bool isEmpty() const;
~Stack();
Stack(const Stack& st);
const Stack& operator=(const Stack& Stk);
size_t getArrayCapacity() const;
size_t getNumItems() const;
};
//----------------------------------------------------
#include "Stack.h"
#include <iostream>
using namespace std;
Stack::Stack()
{
items = new ItemType[MAX];
numItems = 0;
top_position = -1;
arrayCapacity = MAX;
}
//-------------------------------
void Stack::push(ItemType newItem)
{
if (arrayCapacity > this->numItems)
{
cout << "Using the existing array to push item on stack. \n";
top_position = (top_position + 1) % arrayCapacity;
items[top_position] = newItem;
numItems = numItems + 1;
cout << "The number of items on stack is: " << numItems << endl;
}
else
{
cout << "Expanding the existing array to push item on stack. \n";
size_t len = arrayCapacity + GROWBY;
ItemType * Temp;
Temp = new ItemType[len];
for (size_t index = 0; index<this->numItems; index++)
{
Temp[index] = items[top_position];
top_position = (top_position + 1) % arrayCapacity;
}
delete[] items;
items = Temp;
Temp = nullptr;
arrayCapacity = len;
top_position = numItems -1;
top_position = (top_position + 1) % arrayCapacity;
items[top_position] = newItem;
this->numItems = this->numItems + 1;
cout << "Number of items in queue now: " << numItems << endl;
}
}
当我跑步并按顺序输入10 9 8 7 6 我得到以下输出:6 10 9 8 7 当它应该像这样的顶部:6 7 8 9 10
如果需要更多代码,我可以提供。
答案 0 :(得分:0)
好的,我已经解决了我的问题,我只需要编辑
for (size_t index = 0; index<this->numItems; index++)
{
Temp[index] = items[(top_position + 1) % arrayCapacity];
top_position = (top_position + 1) % arrayCapacity;
}