Requirements: Use the stack to reverse the elements of an array output
Analysis:
1. The elements in the array are in linear arrangement
2. The stack is characterized by advanced back-out
Problem-solving ideas: The elements in the array are stacked sequentially, then out of the stack. You can reverse the sequence of elements of an array.
1. Define the structure body
#define N-struct mystack { int// stack top element int data[n];}; struct mystack selfstack={-1, {0// initial stack is empty
2. Declaring functions
int isEmpty (); // determine if the stack is empty return value:1--> null 0--> non-empty void setempty (); set the stack to null int push (int num); // Stack return value: 1--> stack successfully 0--> stack full int pop (); // stack return value: 1--out of stack success -1--and stack empty
3. Implementing functions
intIsEmpty () {if(Selfstack.top = =-1) { return 1; }Else { return 0; }}voidSetempty () {selfstack.top= -1;}intPushintnum) { if(Selfstack.top = = N1) {//Stack is full return 0; }Else{selfstack.top+=1; Selfstack.data[selfstack.top]=num; return 1; }}intpop () {if(Selfstack.top = =-1) { return-1; }Else{selfstack.top-=1; returnselfstack.data[selfstack.top+1]; }}
Called in the 4.main function
voidMain () {inta[Ten]= {1,2,3,4,5,6,7,8,9,Ten}; for(inti =0; i<Ten; i++) {push (a[i]);//press the elements in the array to stack sequentially } while(!IsEmpty ()) {printf ("%d\n", pop ());//out of the stack} system ("Pause");}
5. Output status
Using Reverse stack elements