help please in stack

im trying to add more memory space to this stack code using this:
temp = new(nothrow) StackElement[myCapacity + 10];
for (int i = 0; i < myCapacity; i++)
temp[i] = myArray[i];
delete [] myArray;
myArray = temp;

and this is my code:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Stack::Stack(int numElements)
{
   assert (numElements > 0);  // check precondition
   myCapacity = numElements;  // set stack capacity
                              // allocate array of this capacity
   myArray = new(nothrow) StackElement[myCapacity];
   if (myArray != 0)          // memory available
      myTop = -1;
   else
   {
      cerr << "Inadequate memory to allocate stack \n"
              " -- terminating execution\n";
      exit(1);
   }                          // or assert(myArray != 0);
}

//--- Definition of Stack copy constructor
Stack::Stack(const Stack & original)
: myCapacity(original.myCapacity), myTop(original.myTop)
{
   //--- Get new array for copy
   myArray = new(nothrow) StackElement[myCapacity];  
   if (myArray != 0)                 // check if memory available
      // copy original's array member into this new array
      for (int pos = 0; pos <= myTop; pos++) 
         myArray[pos] = original.myArray[pos];
   else
   {
      cerr << "*Inadequate memory to allocate stack ***\n";
      exit(1);
   }
}

//--- Definition of Stack destructor
Stack::~Stack()
{ 
   delete [] myArray;
}

//--- Definition of assignment operator
const Stack & Stack::operator=(const Stack & rightHandSide)
{
   if (this != &rightHandSide)                // check that not st = st
   {
      //-- Allocate a new array if necessary
      if (myCapacity != rightHandSide.myCapacity) 
      {
         delete[] myArray;                    // destroy previous array

         myCapacity = rightHandSide.myCapacity;  // copy myCapacity 
         myArray = new StackElement[myCapacity]; 
         if (myArray == 0)                    // check if memory available
         {
            cerr << "*** Inadequate memory ***\n";
					
            exit(1);
         }
        }
		
      myTop = rightHandSide.myTop;            // copy myTop member
      for (int pos = 0; pos <= myTop; pos++)  // copy stack elements
         myArray[pos] = rightHandSide.myArray[pos];

   }
   return *this;
}


//--- Definition of empty()
bool Stack::empty() const
{ 
   return (myTop == -1); 
}

//--- Definition of push()
void Stack::push(const StackElement & value)
{
   if (myTop < myCapacity - 1) //Preserve stack invariant
   { 
      ++myTop;
      myArray[myTop] = value;
   }
   else
   {
      cerr << "*** Stack full -- can't add new value ***\n"
              "Must increase the stack's capacity.\n";
      exit(1);
   }
}

//--- Definition of display()
void Stack::display(ostream & out) const
{
   for (int i = myTop; i >= 0; i--) 
      out << myArray[i] << endl;
}

//--- Definition of top()
StackElement Stack::top() const
{
   if ( !empty() ) 
      return (myArray[myTop]);
   else
   {
      cerr << "*** Stack is empty -- returning garbage value ***\n";
      StackElement garbage;
      return garbage;
   }
}

//--- Definition of pop()
void Stack::pop()
{
   if ( !empty() )
      myTop--;
   else
      cerr << "*** Stack is empty -- can't remove a value ***\n";
}



void print(Stack st)
{ 
	st.display(cout);
}

int main()
{
   int cap;
   cout << "Enter stack capacity: ";
   cin >> cap;

   Stack s(cap);
   cout << "Stack created.  Empty? " << boolalpha << s.empty() << endl;

   cout << "How many elements to add to the stack? ";
   int numItems;
   cin >> numItems;
   for (int i = 1; i <= numItems; i++) 
      s.push(100*i);
   cout << "Stack empty? " << s.empty() << endl;

   cout << "Contents of stack s (via  print):\n";
   print(s); cout << endl;
   cout << "Check that the stack wasn't modified by print:\n";
   s.display(cout); cout << endl;

   Stack t, u;
   t = u = s;
   cout << "Contents of stacks t and u after t = u = s (via  print):\n";
   cout << "u:\n"; print(u); cout << endl;
   cout << "t:\n"; print(t); cout << endl;

   cout << "Top value in t: " << t.top() << endl;

   while (!t.empty())
   {
     cout << "Popping t:  " << t.top() << endl;
     t.pop();
   }
   cout << "Stack t empty? " << t.empty() << endl;
   cout << "\nNow try to retrieve top value from t." << endl;
   cout << "Top value in t: " << t.top() << endl;
   cout << "\nTrying to pop t: " << endl;
   t.pop();
}




should i use template???????????
First of all get rid of exit(1) calls, it's not a good idea. Use bool/int return value (or argument in constructor) to indicate success or failure or throw an exception.

> should i use template?
If you want to use Stack class with types other that int then use templates. You can have simpler implementation of Stack class by deriving from (protected/private derivation) or aggregating std::vector/std::list.

Good luck!
ok i will delete exit and use bool. about this code should i implement another function in order to use this code.

temp = new(nothrow) StackElement[myCapacity + 10];
for (int i = 0; i < myCapacity; i++)
temp[i] = myArray[i];
delete [] myArray;
myArray = temp;
Yes, you need another member function.
But again, if you are not writing this code for learning purposes, then better use standard containers. They can do it all for you.
tanks.... that's for learning purposes!!!! im just trying to swap to another memory when one is full
Topic archived. No new replies allowed.