簡體   English   中英

C++ 如何實現可調整大小的堆棧數組?

[英]C++ how to implement a resizable stack array?

在我早期的實驗室練習之一中,我實現了一個簡單的堆棧程序

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

創建一個具有 -Array 起始指針 - 當前大小 - 堆棧指針的當前位置的類

  • 一種膨脹數組,刪除前一個並復制前一個數據的方法 - 有一個推入堆棧的方法和一個彈出堆棧頂部的方法

當 pop 小於堆棧長度 /2 時,您可以事件處理縮小堆棧

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM