Programing.BlogSky.com
X
تبلیغات
رایتل

این بر نامه با استفاده از یک پشته(stack) عمل حذف درج و... را انجام می دهد

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#  define SIZE 20
class stack
{
   int mytop;
   int items[SIZE];
   public:
    stack();//tabe sazande
    int empty();//test khali naboodan poshte(0 or 1)
    void push(int x);//ezafe kardan adad
    void pop(int &); //hazf az poshte
    void top(int &);
    void display();//namayesh poshte
};
//************************ tarif tavabee *************************

//-------------------------------
stack::stack()
{
 mytop= (-1);
}
//-------------------------------
int stack::empty()
{
 return(mytop==(-1));
}
//-------------------------------
void stack::push(int x)
{
 if(mytop==(SIZE-1))
 {
  cout<<"\n\n\tSory,Stack is Full";
  cout<<"\n\tPress An Key To Contniue...";
  getch();
  exit(0);
 }
 else
  items[++mytop]=x;
}
//----------------------------------
void stack::pop(int &x)
{
 if(empty())
 {
  cout<<"\n\n\tSory,Stack Is Empty...";
  cout<<"\n\tPress Any key To Contenue...";
  getch();
  exit(0);
 }
 else
  x=items[mytop--];
}
//--------------------------------------
void stack::top(int &x)
{
 if(empty())
 {
  cout<<"\n\n\tSory,Stack Is Empty...";
  cout<<"\n\tPress Any key To Contenue...";
  getch();
  exit(0);
 }
 else
  x=items[mytop];
}
//-----------------------------------
void stack::display()
{
 if(mytop==-1)
  cout<<"\n\n\n\tStack Is Emptyyyyyyyyyyyyy!!";
 else
  for(int i=mytop;i>=0;i--)
   cout<<"\n\n    "<<items[i];
 getch();
}
//````````````````````````````````````````````````````````````
int menue();
//******************
//                   Main
//                         **********************
void main()
{
 int x;
 stack s;
 while(1)
 {
  clrscr();
  switch(menue())
  {
  case 1:

   s.display();
   break;

  case 2:
   cout <<"Enter a number to push in stack: ";
   cin>>x;
   s.push(x);
   break;

  case 3:
   s.pop(x);
   cout<<"\n\tpopd Voluvs is: "<<x;
   getch();
   break;

  case 4:
   s.top(x);
   cout<<"\n\tretrieved value is: "<<x;
   getch();
   break;

  case 5:
   exit(1);
  }//end case
 }//end wile
}//end

//-------------------------------------------
int menue()
{
 int x;
 textcolor(7);
 textbackground(1);
                 clrscr();
 
 cout<<"\n\n\n\t1. print stack";
 cout<<"\n\n\t2. Add to Stack";
 cout<<"\n\n\t3. pop from stack";
 cout<<"\n\n\t4. show top of stack";
 cout<<"\n\n\t5. Exit";
 cout<<"\n\n\n\n\tEnter your  select(1 - 5):\a ";
 cin>>x;
 return x;
}