StackStructure cpp Defines the entry point for the console application

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
// StackStructure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
class stack
{
int size;
int *piArr; //point to massive
int count; // 100 - max
//copy-constructor
public:
stack() //constructor
{
piArr = new int[size=100];
count = 0;
}
stack(const stack &s);
void Push(int a)
{
if( count < size )
piArr[count++] = a;
else; //resizing
/*{
int * temp = new int[size*2];
for (int i = 0; i < size; i++)
temp[i] = piArr[i];
delete [] piArr;
piArr = temp;
}
piArr[size/2+1] = a;*/
}
int Pop()
{
if( count >0)
return piArr[--count];
}
void printStack()
{
for(int i=0; i < size; i++)
cout<<piArr[i]<<"||"<<" ";
}
int Chack()
{
return piArr[0];
}
~stack();
};
stack::stack (const stack &s)
{
piArr = new[size = s.size];
memcpy(piArr, s.piArr, size);
}
stack::~stack()
{
delete[] piArr;
}
stack&stack::operator+=(int n)
{
return piArr[0 + n];
}
stack&stack::operator-=(int n)
{
return piArr[size - n];
}
stack&stack::operator++()
{
return piArr[0 + 1];
}
stack&stack::operator--()
{
return piArr[size - 1];
}
std::ostream&stack::operator<<(const std::ostrem&o, stack&st)
{
o << piArr[i]<<"||"<<" ";
return o;
}
int _tmain(int argc, _TCHAR* argv[])
{ stack
s;
for(int i = 0; i < 100; i++)
{
s.Push(i);
}
s.printStack();
cout<<"Pop:"<<endl;
s.Pop();
cout<<endl;
s.printStack();
cout<<endl;
cout<<"Top element of stack:"<<endl;
cout<<s.Chack();
s+=5;
s-=4;
s++;
s--;
cout << s;
cout<<endl;
return 0;
}