Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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 :: c++ iterate through constant list 
Cpp :: Madiar loh 
Cpp :: the amount of input is unknown 
Cpp :: surf interpolation matlab 
Cpp :: even number program in c++ using for loop stack overflow 
Cpp :: top array data structure questions in inteviews 
Cpp :: c++ round number to 2 decimal places 
Cpp :: scope resulation operator :: in c++ 
Cpp :: c create 1 bit value 
Cpp :: log base 10 c+_+ 
Cpp :: How to remove the % in zsh that show after running c++ file 
Cpp :: windows servis from console app 
Cpp :: jquery datepicker default date not working 
Cpp :: dream speedrun song mp4 
Cpp :: KL/wweiok#L['.[- 
Cpp :: c++ constructor inheritance 
Cpp :: c++ x y in arrau 1d 
Cpp :: destiny child 
Cpp :: bnchch 
Cpp :: c++ scanf always expects double and not float 
Cpp :: map::begin 
Cpp :: error c4001 
Cpp :: c++ sort cout end 
Cpp :: GoPro camera for kids aus 
Cpp :: c++ string to const char* 
Cpp :: find maximum contiguous Sub arrays 
Cpp :: program in c++ for simple interest rate 
Cpp :: convert c to C language 
Cpp :: https://www.google 
Cpp :: C++ Features 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =