Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to implement stack

#include<bits/stdc++.h>
using namespace std;
int Stack[100],n,top,i;

void push(int x) {
   if(top<n-1) {
   	  top++;
      Stack[top]=x; 
   } else {
      cout<<"Could not insert data, Stack is full.
";
   }
}

int pop() {
	int data;
   if(top>-1) {
      data = Stack[top];
      top = top - 1;   
      return data;
   } else {
      cout<<"Could not retrieve data, Stack is empty.
";
   }
}

void display()
{
    if(top>=0)
    {
        cout<<"
 The elements in STACK 
";
        for(i=top; i>=0; i--)
           cout<<Stack[i]<<" ";
    }
    else
    {
        cout<<"
 The STACK is empty";
    }
   
}

int main()
{
	cin>>n;
	top=-1;
	push(10);
	push(20);
	push(40);
	push(10);
	cout<<endl<<pop();
	display();
	return 0;
}
Source by www.raulmelo.dev #
 
PREVIOUS NEXT
Tagged: #implement #stack
ADD COMMENT
Topic
Name
7+4 =