الأحد، 15 ديسمبر 2013
12:44 م

Stack

// ConsoleApplication19.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<assert.h>
#include <conio.h>
using namespace std;
typedef int elmtype;
class stack
{
private:
int top;
int size;
elmtype *elems;
public:
stack()
{
top = 0;
size = 10;
elems = new elmtype[size];
assert(elems);
}
~stack(){ delete[]elems; }
bool isempty(){ return top == 0; }
bool isfull() { return size == top; }

void push(elmtype &el)
{
assert(!isfull());
elems[top++] = el;
}
elmtype pop()
{
assert(!isempty());
return elems[--top];

}
elmtype pick()
{
return elems[top];
}
};

void main()
{
stack s;
int x;
cout << "enter elements:-\n";
for (int i = 0; i <10; i++)
{
cin >> x;
s.push(x);
}
cout << "\n stack poped elements:";
for (int i = 0; i < 10; i++)
{
elmtype x = s.pop();
cout << x << " ";

}

elmtype y = s.pick();
cout << "\n top is : " << y << endl;
_getch();
}

0 التعليقات:

إرسال تعليق