General Stack Algorithm