Description: For a non-empty stack, design an algorithm to reverse the elements in the stack.
First, the stack implementation code we designed is:
/** Problem description: the basic code for stack implementation. * @ Author: xiaoq-ohmygirl * @ time: 2012-07-03 **/# include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <math. h> // initial maximum stack capacity # define stack_max_size 100 // stack Capacity Increment # define increament 10 // status function # define error 0 # define OK 1 # define soverflow-1 # define Yes 1 # define no 0 typedef int status, selemtype, elemtype; typedef struct {selemtype * base; selemtype * Top; int stacksize; // currently allocated space, not the number of elements in the stack.} Sqstack; Status initstack (sqstack & S) {// initialize the stack space. The stack is empty. base = (selemtype *) malloc (stack_max_size * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base; S. stacksize = stack_max_size; Return OK;} // If the stack is not empty, use e to output the top element of the stack and Return OK. Otherwise, an error is returned. Status gettop (sqstack S, selemtype & E) {If (S. top = S. base) {return error;} e = * (S. top-1); Return OK;}/** push (S, e) medium pressure element e to stack S. Consider the following situations: * 1. the stack is full and space needs to be increased. If you cannot add more space, exit. * 2. the stack is not full. Press the element first, and then modify the stack top pointer. **/Status push (sqstack & S, selemtype e) {If (S. top-s. base)> = S. stacksize) {S. base = (elemtype *) realloc (S. base, (S. stacksize + increament) * sizeof (elemtype); If (! S. base) {exit (soverflow);} s. top = S. base + S. stacksize; S. stacksize + = increament;} // press the element first, and then modify the pointer. * S. Top = E; S. Top ++; Return OK;}/** pops up the top element of the stack. Note: * 1. the stack is not empty. Modify the pointer, then pop up the element and send it to e. * 2. If the stack is empty, an error is returned. */Status POP (sqstack & S, elemtype & E) {If (S. Top = S. Base) {Return Error ;}// first modify the pointer and then the element pops up. S. top --; E = * s. top; Return OK;} status isempty (sqstack s) {If (S. top = S. base) {return yes;} return no ;}
With this implementation, the stack upside down operation has a foundation.
Idea 1 is the simplest idea, based on the stack's first-in-first-out nature. Another auxiliary stack (stack 2) can be designed. Each stack 1pops one element, and the corresponding elements are pushed in stack 2.
The algorithm is as follows:
Sqstack reversestack (sqstack s) {sqstack help; initstack (HELP); selemtype node; while (! Isempty (s) {Pop (S, node); push (HELP, node);} return help;} Main () {sqstack s; sqstack TMP; initstack (s ); initstack (TMP); int rands, rest; printf ("inbound Stack:"); For (INT I = 0; I <100; I ++) {rands = rand () % 1000; push (S, rands); printf ("% d \ n", rands);} TMP = reversestack (s); printf ("\ n reverse Stack: "); While (! Isempty (TMP) {Pop (TMP, rest); printf ("% d \ n", rest);} system ("pause"); Return 0 ;}
2. Idea 2: assume that the stack elements are 1, 2, 3, 4, and 5. 1 indicates the bottom of the stack, and 5 indicates the top of the stack. The process of reversing a stack can be described as: Reversing 1, 2, 3, 4. Then adding element 5 to the bottom of the stack.
Perform this process in sequence. Until the stack is empty.
How can we add elements to the bottom of the stack?
After careful analysis, it is found that another recursive process.
That is, if (Stack is empty ){
Then push directly. At this moment, the elements are at the bottom of the stack.
} Else {
Otherwise. The top element of the stack is displayed. Place the elements to be pushed to the bottom of the stack. Then, push the elements at the top of the original stack into the stack.
}
The complete code for this idea is as follows:
Void addtobottom (sqstack & S, selemtype node) {If (isempty (s) {push (S, node) ;}else {selemtype TMP; POP (S, TMP ); addtobottom (S, node); push (S, TMP) ;}} void reverstack (sqstack & S) {selemtype node; If (! Isempty (s) {Pop (S, node); reverstack (s); addtobottom (S, node) ;}} main () {sqstack s; initstack (s ); int rands, rest; printf ("inbound Stack:"); For (INT I = 0; I <100; I ++) {rands = rand () % 1000; push (S, rands); printf ("% d \ n", rands);} reverstack (s); printf ("\ n reverse Stack:"); While (! Isempty (s) {Pop (S, rest); printf ("% d \ n", rest);} system ("pause"); Return 0 ;}