What does implement stack mean?

Does my professor allow me to draw a stack? Does he want me to draw it in action ? I feel stupid, but that’s not how I was ever told! Thank you for your help.

Wow guys, fast. Thank you. The complete question: consider two stacks, each of which has size n (i.e., each can contain a maximum of n elements). If the sum of the number of elements in two stacks is n, then any additional PUSH operation should result in an overflow error. (Note: your implementation should take care that the elements must POP, the opposite of the order in which they are PUSHed).

*** I'm not asking for an answer, I'm just wondering ... what do you think he asks me to do? Because he still hasn’t answered my email, and I need to do this by midnight.

+3
source share
2 answers

"Implement" usually means recording, clean and simple. Your teacher wants you to write code that can do what the assignment says.

Fixed-size stacks ( n) can easily be implemented as an array with the current stack depth, but you have an additional twist to your destination, as you are only stipulated to have elements non both stacks together and not every stack.

( , , ):

# Create the two stacks, each of size sz.
init_stack (sz):
    allocate stack1 as array[1 to sz] of integer
    allocate stack2 as array[1 to sz] of integer
    set stack1sz to 0
    set stack2sz to 0
    set maxsz to sz

 

# Push the value val onto stack stk.
push_stack (stk,val):
    if stk is not equal to 1 or 2:
        return error
    if stack1sz + stack2sz is equal to maxsz:
        return error
    if stk is 1:
        add 1 to stack1sz
        set element stack1sz of stack1 to val
    else:
        add 1 to stack2sz
        set element stack2sz of stack2 to val

 

# Pop a value off stack stk.
pop_statkck (stk):
    if s is not equal to 1 or 2:
        return error
    if stk is 1:
        if stack1sz is 0:
            return error
        set val to element stack1sz of stack1
        subtract 1 from stack1sz
    else:
        if stack2sz is 0:
            return error
        set val to element stack2sz of stack2
        subtract 1 from stack2sz
    return val

stack1, stack2, stack1sz, stack2sz maxsz , (.. ). .

, , , .

+2

, .

, () Stack Abstract Data Type, - , , - , , , , ( ..).

- ? / , ? ?

, , , , (, , ), , , , , , , , , - , .

+1
source

Source: https://habr.com/ru/post/1754165/


All Articles