Link to home
Start Free TrialLog in
Avatar of rdf
rdf

asked on

push on stack dynamic

HI, this is simple but Im still learning. I have a stack and I need to add an Item to the top. If there is no room, I need to create a new stack, delete the old one, then add the Item to the top of the stack. My problem is in the creation of the new stack. How do I copy the old one into the new one if they both have the same name? If I change one of the names I get error messages, so where do I define it if I do???

//  stack.h
//  specification file

   #ifndef STACK_T
   #define STACK_T

   typedef int ItemType;
   class StackType
   {
      public:
         
         StackType(int Size);
         int Push(const ItemType& Item);
         void print();
      private:
         int Size;
         int Top;
         ItemType* data;
   };    

   #endif

_______________________________________________________________________

//  stack.cpp
//  implementation file


   #include<iostream.h>
   #include"stack.h"
   #include<string.h>
   #include<stdio.h>
   #include<stdlib.h>
   #include<stddef.h>
   #include<ctype.h>

   
   StackType::StackType(int Size)
   {
      Top = -1;
      data = new ItemType [Size];
      this->Size = Size;
   }

   int StackType::Push(const ItemType& Item)
   {
      if(Top == (Size - 1))
         {    
               data = new ItemType [Size];
               if(data==0)
               {
               return 1;
               }
               else
               {
               for(int i = 0; i < Size; i++)
                {
                 data[i] = data[i]; //?how do i do this?
 // ?? how do I copy the old array into the new array. I     //    need  to change the name of one of them, right?  I  //    need to check the stack to see if it is full, and if  //it if it is, I need to create a new stack, copy in the  // old one, then delete the new one and add the item to the  //top of the stack.
                }
             delete[]data;
             Top++;
             data[Top] = Item;

             return 0;
         }          
   }        
}


 
           



ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod


    item *NewPtr = new ItemType [Size];

     if(NewPtr == 0)  // According to the standard new does not
                                // return 0 on an error so this test is not needed.
     {
         return 1;
     }
     else
     {
           for(int i = 0; i < Size; i++)
           {
                NewPtr[i] = data[i]; //?how do i do this?
                delete [] data;
                data = NewPtr;
           }

Avatar of rdf

ASKER

  Thanks nietod. I can't seem to be able to increase the size of the stack if I enter more than the original 'Size 5' integers. If I enter 6 or more integers, it doesn't create the new stack on the heap to accomodate the extra integers. Am I declaring the Size in the wrong place?? If 'Top' == size - 1, it should double the size of the stack, where I have 2*Size. Can't seem to get it.
   If I enter 5 integers, it prints them out along with the size of 5, where I have
cout<<Size. But if I push more than 5, it truncates and only prints the last 5 and the cout<<Size is still 5. The new stack isn't created for some reason. Know it probably sounds stupid to you, but what can I say?? :)
Thanks for any help



//  stack.cpp
//  implementation file

   #include<iostream.h>
   #include"stack.h"
   #include<string.h>
   #include<stdio.h>
   #include<stdlib.h>
   #include<stddef.h>
   #include<ctype.h>
**stack.cpp******************************

   StackType::StackType(int Size)
   {
      Top = -1;
      data = new ItemType [Size];
      this->Size = Size;
   }

   int StackType::Push(const ItemType& Item)
   {
      if(Top == (Size-1))
      {
      ItemType* newPtr = new ItemType [2*Size];
         
         if(newPtr==0)
            {
               return 1;
            }
            else
            {
               for(int i = 0; i < Size; i++)
                 {
                 newPtr[i] = data[i];
                 }

                 delete[]data;
                 data = newPtr;
                 Top++;
                 data[Top] = Item;

             return 0;
          }          
          }
           else
           {
            Top++;
            data[Top] = Item;  

            return 0;
 
}
}

   void StackType::print()
   {
      cout<<endl<<endl;
      for(int j=0;j<Size;j++)
        {
        cout<<data[j]<<endl;
        }
      cout<<endl<<endl<<Size<<endl;         /********this is always 5
  }

******************************************************************
**driver for stack.cpp**

#define STOP_FOR(x) {char nob;cout<<"The value is  "<<x<<"  Any Key\
        to continue  ";cin>> nob;cout<<endl;}



   #include"stack.h"
   #include<iostream.h>
   #include<iomanip.h>
   #include<fstream.h>
   #include<stdio.h>
   #include<stdlib.h>
   #include<stddef.h>
   #include<string.h>
   #include<math.h>
   #include<ctype.h>
   #include<limits.h>


int main(){
       

        cout.setf(ios::fixed, ios::floatfield);
        cout.setf(ios::showpoint);
        cout << setprecision(2);

   int Size = 5;
   ItemType Item = 0;
   
   StackType S(Size);
   cout<<"Enter an integer to add to the stack."<<endl;
   cout<<"-1 to stop: "<<endl;

   while (Item != -1)
   {
   cout<<"enter an integer: "<<endl;
   cin>>Item;
   S.Push(Item);
   }

   S.print();

        return 0;
}
                       



























                       





StackType::StackType(int Size)
 {
            Top = -1;
            data = new ItemType [Size];
            this->Size = Size;
 }

You wouldn't need to use "this" to set size if you specified a different name for the parameter, like

StackType::StackType(int InitialSize)
 {
            Top = -1;
            data = new ItemType [InitialSize];
            Size = InitialSize;
 }

**********************
in Push() if the size of the stack changes you do create an array of the new size and initialize it correctly, but you don't set "size" to the new size.  size stays at the old size.

Also you can clean it up by using logic like

if (arraysize is too small)
{
    create new array
    copy old array to new array
    delete old array
    set pointer to new array
    set array size to new size
}
increment top
store item in array at top position.

Notice how in my above example the item is stored in the array by the last two lines of code and this happens in both the case where the array was large enough or where it needed to be resized.  Try this.  The code will be much shorter, simpler, and easier to debug.

**************
The print() function prints out the entire array, even if only a few items in the array are being used.  usually you would only print the items that are stored on the stack.  Thus of the array can store 10 items, but "top" indicates only 1 is stored, only 1 would be printed.  Usually anyways.

Avatar of rdf

ASKER

Thanks nietod, you provided me with much insight. I appreciate it. :)
Dont know what's going on with my points, seem to have 1200 or so when i should have about 120 or something.
Thanks again