In this lesson, we will learn about implementing a stack data structure in C++. A stack is a data structure that follows the last-in, first-out (LIFO) order. This means that the last element added to the stack is the first one to be removed.
To implement a stack, we will use an array and a variable to keep track of the top of the stack. We will define a class called Stack
which will have the following private members:
top
: an integer variable to store the index of the top elementstack
: an array to store the elements of the stack
We will also define the following public member functions:
isEmpty()
: a function to check if the stack is emptyisFull()
: a function to check if the stack is fullpush(element)
: a function to push an element onto the stackpop()
: a function to pop an element from the stackpeek()
: a function to get the top element of the stack
To push an element onto the stack, we will first check if the stack is full. If it is, we will output 'Stack Overflow' and return. Otherwise, we will increment the top
variable and assign the element to stack[top]
.
To pop an element from the stack, we will first check if the stack is empty. If it is, we will output 'Stack Underflow' and return -1. Otherwise, we will decrement the top
variable and return the element at stack[top]
.
Here is an example code implementation of a stack in C++:
1#include <iostream>
2using namespace std;
3
4// Define the maximum size of the stack
5#define MAX_SIZE 100
6
7// Define the class for the stack data structure
8class Stack {
9private:
10 int top;
11 int stack[MAX_SIZE];
12
13public:
14 // Constructor
15 Stack() {
16 top = -1;
17 }
18
19 // Check if stack is empty
20 bool isEmpty() {
21 return (top == -1);
22 }
23
24 // Check if stack is full
25 bool isFull() {
26 return (top == MAX_SIZE - 1);
27 }
28
29 // Push element onto the stack
30 void push(int element) {
31 if (isFull()) {
32 cout << "Stack Overflow" << endl;
33 return;
34 }
35 stack[++top] = element;
36 cout << "Pushed " << element << " onto the stack" << endl;
37 }
38
39 // Pop element from the stack
40 int pop() {
41 if (isEmpty()) {
42 cout << "Stack Underflow" << endl;
43 return -1;
44 }
45 return stack[top--];
46 }
47
48 // Get the top element of the stack
49 int peek() {
50 return stack[top];
51 }
52};
53
54int main() {
55 // Create a stack object
56 Stack stack;
57
58 // Push elements onto the stack
59 stack.push(1);
60 stack.push(2);
61 stack.push(3);
62
63 // Pop elements from the stack
64 int popped = stack.pop();
65
66 // Get the top element of the stack
67 int topElement = stack.peek();
68
69 // Print the stack
70 cout << "Stack: ";
71 while (!stack.isEmpty()) {
72 cout << stack.pop() << " ";
73 }
74 cout << endl;
75
76 // Print the popped and top element
77 cout << "Popped: " << popped << endl;
78 cout << "Top Element: " << topElement << endl;
79
80 return 0;
81}
xxxxxxxxxx
}
using namespace std;
// Define the maximum size of the stack
// Define the class for the stack data structure
class Stack {
private:
int top;
int stack[MAX_SIZE];
public:
// Constructor
Stack() {
top = -1;
}
// Check if stack is empty
bool isEmpty() {
return (top == -1);
}
// Check if stack is full
bool isFull() {
return (top == MAX_SIZE - 1);
}
// Push element onto the stack