Search
 
SCRIPT & CODE EXAMPLE
 

CPP

what is a stack in programming

A stack is an array or list structure of function calls and parameters used in modern computer programming
Comment

stack function

function Stack() {
  let items = [];

  function push(element) {
    items.push(element);
  }

  function pop() {
    return items.pop();
  }

  function peek() {
    return items[items.length - 1];
  }

  function isEmpty() {
    return items.length === 0;
  }

  function size() {
    return items.length;
  }

  function clear() {
    items = [];
  }

  return {
    clear,
    push,
    pop,
    peek,
    isEmpty,
    size,
  };
}
Comment

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: scope resolution operator in c++ 
Cpp :: double pointers C++ 
Cpp :: how to pass arrays by reference c++ 
Cpp :: How to get the last element of an array in C++ using std::array 
Cpp :: search in vector of pairs c++ 
Cpp :: c++ array on heap 
Cpp :: c++ set element at index 
Cpp :: how to parse using stringstream 
Cpp :: how to include a library in arduino 
Cpp :: what is push() c++ 
Cpp :: what does | mean in c++ 
Cpp :: open a url with dev c 
Cpp :: char * to string c++ 
Cpp :: linux x11 copy paste event 
C :: remix icon cdn 
C :: how to store a user input with spaces in c 
C :: c colour 
C :: roll binary c 
C :: how to print helloq world in c 
C :: type change in c 
C :: thread in c 
C :: how to read character from a string in c 
C :: c programming itoa() example 
C :: svg not loading in chrome 
C :: write array of char to file in c 
C :: strong number in c 
C :: read a document from console in c 
C :: multiplication table in c 
C :: C program to check whether character is lowercase or not using ASCII values 
C :: arrays in c 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =