0% found this document useful (0 votes)
6 views2 pages

DSA Array

The document presents a C++ implementation of a stack using an array, including functions for pushing, popping, and displaying stack elements. It defines a maximum stack size and handles overflow and underflow conditions. The main function provides a user interface for continuous operations on the stack through a menu-driven approach.

Uploaded by

tejassasane7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

DSA Array

The document presents a C++ implementation of a stack using an array, including functions for pushing, popping, and displaying stack elements. It defines a maximum stack size and handles overflow and underflow conditions. The main function provides a user interface for continuous operations on the stack through a menu-driven approach.

Uploaded by

tejassasane7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1) Stack Using Array:-

=======================

#include <iostream>
using namespace std;
#define MAX 100

int stack[MAX];
int top = -1;

void push(int val){


if(top>=MAX-1){
cout <<"Stack is Overflow";
return;
}
top++;
stack[top] = val;
cout << val <<" pushed into stack";
cout <<endl;
}

int pop(){
if(top < 0){
cout <<"Stack is empty";
return -1;
}
int num=stack[top];
top--;
cout <<"\n"<< num <<" is poped from stack\n";
return num;
}

void display(){
if(top < 0){
cout <<"Stack is empty";
return;
}
cout <<"\nStack elements are:-\n";
for(int i=top; i>=0; i--){
cout << stack[i] << " ";
cout << endl;
}
}

int main() {
int ch, value;
while (true) { // Loop for continuous operation
cout <<"------------------------------";
cout << "\n1. Push\n";
cout << "2. Pop\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout <<"------------------------------\n";
cout << "Enter your choice: ";
cin >> ch;
cout <<"\n------------------------------";

switch (ch) {
case 1:
cout << "\nEnter the element to push: ";
cin >> value;
push(value);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
cout << "\nExiting...\n";
return 0;

default:
cout << "Invalid choice. Try again.\n";
}
}
return 0;
}
===================================================================================
=======================================================

You might also like