Pushing Template Type to Stacks

I am currently trying to figure out how to push data correctly into a BiStack. What happens in the main is that a BiStack of some type (For example BiStack<long> b{1, 2, 3, 4, 5, 6};) is added, then b.push2(200); and b.push1(100); are called. This is suppose to push 100 and 200 to the end of the array 1, 2, 3, 4, 5, 6 so that I get 1, 2, 3, 4, 5, 6, 100, 200. The following I have done causes 1, 2, 3, 4, 5, 6, 200, 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
template<typename T>
void BiStack<T>::push1(T element) {

	if (size_ == max_) {
		throw overflow_error("stack past max");
	}

	if (capacity_ == size_) {
		grow_and_copy();
	}
	
		size_ += 1;
		T* nstack = new T[size_];
		for(int i = 0; i < size_; i++) {
			nstack[i] = data_[i];
		}
		delete [] data_;
		nstack[size_ - 1] = element;
		data_ = nstack;
}

template<typename T>
void BiStack<T>::push2(T element) {
			
	if (size_ == max_) {
		throw overflow_error("stack past max");
	}

	if (capacity_ == size_) {
		grow_and_copy();
	}

		size_ += 1;
		T* nstack = new T[size_];
		for(int i = 0; i < size_; i++) {
			nstack[i] = data_[i];
		}
		delete [] data_;
		nstack[size_ - 1] = element;
		data_ = nstack;
}


My biggest issue is that sometimes the list being pushed in contains strings, so I can't do something like data_[++top1_] = element;

Any ideas or the point in the right direction would be greatly appreciated, thanks!
¿what's the difference between push1() and push2()?
¿what does grow_and_copy() do? ¿how is it diferent to the 8 lines you've got after?
¿why are you copying the whole array each time?

> the list being pushed in contains strings, so I can't do something like data_[++top1_] = element;
¿why not?
Last edited on
Topic archived. No new replies allowed.