C + + implementation of two elements in the stack and the order of the stack

Source: Internet
Author: User

1, in the data structure of the book is mostly pseudo-code, did not give a complete program, in this small series to everyone a complete simple into the stack operation.

2, the realization of the function is : to observe the two numbers in the stack and out of the stack sequence.

3. Program and Code:

#include <iostream>

#include <stdlib.h>

using namespace Std;

struct stack{

int* Base;

int top;

int stack_size;

};

int Inistack (Stack &s,int size) {

S.base= (int*) malloc (size *sizeof (int));

if (S.base==null) return 0;

S.top=0;

S.stack_size=size;

return 1;

}

int Push (Stack &s,int e) {

S.base[s.top]=e;

s.top++;

return 1;

}

int Pop (Stack &s) {

if (s.base[s.top]==0) return 0;

s.top--;

return s.base[s.top];

}

int exchange (int x,int y) {

Stack s;

Inistack (s,3);

Push (S,X);

Push (S,y);

X=pop (s);

Y=pop (s);

cout<< "x=" <<x<< "y=" <<y<<endl;

return 1;

}

int main () {

int x, y;

cout<< "Please enter the value of x:" <<endl;

cin>>x;

cout<< "Please enter the value of y:" <<endl;

cin>>y;

Exchange (x, y);

return 0;

}

Operation Result:

Please enter the value of x:

45

Please enter a value for y:

56

X=56y=45

Please press any key to continue ...


This article is from the "jinleijie_123" blog, make sure to keep this source http://3998540.blog.51cto.com/3988540/1696534

C + + implementation of two elements in the stack and the order of the stack

Related Article

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.