Show Menu
Cheatography

Data Structure API Cheat Sheet (DRAFT) by

Programming data strucure cheet sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Java Wrapper Class

byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character

Array

 
C++
Java
Python3
new
int x = [0];
int[] x = new int[]{0};
x = [0]
length
std::s­ize(x) // C++17
x.length
len(x)

String

 
C++
Java
Python3
new
char* s = "­str­ing­";
String s = "­str­ing­";
s = "­str­ing­"
get
char c = s[0];
char c = s.char­At(0);
c = s[0];
length
strlen(s)
s.length()
len(s)
sub string
char* sub = malloc(10);
strncpy(sub, s+begin, end-be­gin);
String sub = s.subs­tri­ng(­begin, end);
s[begin, end]
check substring
if (strst­r(sub, s))
if (s.con­tai­ns(­sub))
if sub in s:

Stack

 
C++
Java
Python3
include
#include <st­ack>
import java.u­til.*;
new
std::s­tac­k<i­nt> s;
Stack<­Int­ege­r> s = new Stack<­Int­ege­r>();
s = []
push
s.push(i)
s.push(i);
s.appe­nd(i)
pop
s.pop();
popped = s.pop();
popped = s.pop()
top
s.top()
s.peak()
s[-1]
size
s.size()
s.size()
len(s)
check empty
if (s.emp­ty())
if (s.emp­ty())
if not s:

Queue

 
C++
Java
Python3
include
#include <qu­eue>
import java.u­til.*;
new
std::q­ueu­e<i­nt> q;
Queue<­Int­ege­r> q = new Linked­Lis­t<>();
q = []
push
q.push(i);
q.add(i);
q.appe­nd(i)
pop
q.pop()
popped = q.remo­ve();
popped = q.pop(0)
front
q.front()
q.peek()
q[0]
size
q.size()
q.size()
len(q)
check empty
if (q.emp­ty())
if (q.isE­mpty())
if not q:

Heap

 
C++
Java
Python3
include
#include <qu­eue>
import java.u­til.*;
from queue import Priori­tyQueue
new min heap
std::p­rio­rit­y_q­ueu­e<int, std::v­ect­or<­int­>, std::g­rea­ter­<in­t>> h;
Queue<­Int­ege­r> h = new Priori­tyQ­ueu­e<>();
h = Priori­tyQ­ueue()
new max heap
std::p­rio­rit­y_q­ueu­e<i­nt> h;
Queue<­Int­ege­r> h = new Priori­tyQ­ueu­e<>­(Co­lle­cti­ons.re­ver­seO­rde­r());
push
h.push(i);
h.add(i);
h.put((1, "­Har­ry"))
pop
h.pop();
Integer popped = h.poll();
popped = h.get()
top
h.top()
h.peek()
popped = h.get()
h.put(popped)
size
h.size()
h.size()
h.qsize()
check empty
if (h.emp­ty())
if (h.isE­mpty())
if h.empty():

HashSet

 
C++
Java
Python3
include
#include <un­ord­ere­d_s­et>
import java.u­til.*;
new
std::u­nor­der­ed_­set­<in­t> s;
Set<In­teg­er> s = new HashSe­t<>();
s = set()
add
s.inse­rt(x);
s.add(x);
s.add(x)
delete
s.eras­e(x);
s.remo­ve(x);
s.disc­ard(x)
check in set
std::u­nor­der­ed_­set­<in­t>:­:co­nst­_it­erator it = s.find(x);
if (it != s.end())
if (s.con­tai­ns(x))
if x in s:
size
s.size()
s.size()
len(s)
check empty
if (s.emp­ty())
if (s.isE­mpty())
if not s:

HashMap

 
C++
Java
Python3
include
#include <un­ord­ere­d_m­ap>
import java.u­til.*;
new
std::u­nor­der­ed_­map­<int, int> t;
Hashta­ble­<In­teger, Intege­r> t = new Hashta­ble­<>();
d = {}
add
t.inse­rt(­std­::m­ake­_pa­ir<int, int>(key, val));
t.put(key, val);
d[key] = val
get
int val = t.at(key);
Integer val = t.get(­key);
val = d[key]
delete
t.eras­e(key);
t.remo­ve(­key);
del d[key]
iterate
 
for (Map.E­ntr­y<I­nteger, Intege­r> set : t.entr­ySet()) {
key=set.getKey();
val=set.getValue();
}
iterate
 
t.forE­ach­((key, val)->­{...})
for key in d:
check in table
std::u­nor­der­ed_­map­<int, int>::­con­st_­ite­rator it = t.find(key);
if (it != t.end())
if (t.con­tai­nsK­ey(­key))
if key in d.keys():
size
s.size()
s.size()
len(s)
check empty
if (s.emp­ty())
if (s.isE­mpty())
if not s: