简体   繁体   中英

C - most members in a stack implemented with array

I'm trying to write a function that will print out a value of when the stack had most members.

Example: push(1), push(2), push(3), push(4), push(5), push(6)
Maximum members should be 6.

Example 2: push(1), push(2), push(3), push(4), push(5), push(6), pop, pop, push(5), push(6), push(7), push(8), pop;
Maximum members should be 8.

I've been trying to figure out how to do this.
I started by adding a counter everytime push, or pop function is called.

case 2:
            pushCalled++;
            scanf("%d", &val);
            ret_val = push(stack, &top, val);
            if(ret_val==-1) DEBUG("Stog is full.\n");
            break;

case 3:
            popCalled++;
            ret_val = pop(stack, &top);
            if(ret_val==-1) DEBUG("Stog is empty.\n");
            break;

And sending the data over to the function:

case 1:
            TopTopova(top, pushCalled, popCalled);
            break;

In the function itself, my thought process was to check if the pop was not called even once, because that would mean that the maximum would be = pushCalled;

   void TopTopova(int top, int pushOperation, int popOpeartion)
{
    if(popOpeartion == 0)
    {
        max = pushOperation;
    }
}

Now the problem is, I'm not sure how to keep track of the maximum when we also use pop. I hope my problem is clear enough for you guys to understand.

You can define some currentDepth to increment when push is called and decrement when pop. Also define maxDepth (a value of when the stack had most members) and check if currentDepth is greater than max depth.

int maxDepth = 0;
int currentDepth= 0;
switch(action) {
        case 2:
                    currentDepth++;
                    scanf("%d", &val);
                    ret_val = push(stack, &top, val);
                    if(ret_val==-1) DEBUG("Stog is full.\n");
                    break;

        case 3:
                    currentDepth--;
                    ret_val = pop(stack, &top);
                    if(ret_val==-1) DEBUG("Stog is empty.\n");
                    break; 
}

if (currentDepth > maxDepth)
{
    maxDepth = currentDepth;
}

I would keep two variables - int size , to keep the current size of the stack, and int max_size to hold the information you want.

size would increment when pushing and decrements when " poping ". max_size would change only in the end of push procedure, like this:

if (size > max_size)
    max_size = size;

You need to keep a track of the current maximum and do something like this in your push function:

int
push(stack* this, item_t item)
{
    // Push the element
    this->most = max(this->most, this->size);
    // Do things and return
}

The pop operation won't affect it at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM