如何在C ++中构建堆栈 [英] How to build stack in C++
本文介绍了如何在C ++中构建堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
我需要一个实现堆栈(push,pop,print元素)的C ++代码..
解决方案
如果您正在寻找一个简单的C ++堆栈程序,请准备一个看这个:
#include >< iostream > 使用 命名空间 std; #define MAX 10 类堆栈 { 私有: int arr [MAX]; int 顶部; 公共: 堆() { 顶部=- 1 ; } 无效推送( int 项) { 如果(顶部== MAX- 1 ) { cout<< endl<< " ; 返回; } 顶++; arr [top] =项目; } int Pop() { 如果(顶部==- 1 ) { cout<< endl<< " ; 返回 NULL; } int data = arr [top]; 最佳 - ; 返回数据; } }; int main() { 堆栈s; s.Push( 1 ); s.Push( 2 ); s.Push( 3 ); int i = s.Pop(); cout<< endl<< " << i<< endl; i = s.Pop(); cout<< endl<< " << i<< endl; 返回 0 ; }
提供的STL
有什么问题一个 [ ^ ]?
Hello friends,
I need a C++ code that implement a stack ( push , pop , print elements )..
解决方案
If you are looking for a simple C++ stack program, then have a look at this:
#include <iostream> using namespace std; #define MAX 10 class Stack { private: int arr[MAX]; int top; public: Stack() { top = -1; } void Push(int item) { if(top == MAX-1) { cout<<endl<< "Stack is full"; return; } top++; arr[top] = item; } int Pop() { if(top == -1) { cout<<endl<< "Stack is empty"; return NULL; } int data = arr[top]; top--; return data; } }; int main() { Stack s; s.Push(1); s.Push(2); s.Push(3); int i = s.Pop(); cout<<endl<< "item= popped = "<<i<<endl; i = s.Pop(); cout<<endl<< "Item popped = "<<i<<endl; return 0; }
What''s wrong with the STL
provided one[^]?
这篇关于如何在C ++中构建堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文