0% found this document useful (0 votes)
2 views

STACK DSA 70

The document presents a C++ implementation of a stack data structure using an array. It includes methods for pushing, popping, peeking, checking if the stack is empty, and displaying the stack contents. The main function allows user input for the number of elements to push and pop from the stack, demonstrating its functionality.

Uploaded by

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

STACK DSA 70

The document presents a C++ implementation of a stack data structure using an array. It includes methods for pushing, popping, peeking, checking if the stack is empty, and displaying the stack contents. The main function allows user input for the number of elements to push and pop from the stack, demonstrating its functionality.

Uploaded by

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

STACK IMPLEMENTATION USING ARRAY

//NAME – DIVYA PATWA| 24U030070| IT SEC2

#include <iostream>

# define max 100

using namespace std;

struct stack{

public:

int top;

int arr[max];

stack(){

top =-1;

void push(int x){

if (top>=max-1){

cout<<"stack overflow"<<endl;

else{

arr[++top]= x;

int pop(){

if (top<0){

cout<<"stack underflow"<<endl;

return -1;

else{

int x = arr[top];

top--;

return x;

}
STACK IMPLEMENTATION USING ARRAY

int peek(){

if (top<0){

return -1;

else{

return arr[top];

bool isempty(){

if (top<0){

return 1;

else {

return 0;

void display(){

for (int i=0;i<=top;i++){

cout<<arr[i]<<" ";

};

int main() {

stack s;

int n;

cout<<"no .of elements push in stack ";

cin>>n;
STACK IMPLEMENTATION USING ARRAY

int x;

for(int i=0;i<n;i++){

cin>>x;

s.push(x);

int y;

cout<<"no .of elements pop in stack ";

cin>>y;

for(int i=0;i<y;i++){

s.pop();

s.display();

cout<<endl;

cout<<"the top element is "<< s.peek()<<endl;;

if(s.isempty()){

cout<<"stack is not zero"<<endl;

else{

cout<<"stack is empty"<<endl;

return 0;

OUTPUT

You might also like