[英]C++ how to implement a resizable stack array?
In one of my earlier lab exercises I implemented a simple stack program在我早期的实验室练习之一中,我实现了一个简单的堆栈程序
#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;
}
I am now required to revisit this program, however, this time modify it so that If you attempt to push data onto a full stack you should create a new array of twice the current size, copy the data from the old stack to the new stack and then delete the old stack without using STL or classes.我现在需要重新访问这个程序,但是,这次修改它,以便如果您尝试将数据推送到完整堆栈上,您应该创建一个当前大小两倍的新数组,将数据从旧堆栈复制到新堆栈然后在不使用 STL 或类的情况下删除旧堆栈。 We are allowed to use dynamic memory
我们可以使用动态内存
So if the input, for example, looked something like this with an array size of 2因此,例如,如果输入看起来像这样,数组大小为 2
push 1
push 1
push 1
push 1
push 1
pop
pop
push 1
Then the output would look like this然后输出看起来像这样
Stack doubled from 2 to 4.
Stack doubled from 4 to 8.
Stack contains 4 entries.
How would I implement this?我将如何实现这一点? Thankyou
谢谢
Push attempt推送尝试
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++;
}
What you are asking for requires the stack array to be allocated in dynamic memory, eg:您所要求的需要在动态内存中分配堆栈数组,例如:
#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;
}
Create a class That has -Array start pointer -current size -the current position of the stack pointer创建一个具有 -Array 起始指针 - 当前大小 - 堆栈指针的当前位置的类
You can event handle shrinking the stack when pop is less than length of the stack /2当 pop 小于堆栈长度 /2 时,您可以事件处理缩小堆栈
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.