1

I am a student and my teacher gave me this "homework". I have to build a dynamic stack, the trivial part is that I mustn't use a list structure (e.g. linked lists). What I thought was that an array implementation was the bets thing, but during the development I stopped at the point I had to increment the array size. I can't figure out how to increment the size without losing data. Can someone help me ?

man_o_war
  • 17
  • 1
  • 5
  • 1
    are you allowed to use `std::vector` ? If not do it anyhow :P – 463035818_is_not_an_ai Aug 27 '18 at 14:13
  • 6
    Make a bigger array, copy the old one into the new one, delete the old one, set the new one as the old one. – NathanOliver Aug 27 '18 at 14:14
  • 1
    Is your task to implement a class similar to std::stack? Are you allowed to use other container types? Maybe if you posted the code you have now, it'll be easier to help – Support Ukraine Aug 27 '18 at 14:19
  • I think you may have misunderstood the meaning of "trivial". – molbdnilo Aug 27 '18 at 14:21
  • Arrays cannot be resized in c++. Once they are created, their size cannot change for any reason. – François Andrieux Aug 27 '18 at 14:21
  • I think may be by dynamic stack you meant to create an app which acts the same way..? It may be yours to do and learn, but you’ll get good “pointers” here.. – boateng Aug 27 '18 at 14:24
  • This is an exercise in dynamic memory allocation. The solution is to change perspective from "make it larger" to "make a new one just like it, but larger". – molbdnilo Aug 27 '18 at 14:24
  • 1
    One option is to create another array of bigger size and copy the contents over, delete the old one. If you have already had a class on STL(Standard Template Libraries) probably your professor is looking at you understanding and using it. Share your code here and there are many willing to help you out. – Shrikanth N Aug 27 '18 at 14:31
  • thank you all for those comments, they helped me a lot. I love Programming communities <3 – man_o_war Aug 27 '18 at 15:06

2 Answers2

0

One thing you can do is creating a new array with the new size and then copying the old one in it. Something like this is what you're looking for?

const int new_size = old_size + 1;
int new_array[new_size];
std::copy(old_array, old_array+old_size, new_array);
Ragrag
  • 1
  • 1
  • Please don't grow the array by one. That is very inefficient. Memory is cheap so you should double it to keep the number of times the array needs to grow down. – NathanOliver Aug 27 '18 at 14:36
  • For performance reasons you may want to increase the size by more than 1 if you expect to increase again. – drescherjm Aug 27 '18 at 14:37
  • thank you all for the comments. I think it is what i was looking for, obviously i would double it since i haven't problems with memory leaks or whatever. Thank you again! – man_o_war Aug 27 '18 at 15:05
  • You may be interested in looking at std::vector for ideas. What I mean is `std::vector` has `capacity` and `size`. You may want to have your vector use these concepts. https://stackoverflow.com/questions/6296945/size-vs-capacity-of-a-vector – drescherjm Aug 27 '18 at 16:18
  • @drescherjm sorry, i can't use vectors – man_o_war Aug 28 '18 at 13:34
  • I did not suggest to use a vector. I suggested to use the `capacity` and `size` concepts from `std::vector` when you design your class. – drescherjm Aug 28 '18 at 13:43
0

I assume that your not allowed to use std::vector and that you must use dynamic memory allocation.

In that case when you run out of space in the currently allocated array, you'll have to do something like:

  • create an array larger than the current array size

  • copy current array to the new array

  • make the new array the current

  • delete the old array

Here is a simple and incomplete class to illustrate the idea.

#include <iostream>
#include <algorithm>
using namespace std;

class myStack
{
    size_t capacity {0};
    size_t size {0};
    int *data {nullptr};
public: 
    void push(int n)
    {
        if (size == capacity)
        {
            cout << "Increase capacity by 5 elements" << endl;
            capacity += 5;
            int* tmp = new int[capacity];
            copy_n(data, size, tmp);
            swap(data, tmp);
            delete[] tmp;
        }

        data[size] = n;
        ++size;
    }

    void print_all()
    {
        cout << "capacity=" << capacity << endl;
        for (size_t i = 0; i < size; ++i)
            cout << data[i] << " ";
        cout << endl;
    }
};

int main(void) {
    myStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    s.print_all();
    s.push(6);
    s.print_all();
    return 0;
}

Output:

Increase capacity by 5 elements
capacity=5
1 2 3 4 5 
Increase capacity by 5 elements
capacity=10
1 2 3 4 5 6 

I'll leave the rest to you for practice (e.g. destructor to delete allocated memory, top to read data, pop to remove elements etc.) Probably you would also like to turn it into a template so that you can handle other data types than int with the same code.

Notice: This simple class just grows the capacity by 5 when necessary. A more common approach is to double the capacity when an increase is needed. I'll leave that to you as well so that you can practice.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • yes, the other methods are just already done, this is EXACTLY, and when i say that i mean literally what i was looking for. The one thing that is not clear is the ptr *data – man_o_war Aug 27 '18 at 15:07
  • @man_o_war "The one thing that is not clear is the ptr *data" well, it's a pointer to the location where the real stack data is stored – Support Ukraine Aug 27 '18 at 15:15
  • if i turn the class into a template, the stack won't allow me to access usign the [ ] operator even though i redefined it making it return a T& value, have you got any suggestion? Maybe I can post another question and post the code i have right now – man_o_war Aug 28 '18 at 14:40
  • @man_o_war Hard to say without seeing your exact code. A new question sounds ok to me – Support Ukraine Aug 28 '18 at 15:11