-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1139800
commit cd914d4
Showing
1 changed file
with
57 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
#include <iostream> | ||
#define SIZE 30 | ||
using namespace std; | ||
//Stack Class | ||
class Stack { | ||
#include <iostream> | ||
|
||
// Defines the size of Array | ||
#define ARRAY_SIZE 10 | ||
|
||
// Class Stack implemented using C array | ||
class Stack | ||
{ | ||
private: | ||
int arr[SIZE], top; | ||
int array[ARRAY_SIZE], top; | ||
public: | ||
Stack() { | ||
Stack() | ||
{ | ||
top = -1; | ||
} | ||
// push Method | ||
void push(int x) { | ||
if(is_full()) | ||
cout << "Stack overflow." << endl; | ||
else { | ||
top++; arr[top] = x; | ||
// Push a value on top of Stack(Array) | ||
void push(int value) | ||
{ | ||
// Checking if Stack is full | ||
if(full()) | ||
{ | ||
std::cout << "Stack is Full." << '\n'; | ||
} | ||
else | ||
{ | ||
top++; | ||
array[top] = value; | ||
} | ||
} | ||
// Return true if stack is full else false | ||
bool full() | ||
{ | ||
return (top >= ARRAY_SIZE); | ||
} | ||
//is_full Method | ||
bool is_full() | ||
return (top >= SIZE); | ||
// is_empty Method | ||
bool is_empty() | ||
return (top == -1) | ||
// pop Method | ||
void pop() { | ||
if(is_empty()) | ||
cout << "Stack underflow." << endl; | ||
else | ||
// Return true if Stack is empty else false | ||
bool empty() | ||
{ | ||
return (top == -1); | ||
} | ||
// Remove a value from top of Stack(Array) | ||
void pop() | ||
{ | ||
// Checking if Stack is empty | ||
if(empty()) | ||
{ | ||
std::cout << "Stack underflow." << '\n'; | ||
} | ||
else | ||
{ | ||
top--; | ||
} | ||
} | ||
// peek Method | ||
int peek() { | ||
if(is_empty()) | ||
cout << "Stack underflow." << endl; | ||
else | ||
return arr[top]; | ||
// Return top value of Stack(Array) | ||
int peek() | ||
{ | ||
return array[top]; | ||
} | ||
}; | ||
//Main Method | ||
int main() { | ||
|
||
int main() | ||
{ | ||
// Declare Stack object | ||
Stack s; | ||
//Todo | ||
// Perform operations | ||
s.push(10); | ||
s.push(20); | ||
s.push(30); | ||
std::cout << s.peek(); | ||
} |