codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ stacks and queues

C++ stacks and queues

LIFO and FIFO.

A stack uses push, top, pop. A queue uses push, front, pop.

Commands in this lesson

push

Definition: Adds an item to the end of an array.

Syntax:

array.push(item);

Example:

const a = [1, 2];
a.push(3);
console.log(a);

Example code

#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main() {
  stack<int> s; s.push(1); s.push(2);
  queue<int> q; q.push(1); q.push(2);
  cout << s.top() << " " << q.front() << endl;
  return 0;
}

Run this code in the online editor

← C++ listC++ deque →