使用VS.net2008编写C++程序---堆栈操作

  1. #include "stdio.h"
  2. #include <iostream>
  3. using namespace std; 
  4. #define MAX 10 // MAXIMUM STACK CONTENT 
  5. class stack 
  6. private
  7.     int arr[MAX]; // Contains all the Data 
  8.     int top; //Contains location of Topmost Data pushed onto Stack 
  9. public
  10.     stack() //Constructor 
  11.     { 
  12.         top=-1; //Sets the Top Location to -1 indicating an empty stack 
  13.     } 
  14.     void push(int a) // Push ie. Add Value Function 
  15.     { 
  16.         top++; // increment to by 1 
  17.         if(top==-1)
  18.         { 
  19.             arr[top]=a; //If Stack is Vacant store Value in Array 
  20.         } 
  21.         else 
  22.         { 
  23.             cout<<"STACK FULL!!";
  24.             top--; 
  25.         } 
  26.     } 
  27.     int pop() // Delete Item. Returns the deleted item 
  28.     { 
  29.         if(top==-1) 
  30.         { 
  31.             cout<<"STACK IS EMPTY!!!"
  32.             return NULL; 
  33.         } 
  34.         else 
  35.         { 
  36.             int data=arr[top]; //Set Topmost Value in data 
  37.             arr[top]=NULL; //Set Original Location to NULL 
  38.             top--; // Decrement top by 1 
  39.             return data; // Return deleted item 
  40.         } 
  41.     } 
  42. }; 
  43. int main() 
  44.     stack a; 
  45.     a.push(5); 
  46.     cout<<"5 is Pushed/n"
  47.     a.push(10); 
  48.     cout<<"10 is Pushed/n"
  49.     a.push(2); 
  50.     cout<<"2 is Pushed/n/n"
  51.     return 0; 
  52. }

这是一段关于堆栈操作的程序,经VS.net2008调试可以运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值