Coding Exercise – Simple Stack Implementation in C++


A Stack is a data structure that satisfies Last-In-First-Out. It can be considered as a pile of plates where you always take the top plate (you put on last).

Here is athe walk-through picture that contains the C++ source code for defining the simplest stack.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
// HelloACM.com
 
using namespace std;
 
template <class T> 
class Stack{
private:
    T *arr; // basic type of element comes from template
    int pt, cap;
public:
    Stack(int capacity) { // constructor
        arr = new T[cap = capacity];
        pt = -1; // next pop position
    }
 
    ~Stack() { // destructor
        delete [] arr; // avoid memory leak
    }
 
    T pop() {
        if (pt == -1) {
            throw "stack empty";
        }
        T elem = arr[pt--];
        return elem;
    }
 
    void push(T elem) {
        if (pt == capacity() - 1) {
            throw "stack full";
        }
        arr[++pt] = elem;
    }
 
    int len() {
        return pt + 1;// current number in the stack
    }
 
    int capacity() {
        return cap; // maximum number of elements in the stack
    }
};
 
int main()
{
    Stack stack(10);
    for (int i = 1; i < 5; i ++) {
        stack.push(i); // push 1,2,3,4
    }
    for (int i = 1; i < 5; i ++) {
        cout << stack.pop(); // pop, 4,3,2,1
    }
    return 0;
}
#include <iostream>
// HelloACM.com

using namespace std;

template <class T> 
class Stack{
private:
    T *arr; // basic type of element comes from template
    int pt, cap;
public:
    Stack(int capacity) { // constructor
        arr = new T[cap = capacity];
        pt = -1; // next pop position
    }

    ~Stack() { // destructor
        delete [] arr; // avoid memory leak
    }

    T pop() {
        if (pt == -1) {
            throw "stack empty";
        }
        T elem = arr[pt--];
        return elem;
    }

    void push(T elem) {
        if (pt == capacity() - 1) {
            throw "stack full";
        }
        arr[++pt] = elem;
    }

    int len() {
        return pt + 1;// current number in the stack
    }

    int capacity() {
        return cap; // maximum number of elements in the stack
    }
};

int main()
{
    Stack stack(10);
    for (int i = 1; i < 5; i ++) {
        stack.push(i); // push 1,2,3,4
    }
    for (int i = 1; i < 5; i ++) {
        cout << stack.pop(); // pop, 4,3,2,1
    }
    return 0;
}

The dynamic array is created inside the constructor of the class and deleted in the destructor. Each time a push is called, it checks first the position of the pointer (points to next pop position), if it reaches the end, then it means the stack is full, an exception is thrown, otherwise, increment the pointer and store the new item.

For pop operation, if checks if the pointer is -1, meaning the stack is empty, if not, return the current element at the pointer and decrement the pointer.

This simple C++ example teaches you how to declare a class with template, create an object (instance), define the stack data structure, throw an exception and so much more.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
359 words
Last Post: Algorithms to Find the Single Number in C++
Next Post: Microsoft Excel Tricks: Remove Unit from Cell Values

The Permanent URL is: Coding Exercise – Simple Stack Implementation in C++

Leave a Reply