Python/C/C++/JAVA

Advanced Practice Programs with Code and Concept

By D.S

Implementing a Stack

"A stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out). Stacks are used in a variety of applications, such as function call management in recursion, syntax parsing, and expression evaluation. By learning how to implement a stack, you'll understand one of the fundamental data structures in computer science, which can be a powerful tool in your programming toolkit."

(a.) C Code

] #include <stdio.h>
        #include <stdlib.h>
        
        #define MAX 100
        
        typedef struct {
            int items[MAX];
            int top;
        } Stack;
        
        void initialize(Stack *s) {
            s->top = -1;
        }
        
        int isFull(Stack *s) {
            return s->top == MAX - 1;
        }
        
        int isEmpty(Stack *s) {
            return s->top == -1;
        }
        
        void push(Stack *s, int value) {
            if (isFull(s)) {
                printf("Stack Overflow
");
                return;
            }
            s->items[++s->top] = value;
        }
        
        int pop(Stack *s) {
            if (isEmpty(s)) {
                printf("Stack Underflow
");
                return -1;
            }
            return s->items[s->top--];
        }
        
        int peek(Stack *s) {
            if (isEmpty(s)) {
                printf("Stack is empty
");
                return -1;
            }
            return s->items[s->top];
        }
        
        int main() {
            Stack s;
            initialize(&s);
        
            push(&s, 10);
            push(&s, 20);
            push(&s, 30);
        
            printf("Top element is %d
", peek(&s));
            printf("Stack elements:
");
        
            while (!isEmpty(&s)) {
                printf("%d
", pop(&s));
            }
        
            return 0;
        }
        
    
Output:-
Top element is 30
Stack elements:
30
20
10

(b.) C++ Code

#include <iostream>
        #include <vector>
        using namespace std;
        
        class Stack {
        private:
            vector<int> items;
        
        public:
            void push(int value) {
                items.push_back(value);
            }
        
            void pop() {
                if (items.empty()) {
                    cout << "Stack Underflow
";
                    return;
                }
                items.pop_back();
            }
        
            int top() {
                if (items.empty()) {
                    cout << "Stack is empty
";
                    return -1;
                }
                return items.back();
            }
        
            bool isEmpty() {
                return items.empty();
            }
        };
        
        int main() {
            Stack s;
        
            s.push(10);
            s.push(20);
            s.push(30);
        
            cout << "Top element is " << s.top() << endl;
            cout << "Stack elements:
";
        
            while (!s.isEmpty()) {
                cout << s.top() << endl;
                s.pop();
            }
        
            return 0;
        }
Output:-
Top element is 30
Stack elements:
30
20
10

(c.) Python Code

class Stack:
      def __init__(self):
          self.items = []
  
      def push(self, item):
          self.items.append(item)
  
      def pop(self):
          if self.is_empty():
              return "Stack Underflow"
          return self.items.pop()
  
      def peek(self):
          if self.is_empty():
              return "Stack is empty"
          return self.items[-1]
  
      def is_empty(self):
          return len(self.items) == 0
  
      def size(self):
          return len(self.items)
  
  # Testing the Stack class
  stack = Stack()
  stack.push(10)
  stack.push(20)
  stack.push(30)
  
  print("Top element is", stack.peek())
  print("Stack elements:")
  
  while not stack.is_empty():
      print(stack.pop())
Output:-
Top element is 30
Stack elements:
30
20
10

(d.) Java Code

import java.util.Stack;

      public class Main {
          public static void main(String[] args) {
              Stack<integer> stack = new Stack<>();
      
              stack.push(10);
              stack.push(20);
              stack.push(30);
      
              System.out.println("Top element is " + stack.peek());
              System.out.println("Stack elements:");
      
              while (!stack.isEmpty()) {
                  System.out.println(stack.pop());
              }
          }
      }
Output:-
Top element is 30
Stack elements:
30
20
10

How did you feel about this post?

😍 🙂 😐 😕 😡

Was this helpful?

👍 👎