Skip to content

Instantly share code, notes, and snippets.

@lilpolymath
Created May 5, 2020 22:36
Show Gist options
  • Save lilpolymath/eab20332ffdc39169ef12f0a8da7b0e4 to your computer and use it in GitHub Desktop.
Save lilpolymath/eab20332ffdc39169ef12f0a8da7b0e4 to your computer and use it in GitHub Desktop.
Stacks and Queues with C++
#include <iostream>
#include <iomanip>
#include <string>
// Implementing a stack using an array
// THe initial size of the array
#define st_len 5
using namespace std;
class Stacks
{
public:
Stacks(); // Constructor
int isFull(); //To check if the stack is full i.e. maxed out the allocated space or not
int isEmpty(); // To check if the stack is empty or not
void st_push(int element);
void st_pop();
void show_stack(); //
struct stacks
{
int s[st_len]; // Initalizing the stack
int top; // The topmost element of the array.
} stack;
};
// Initalizes the stack with temp elements of data
Stacks::Stacks()
{
stack.top = -1; //index of the top stack
for (int i; i < st_len; i++)
{
stack.s[i] = 0;
}
}
// Function to check isFull property of the stack
int Stacks::isFull()
{
if (stack.top >= st_len - 1)
{
return 1;
}
else
return 0;
}
// Handles the adding elements into the stack from the top
void Stacks::st_push(int element)
{
stack.top++;
stack.s[stack.top] = element;
}
// Handles removal of an the last element ffrom the stack.
void Stacks::st_pop()
{
stack.top--;
}
// Checks whether the stack is empty or not.
int Stacks::isEmpty()
{
if (stack.top == -1)
{
return 1;
}
else
{
return 0;
}
}
// This show the elements of the stack.
void Stacks::show_stack()
{
if (isEmpty())
{
cout << "The stack is empty. \nPlease initalize one to use the properties." << endl;
}
else
{
for (const int &val : stack.s)
{
cout << val << endl;
}
}
}
// Driver Function
int main()
{
Stacks obj;
obj.show_stack();
if (obj.isFull())
{
cout << "The stack is full." << endl;
}
if (obj.isEmpty())
{
cout << "The stack is empty." << endl;
}
obj.st_push(6);
obj.st_push(2);
obj.st_pop();
obj.st_push(9);
obj.st_push(4);
obj.st_push(3);
obj.show_stack();
if (obj.isFull())
{
cout << "The stack is full." << endl;
}
else
{
obj.show_stack();
}
if (obj.isEmpty())
{
cout << "The stack is empty." << endl;
}
else
{
obj.show_stack();
}
}
@lilpolymath
Copy link
Author

#include <iostream>
#include <iomanip>
#include <string>

#define st_len 5

using namespace std;

class Queues
{
public:
    Queues();      // Constructor
    int isFull();  //To check if the queue is full i.e. maxed out the allocated space or not
    int isEmpty(); // To check if the queue is empty or not
    void push(int element);
    void pop();
    // void show_queue();
    struct queues
    {
        /* elements of data */
        int q[st_len]; // Initalizing the stack
        int top;       // The topmost element of the array.
    } queue;
};

// Initalizes the queue with temp elements of data
Queues::Queues()
{
    queue.top = -1; //index of the top queue
    for (int i; i < st_len; i++)
    {
        queue.q[i] = 0;
    }
}

// Function to check isFull property of the queue
int Queues::isFull()
{
    if (queue.top >= st_len - 1)
    {
        return 1;
    }
    else
        return 0;
}

// Handles the adding elements into the queue from the top
void Queues::push(int element)
{
    queue.top++;
    queue.q[queue.top] = element;
}

void Queues::pop()
{
    queue.top--;
}

int Queues::isEmpty()
{
    if (queue.top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// void Queues::show_queue()
// {
//     if (isEmpty())
//     {
//         cout << "\n The queue is empty. Please initalize one to use the properties.";
//     }
//     else
//     {
//         for (int i = 0; i < st_len; i++)
//         {
//             cout << queue.q[i];
//         }
//     }
// }

int main()
{
    Queues obj;
    obj.push(6);
    obj.push(2);
    obj.pop();
    obj.push(4);
    obj.push(4);
    obj.push(4);
    // obj.show_queue();
    if (obj.isFull())
    {
        cout << "The queue is full.";
    }
    else
    {
        for (int i = 0; i < st_len; i++)
        {
            cout << obj.queue.q[i];
        }
    }
    if (obj.isEmpty())
    {
        cout << "The queue is empty.";
    }
    else
    {
        for (int i = 0; i < st_len; i++)
        {
            cout << obj.queue.q[i];
        }
    }
}

@Youngestdev
Copy link

Agba🙌🏿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment