Simple Stack Program using pointers in C++ Programming
Definition
A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.
Simple Stack Program using pointers in C++ Programming
/* Simple Stack Program Example Using Pointers in C++*/
/* Data Structure Programs,Stack Programs,C++ Examples */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 3
using namespace std;
void push(int i);
void pop(void);
int choice, i;
int *tos, *p1, arr_stack[MAX_SIZE];
int exit_p = 1;
int main() {
int value;
tos = arr_stack; /* tos points to the top of stack */
p1 = arr_stack; /* initialize p1 */
cout << "\nSimple Stack Example - Pointers";
do {
cout << "\nStack Pointer : Main Menu";
cout << "\n1.Push \t2.Pop \tOthers to exit : Your Choice : ";
cin>>choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin>>value;
push(value);
break;
case 2:
pop();
break;
default:
exit_p = 0;
break;
}
} while (exit_p);
return 0;
}
void push(int i) {
if (p1 == (tos + MAX_SIZE)) {
cout << "\nStatus : Stack Overflow.\n";
} else {
*p1 = i;
cout << "\nPush Value : %d " << *(p1);
p1++;
}
}
void pop(void) {
if (p1 == tos) {
cout << "\nStatus : Stack Underflow.";
//return 0;
} else {
p1--;
cout << "\nPop Value : %d " << *(p1);
}
}
Sample Output
Simple Stack Example - Array and Pointers
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 100
Push Value : 100
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 200
Push Value : 200
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 300
Push Value : 300
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 400
Status : Stack Overflow.
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 500
Status : Stack Overflow.
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 1
Enter value: 2
Status : Stack Overflow.
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 2
Pop Value : 300
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 2
Pop Value : 200
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 2
Pop Value : 100
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 2
Status : Stack Underflow.
Stack Pointer : Main Menu
1.Push 2.Pop Others to exit : Your Choice : 3
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming