首页 > 解决方案 > 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++;
}

标签: c++arraysstack

解决方案


您所要求的要求在动态内存中分配堆栈数组,例如:

#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;
}

推荐阅读