c++ - C ++如何实现可调整大小的堆栈数组?
问题描述
在我早期的一个实验练习中,我实现了一个简单的堆栈程序
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int count = 0; //stores the indexed number
string myStack[20]; //stores the data from file
string fileName;
fstream readFile;
string storeFile; //stores the data as a string
//function declarations/prototypes
void push(string aString);
string top();
string pop();
bool isEmpty();
int main(int argc, char *argv[])
{
fileName = "file.txt";
readFile.open(fileName); //attempts to read the file
while(readFile >> storeFile){
if(readFile.bad()) {
cerr << "File failed to read " << endl;
break; //loop terminates
} else {
push(storeFile); //pushes file to stack
}
}
readFile.close();
while(isEmpty() !=true) { //while file is not emptpy return the top stack.
cout << top() << endl;
pop();
}
return 0;
}
void push(string value) {
myStack[count] = value;
count++;
}
string pop() {
if(count < 0) {
return NULL;
} else {
count--; //decrement count for stack
return myStack[count]; //return top value
}
}
string top() { //returns the current element at the top of the stack
return myStack[count];
}
bool isEmpty() { //checks whether or not the stack is empty
if(count < 0) {
return true;
}
else return false;
}
我现在需要重新访问这个程序,但是,这次修改它,以便如果您尝试将数据推送到完整堆栈中,您应该创建一个两倍于当前大小的新数组,将数据从旧堆栈复制到新堆栈然后在不使用 STL 或类的情况下删除旧堆栈。我们被允许使用动态内存
因此,例如,如果输入看起来像这样,数组大小为 2
push 1
push 1
push 1
push 1
push 1
pop
pop
push 1
然后输出看起来像这样
Stack doubled from 2 to 4.
Stack doubled from 4 to 8.
Stack contains 4 entries.
我将如何实现这一点?谢谢
推送尝试
int dataCount = 0;
const int SIZE = 5;
int capacity = 0;
int myStack[SIZE];
void push(int value)
{
if(SIZE == capacity) {
cout << "Doubling current size " << endl;
capacity = capacity * 2;
int* temp = new int[capacity];
copy_n(myStack, SIZE, temp);
std::swap(myStack, temp);
delete[] temp;
}
myStack[dataCount] = value;
dataCount++;
}
解决方案
您所要求的要求在动态内存中分配堆栈数组,例如:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int count = 0; //stores the indexed number
int capacity = 0; //stores the allocated size
string *myStack = NULL; //stores the data from file
//function declarations/prototypes
void push(const string &aString);
string top();
void pop();
bool isEmpty();
void cleanup();
int main(int argc, char *argv[])
{
string storeFile; //stores the data as a string
string fileName = "file.txt";
ifstream readFile(fileName); //attempts to read the file
while (readFile >> storeFile) {
push(storeFile); //pushes file to stack
}
if (readFile.bad()) {
cerr << "File failed to read " << endl;
}
readFile.close();
while (!isEmpty()) { //while file is not empty return the top stack.
cout << top() << endl;
pop();
}
cleanup();
return 0;
}
void push(const string &value) {
if (count == capacity) {
int newCapacity = (capacity == 0) ? 20 : (capacity * 2);
string *newStack = new string[newCapacity];
for(int i = 0; i < count; ++i) {
newStack[i] = myStack[i];
}
delete[] myStack;
myStack = newStack;
capacity = newCapacity;
}
myStack[count] = value;
++count;
}
string top() {
if (count <= 0) {
return string();
}
return myStack[count-1]; //return top value
}
void pop() {
if (count > 0) {
--count; //decrement count for stack
myStack[count] = "";
}
}
bool isEmpty() {
return (count < 1);
}
void cleanup() {
capacity = count = 0;
delete[] myStack;
myStack = NULL;
}
推荐阅读
- git - 如何将一个分支的更改移植到另一个分支
- python - 我不断收到这个 AttributeError: '_tkinter.tkapp' 对象没有属性 'TK'
- python - PyQGIS在导入csv时更改底图crs
- google-cloud-platform - 如何将项目链接到链接到不同帐户的 GCP 积分?
- java - 如何在spring rest控制器中获取发件人的uri?
- c# - 您好,我想知道这种条件语句是什么意思?if (i == 0) 最低 = 最高 = 输入;
- python - 无法将正确的格式应用于熊猫日期框
- c - 管道和控制台的区别
- android - 什么是 provideLayout()?.let { setContentView(it) }?
- powershell - 为什么Powershell运行的应用程序没有出现在远程桌面上,而是出现在任务管理器中?