Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

push on stack dynamic

Posted on 1999-06-25
5
Medium Priority
?
279 Views
Last Modified: 2010-04-16
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;
         }          
   }        
}


 
           



0
Comment
Question by:rdf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 80 total points
ID: 1198500
When you create the new array, you need to store a pointer to the array in a local variable, not in the "data" varaible  Then you will have access to both the new arrray and the old array.  When you are done with the old array, you can delete it and then store the pointer to the new array in data.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198501

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

0
 

Author Comment

by:rdf
ID: 1198502
  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;
}
                       



























                       





0
 
LVL 22

Expert Comment

by:nietod
ID: 1198503
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.

0
 

Author Comment

by:rdf
ID: 1198504
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question