Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this is updated program #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 44 additions & 67 deletions cpp/stackcpp.cpp
Original file line number Diff line number Diff line change
@@ -1,76 +1,53 @@
//stackArray.cpp
//a program that shows Arrays as class data members


#include<iostream>
#include<cstdio>
using namespace std;
class Stack{
int top,totalSize;
int *ptr;
public:
Stack(int totalSize){
this->totalSize=totalSize;
this->top=-1;
ptr=new int[totalSize];
}

void push(int n){
if(!isFull()){
top++;
ptr[top]=n;
}
else{
cout<<"Stack overFlow"<<endl;
}
}

int pop(void){
if(!isEmpty()){
int temp=ptr[top];
top--;
return temp;
}
else{
cout<<"Stack underFlow"<<endl;
}
}
bool isFull(){
if(top==totalSize-1){
return true;
}
else{
return false;
}
}

bool isEmpty(){
if(top==-1)
return true;
else
return false;
}
//class definition

void stackDisplay(){
for (int i = 0; i<=top; i++)
{
cout<<ptr[i]<<endl;
}
}
class Stack
{
private:
static const int MAX= 10; //define 10 elements in the Stack
int st[MAX]; //stack st an array of integers
int top; //which is the top element of teh Stack
public:
Stack()
{
top = -1; //constructor
}
void push(int var) //put a number on the top of the stack
{
st[++top] = var;
}
int pop() //take a number off the top of the Stack
{
return st[top--];
}
};

int topVal(){
return ptr[top];
}
//main code

int bottomVal(){
return ptr[0];
}
};
int main(){
int main()
{
Stack s1;

Stack stack1(2);
stack1.pop();
stack1.push(5);
stack1.push(6);
stack1.push(62);
stack1.stackDisplay();
cout<<stack1.topVal()<<endl<<stack1.bottomVal()<<endl;
s1.push(11);
s1.push(22);
cout << "1: " << s1.pop() << endl; //22
cout << "2: " << s1.pop() << endl; //11
s1.push(33);
s1.push(44);
s1.push(55);
s1.push(66);
cout << "3: " << s1.pop() << endl; //66
cout << "4: " << s1.pop() << endl; //55
cout << "5: " << s1.pop() << endl; //44
cout << "6: " << s1.pop() << endl; //33
return 0;
}

return 0;
}
}