Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 21st, 2004, 10:04 PM
Registered User
 
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default LIFO in Array

Hi i Have questions, is it possible to make a LIFO program without using any pointers? and how could I do the elimination with a command like DELETE but without living any trash behind, for example i have the array
2 3 5 7 4 9
the last one in is 9, but when I use the delete command it erases the 9 but lives a 0 instead
2 3 5 7 4 0

and what I want to do is live that possition blank
2 3 5 7 4

a friend told me that using a for I could move the whole array when i delete the last number, like this:
  2 3 5 7 4

I really need Help with this.


Reply With Quote
  #2 (permalink)  
Old April 22nd, 2004, 01:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, the easy thing to do is maintain the index of the top of the array. Popping an item from your stack is a simple matter of decrementing that number. You don't actually delete anything from the stack, you just say that a different index is the top.

For example, let's look at your example. We start with an empty stack:


Value:
Index: 0 1 2 3 4 5 6 7 8 9
Top: -1

We assume that a negative "Top" means the array is empty. A "Top" of 0 indicates that one item is in the array -- that the "top" of the stack is at index 0 of the array.


Let's push your '2' onto the stack.

Value: 2
Index: 0 1 2 3 4 5 6 7 8 9
Top: 0


Now let's push 3.

Value: 2 3
Index: 0 1 2 3 4 5 6 7 8 9
Top: 1


See how the "Top" refers to the index in the array that represents the top of the stack? Let's insert the rest of your numbers:

Value: 2 3 5 7 4 9
Index: 0 1 2 3 4 5 6 7 8 9
Top: 5

Now we have 6 items in the array. The top of the stack is at index 5. Notice also that the number of items in the array is always (Top + 1). (An empty array has a top of -1, so (-1 + 1) = 0.)


Now we want to remove an item from the array. This is a simple matter of making sure the array isn't already empty, and just decrementing Top:

Value: 2 3 5 7 4 9
Index: 0 1 2 3 4 5 6 7 8 9
Top: 4


Notice that "9" is still in the array, but Top is 4, which identifies the value of "4" as the top of the stack. If we insert a new number, say 1, into the array, it will overwrite the 9:

Value: 2 3 5 7 4 1
Index: 0 1 2 3 4 5 6 7 8 9
Top: 5


Here's a simple class that illustrates this concept. I do not guarantee correctness -- I'm just typing into the text area. It's your responsibility to understand and debug the code:

#define INTSTACK_SIZE 10

class IntStack
{
public:
    IntStack() { /* empty */ }
    ~IntStack() { /* empty */ }

    int size()
    {
        return top_ + 1;
    }

    bool push(int x)
    {
        bool ret = false;

        // only insert item if there's room:
        if (size() < INTSTACK_SIZE)
        {
            arr_[++top_] = x;
            ret = true;
        }

        return ret;
    }

    bool pop(int * top)
    {
        bool ret = false;

        // Only can pop from a nonempty stack:
        if (size() > 0)
        {
            *top = arr_[top_--];
            ret = true;
        }
    }

    bool top(int * top)
    {
        bool ret = false;

        // Only can get top from a nonempty stack:
        if (size() > 0)
        {
            *top = arr_[top_];
            ret = true;
        }
    }


private:
    int[INTSTACK_SIZE] arr_;
    int top_;
};


The accessor functions push(), pop(), and top() return true if they succeed. pop() and top() return the actual top value via the int pointer passed into the function.

Another way of writing this class is to return the int values as expected and throw exceptions if something goes wrong. Exceptions include Stack Overflow (pushing onto a full stack) and Stack Underflow (popping or topping an empty stack).


Hope this helps!


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old April 22nd, 2004, 01:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I forgot one very important thing: The stack constructor SHOULD initialize top_ to -1.


class IntStack
{
public:
   IntStack() : top_(-1) { /* empty */ }

   ...
};



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old April 22nd, 2004, 06:15 PM
Registered User
 
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks nikolai, i think i got it now

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
FIFO 2 LIFO mafuka C++ Programming 6 May 13th, 2004 06:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.