Concatenar dos pilas (una pila sobre otra)

I understand how to do this with a loop S1 and S2 (in pseudocode)

Loop (NOT emptyStack(S2))
     pop(S2, dataptr)
     push (temp, dataptr)
end loop 
Loop (NOT emptyStack(temp))
     pop (temp dataptr)
     push(S1, dataptr)
end loop 

How would you do this without a loop (big O constant)? With queues it is easy as all you have to do is move the rear pointer of q1 to the front of q2 and link them.

C like pseudocode would be great! Thank you very much.

preguntado el 12 de febrero de 14 a las 06:02

¿Pseudocódigo? concat_stacks(&s1, &s2) -

@KerrekSB what do you mean? -

1 Respuestas

Implement the stacks internally as listas enlazadas and hang on to pointers to the top and bottom elements of the stack. Then the operation of concatenating the stacks is just the same as splicing two linked lists together, which is O(1).

Respondido 12 Feb 14, 06:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.