\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{eyan} \pdfinfo{ /Title (data-strcuture-c.pdf) /Creator (Cheatography) /Author (eyan) /Subject (Data Strcuture C++ Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A36043} \definecolor{LightBackground}{HTML}{F9F5F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Data Strcuture C++ Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{eyan} via \textcolor{DarkBackground}{\uline{cheatography.com/197714/cs/41730/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}eyan \\ \uline{cheatography.com/eyan} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 17th December, 2023.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Linked List}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Singly Linked List}} - Each node has data and an address field that contains a reference to the next node. \{\{nl\}\} {\bf{Doubly Linked List}} - There are two pointer storage blocks in the doubly linked list. The first pointer block in each node stores the address of the previous node.Then there is the data, and last you have the next pointer, which points to the next node. Thus, you can go in both directions (backward and forward). \{\{nl\}\} {\bf{Circular Linked List}} - The circular linked list is extremely similar to the singly linked list. The only difference is that the last node is connected with the first node, forming a circular loop in the circular linked list.} \tn % Row Count 14 (+ 14) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Operation}}: Traversing, Insertion, Deletion, Searching} \tn % Row Count 16 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Declaration}} \{\{nl\}\} `ListNode *dummyHead = new ListNode(0);` \{\{nl\}\} `ListNode *head;` \{\{nl\}\} {\bf{Two Pointers}} \{\{nl\}\} ` ListNode *slow = head; \{\{nl\}\} ListNode *fast = head-\textgreater{}next;\{\{nl\}\} while (slow != fast) \{\{\{nl\}\} if (!fast || !fast-\textgreater{}next) return false; \{\{nl\}\} slow = slow-\textgreater{}next;\{\{nl\}\} fast = fast-\textgreater{}next-\textgreater{}next;\{\{nl\}\}\}`} \tn % Row Count 23 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Stack}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`empty()` - O(1) \{\{nl\}\} `size()` - O(1) \{\{nl\}\} `top()` - O(1) \{\{nl\}\} `push(element)` - O(1) \{\{nl\}\} `pop()` - O(1) \{\{nl\}\} \seqsplit{`stackname1.swap(stackname2)`} - swap the contents of one stack with another stack O(1) \{\{nl\}\} \seqsplit{`stackname.emplace(value)`} - the element is added to the stack at the top position O(1)} \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Queue}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`empty()` - O(1) \{\{nl\}\} `size()` - O(1) \{\{nl\}\} `swap()` - O(1) \{\{nl\}\} `emplace()` - O(1) \{\{nl\}\} `front()` - return the first element of the queue O(1) \{\{nl\}\} `back()` - return the last element of the queue O(1) \{\{nl\}\}`push()` - O(1) \{\{nl\}\} `pop()` - O(1) \{\{nl\}\}} \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{HashMap \& HashSet}} \tn % Row 0 \SetRowColor{LightBackground} HashMap & HashSet \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `unordered\_map\textless{}string, int\textgreater{} umap` & `unordered\_set\textless{}data\_type\textgreater{} name` \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `at()` - returns the reference to the value with the element as key k \{\{nl\}\} `begin()` \{\{nl\}\} `end()`\{\{nl\}\} `count()`\{\{nl\}\} `find()`\{\{nl\}\}`empty()`\{\{nl\}\}`erase()`\{\{nl\}\} & `size()`\{\{nl\}\} `empty()`\{\{nl\}\} `find()`\{\{nl\}\} `erase()`\{\{nl\}\} `insert()`\{\{nl\}\} \tn % Row Count 12 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{`intSet.find(arr{[}i{]}) == intSet.end()`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Vector}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Initialize}} - `vector\textless{}int\textgreater{} new(2)`\{\{nl\}\} {\bf{Return}} `return\{i, j\}` \{\{nl\}\}{\bf{Sort}} `sort(v.begin(), v.end())`\{\{nl\}\} `empty()`\{\{nl\}\} `push\_back()`\{\{nl\}\} `emplace\_back()`\{\{nl\}\} `pop\_back()`\{\{nl\}\} `reverse()`\{\{nl\}\} `erase()`\{\{nl\}\}} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.748 cm} x{3.374 cm} x{6.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Character / Integer / Other}} \tn % Row 0 \SetRowColor{LightBackground} Character & Integer & Other \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `isalnum()` - check if the character is a digit or a letter \{\{nl\}\} `tolower()` & \seqsplit{`to\_string(integer)`} & `isdigit()` \{\{nl\}\} `rand()` \{\{nl\}\} `lower\_bound() / upper\_bound()` `stoi(\textless{}string\textgreater{})` \tn % Row Count 7 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Sliding Window}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Window Size}}: You have a 'window' of a fixed size, which is just a range that can look at a certain number of elements in the sequence at once. \{\{nl\}\}{\bf{Sliding the Window}}: You start with this window at the beginning of your sequence. Then, you move (or slide) this window one element at a time towards the end. \{\{nl\}\}{\bf{Processing Data}}: At each step, you perform some kind of calculation or check on the elements within the window. This could be finding the sum of numbers, checking for a pattern, and so on. \{\{nl\}\}{\bf{Result}}: The outcome of the algorithm depends on the problem you're solving.} \tn % Row Count 12 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{`//add the string to hashSet or hashMap \newline int i = 0, j = 0; \newline while (j \textless{} vector.size()) \newline if(mp.find() == mp.end())\{ \newline mp{[}string{]}++; \newline \} else (j - i + 1) `} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Binary Search}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Start in the Middle:}} Open the book to the middle page. {\bf{Compare:}} Check if the page you opened is the one you're looking for. If it is, great, you're done! If not, decide if the page you're looking for is before or after the current page. If the page number you want is higher than the middle page, you ignore all the pages before the current page. If the page number you want is lower, you ignore all the pages after the current page. \{\{nl\}\}{\bf{Repeat:}} Take the half of the book you decided to keep (based on the above step) and open to the middle page of that section. Repeat the comparison process. \{\{nl\}\}{\bf{Narrow Down:}} Each time you do this, you cut the number of pages you have to search in half. This makes finding the page much faster than flipping through each page one by one.} \tn % Row Count 16 (+ 16) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int low = 0, high = nums.size() - 1; \newline \newline while (low \textless{}= high) \{ \newline int mid = (low + high) / 2; \newline \newline if (nums{[}mid{]} == target) \{ \newline return mid; \newline \} \newline \newline if (nums{[}low{]} \textless{}= target \&\& target \textless{} nums{[}mid{]}) \{ \newline high = mid - 1; \newline \} else \{ \newline low = mid + 1; \newline \} \newline \newline if (nums{[}mid{]} \textless{} target \&\& target \textless{}= nums{[}high{]}) \{ \newline low = mid + 1; \newline \} else \{ \newline high = mid - 1; \newline \} \newline \newline return -1;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Two Pointers \& Fast \& Slow Pointers}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Two Pointers}} \{\{nl\}\}`int start = 0; \{\{nl\}\}int end = vector.size() - 1; \{\{nl\}\} while (start \textless{} end)...`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Fast Slow Pointers}} `Node *slow\_p = list, *fast\_p = list;\{\{nl\}\}while (slow\_p \&\& fast\_p \&\& fast\_p-\textgreater{}next) \{ \{\{nl\}\}slow\_p = slow\_p-\textgreater{}next;\{\{nl\}\}fast\_p = fast\_p-\textgreater{}next-\textgreater{}next; ...`} \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}