Using Reverse stack elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.