Sonzaschool
Rudi

Sekondari ya Juu · Kidato cha Sita

Sayansi ya Kompyuta

Implement stack and queue, binary search tree, balanced tree (such as an AVL tree), graph, hash table data structures in object-oriented programming language

takriban dakika 10 kusoma

Mada za sehemu hiiDemonstrate mastery of basic principles of Algorithms and Data structuresMada 7

Implementing Data Structures in Object-Oriented Programming

When programming, the way we organise and store data directly affects how fast our programs run and how efficiently they use memory. Different problems require different data organisations—some need quick insertion and removal from one end (stack), others need to handle items in the order they arrive (queue), and more complex scenarios require hierarchical structures (trees), networks (graphs), or fast lookup tables (hash tables). This study note shows you how to implement each of these data structures using an object-oriented approach in C++.

A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from only one end called the top. Think of a stack of plates in a cafeteria: you add plates on top and remove them from the top.

Stack Operations

  • push(item): Add an element to the top of the stack
  • pop(): Remove and return the top element
  • peek(): View the top element without removing it
  • isEmpty(): Check if the stack contains no elements
  • isFull(): Check if the stack has reached its maximum capacity
  • size(): Return the number of elements in the stack

C++ Implementation Using a Class

#include <iostream>
using namespace std;

class Stack {
private:
    int *arr;
    int top;
    int capacity;

public:
    Stack(int size) {
        capacity = size;
        arr = new int[capacity];
        top = -1;
    }

    ~Stack() {
        delete[] arr;
    }

    void push(int item) {
        if (top >= capacity - 1) {
            cout << "Stack Overflow! Cannot push " << item << endl;
            return;
        }
        arr[++top] = item;
    }

    int pop() {
        if (isEmpty()) {
            cout << "Stack Underflow!" << endl;
            return -1;
        }
        return arr[top--];
    }

    int peek() {
        if (isEmpty()) {
            return -1;
        }
        return arr[top];
    }

    bool isEmpty() {
        return top == -1;
    }

    bool isFull() {
        return top == capacity - 1;
    }

    int size() {
        return top + 1;
    }

    void display() {
        if (isEmpty()) {
            cout << "Stack is empty" << endl;
            return;
        }
        cout << "Stack elements: ";
        for (int i = 0; i <= top; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    Stack s(5);
    
    s.push(10);
    s.push(20);
    s.push(30);
    s.display();
    
    cout << "Popped: " << s.pop() << endl;
    cout << "Top element: " << s.peek() << endl;
    s.display();
    
    return 0;
}

Output:

Stack elements: 10 20 30 
Popped: 30
Top element: 20
Stack elements: 10 20 

Swali

In a stack implemented using a class in C++, what happens when the pop() operation is called on an empty stack?

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza