C++ : Implement stack using array
Code:
// Stack - Object oriented implementation using arrays
|
int A[MAX_SIZE]; // array to store the stack |
int top; // variable to mark the top index of stack. |
top = -1; // for empty array, set top = -1 |
// Push operation to insert an element on top of stack. |
if(top == MAX_SIZE -1) { // overflow case. |
printf("Error: stack overflow\n"); |
// Pop operation to remove an element from top of stack. |
if(top == -1) { // If stack is empty, pop should throw error. |
printf("Error: No element to pop\n"); |
// Top operation to return element at top of stack. |
// This function will return 1 (true) if stack is empty, 0 (false) otherwise |
// ONLY FOR TESTING - NOT A VALID OPERATION WITH STACK |
// This function is just to test the implementation of stack. |
// This will print all the elements in the stack at any stage. |
// Code to test the implementation. |
// calling Print() after each push or pop to see the state of stack. |
}
Output:
Related Video:
Related Links:
Related Programs:
Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...
-
Character I/O: Visual Basic provides some excellent ways for input and output operations. Input can be taken using TextBox, InputBox, ...
-
Design a form contains a sorted list alphabetically such that the user can add the item from text to the list after click on command button...
-
Scientific Calculator Created Using Visual Basic This is a calculator that resembles a typical scientific calculator , albeit a simpler ...