\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{alicanpayasli} \pdfinfo{ /Title (data-structes.pdf) /Creator (Cheatography) /Author (alicanpayasli) /Subject (Data Structes 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}{28A315} \definecolor{LightBackground}{HTML}{F1F9F0} \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 Structes Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{alicanpayasli} via \textcolor{DarkBackground}{\uline{cheatography.com/194447/cs/40587/}}} \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}alicanpayasli \\ \uline{cheatography.com/alicanpayasli} \\ \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 11th October, 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}{Array}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{Allows data of the same type to be kept under a single variable name.The number of elements is specified when defining. \newline % Row Count 3 (+ 3) {\bf{Identification Examples: }} \newline % Row Count 4 (+ 1) {\bf{Example 1:}} `int array1{[}12{]};` \newline % Row Count 5 (+ 1) {\bf{Example 2:}} `char array2{[}20{]};` \newline % Row Count 6 (+ 1) {\bf{Assign elements to arrays examples:}} \newline % Row Count 7 (+ 1) {\bf{Example 1:}} `int array1{[}5{]} = \{1,2,3,4,5\};` \newline % Row Count 8 (+ 1) {\bf{Example 2:}} `int array2{[}5{]} = \{6,7,8\}; //last two elements is 0` \newline % Row Count 10 (+ 2) {\bf{Example 3:}} \newline % Row Count 11 (+ 1) `int array3{[}5{]}, i;` \newline % Row Count 12 (+ 1) `for (i = 0; i\textless{}5; i++)\{` \newline % Row Count 13 (+ 1) `array3{[}i{]} = i;` \newline % Row Count 14 (+ 1) `printf("\%d\textbackslash{}n", array3{[}i{]});\}` \newline % Row Count 15 (+ 1) {\bf{Multidimensional arrays examples:}} \newline % Row Count 16 (+ 1) {\bf{Example 1:}} \newline % Row Count 17 (+ 1) `int array{[}3{]}{[}2{]} = \{\{1,2\},\{3,4\},\{5,6\}\};` \newline % Row Count 18 (+ 1) `int i,j;` \newline % Row Count 19 (+ 1) `for(i = 0;i\textless{}3; i++)\{` \newline % Row Count 20 (+ 1) `for(j=0; j\textless{}2; j++)\{` \newline % Row Count 21 (+ 1) `printf("\%d\textbackslash{}t", array{[}i{]}{[}j{]});\}` \newline % Row Count 22 (+ 1) `printf("\textbackslash{}n");\}` \newline % Row Count 23 (+ 1) {\bf{Example 2:}} \newline % Row Count 24 (+ 1) `int array{[}3{]}{[}2{]}{[}2{]} = \{1,2,3,4,5,6,7,8,9,10,11,12\};` \newline % Row Count 26 (+ 2) `int i,j,k;` \newline % Row Count 27 (+ 1) `for(i = 0; i\textless{}3; i++)\{` \newline % Row Count 28 (+ 1) `for(j = 0; j\textless{}2; j++)\{` \newline % Row Count 29 (+ 1) `for(k = 0; k\textless{}2; k++)\{` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Array (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ `printf("\%d\textbackslash{}t", array{[}i{]}{[}j{]}{[}k{]});``\}` \newline % Row Count 1 (+ 1) `printf("\textbackslash{}n");``\}` \newline % Row Count 2 (+ 1) `printf("\textbackslash{}n");``\}`% Row Count 3 (+ 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}{Stacks}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{It is a data structure in which the data is kept in a linear way and addition and subtraction are made from the top point. Last in first out rule is valid. \newline % Row Count 4 (+ 4) {\bf{ Stack With Array:}} \newline % Row Count 5 (+ 1) `int Stack{[}10{]};` \newline % Row Count 6 (+ 1) `int top = -1;` \newline % Row Count 7 (+ 1) `void Push(int a)\{` \newline % Row Count 8 (+ 1) `if(top == 9)\{` \newline % Row Count 9 (+ 1) `printf("No room in the stack.\textbackslash{}n");\}` \newline % Row Count 10 (+ 1) `else\{` \newline % Row Count 11 (+ 1) `Stack{[}++top{]} = a;` \newline % Row Count 12 (+ 1) `printf("\%d added into the stack.\textbackslash{}n", a);\}\}` \newline % Row Count 14 (+ 2) `int pop()\{` \newline % Row Count 15 (+ 1) `if (top\textless{}0)\{` \newline % Row Count 16 (+ 1) `printf("No data in the stack.\textbackslash{}n");` \newline % Row Count 17 (+ 1) `return -1;\}` \newline % Row Count 18 (+ 1) `else\{` \newline % Row Count 19 (+ 1) `int a = Stack{[}top-{}-{]};` \newline % Row Count 20 (+ 1) `printf("\%d removed from the stack.\textbackslash{}n", a);` \newline % Row Count 22 (+ 2) `return a;`\}\}` \newline % Row Count 23 (+ 1) `int peek()\{` \newline % Row Count 24 (+ 1) `if (top\textless{}0)\{` \newline % Row Count 25 (+ 1) `printf("No data in the stack.\textbackslash{}n");` \newline % Row Count 26 (+ 1) `return -1;\}` \newline % Row Count 27 (+ 1) `else\{` \newline % Row Count 28 (+ 1) `printf("\%d is at the top of the stack.\textbackslash{}n", Stack{[}top{]});` \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Stacks (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ `return Stack{[}top{]};\}\}` \newline % Row Count 1 (+ 1) {\bf{Stack With Linked List: }} \newline % Row Count 2 (+ 1) `struct Node \{` \newline % Row Count 3 (+ 1) `int data;` \newline % Row Count 4 (+ 1) `struct Node* next;\};` \newline % Row Count 5 (+ 1) `struct Node* top = NULL;` \newline % Row Count 6 (+ 1) `void Push(int a) \{` \newline % Row Count 7 (+ 1) `struct Node* t = (struct Node*) malloc(sizeof(struct Node));` \newline % Row Count 9 (+ 2) `t-\textgreater{}data = a;` \newline % Row Count 10 (+ 1) `if(top == NULL)\{` \newline % Row Count 11 (+ 1) `top = t;` \newline % Row Count 12 (+ 1) `top-\textgreater{}next = NULL;\}` \newline % Row Count 13 (+ 1) `else \{` \newline % Row Count 14 (+ 1) `t-\textgreater{}next = top;` \newline % Row Count 15 (+ 1) `top = t;\}` \newline % Row Count 16 (+ 1) `printf("\%d added into the stack.\textbackslash{}n", a);\}` \newline % Row Count 17 (+ 1) `int Pop() \{` \newline % Row Count 18 (+ 1) `if(top == NULL) \{` \newline % Row Count 19 (+ 1) `printf("No data in the stack.\textbackslash{}n");` \newline % Row Count 20 (+ 1) `return -1;\}` \newline % Row Count 21 (+ 1) `else \{` \newline % Row Count 22 (+ 1) `struct Node* t = top;` \newline % Row Count 23 (+ 1) `int a = t-\textgreater{}data;` \newline % Row Count 24 (+ 1) `top = top-\textgreater{}next;` \newline % Row Count 25 (+ 1) `printf("\%d removed from the stack. \textbackslash{}n", a);` \newline % Row Count 26 (+ 1) `free(t);` \newline % Row Count 27 (+ 1) `return a;\}\}` \newline % Row Count 28 (+ 1) `int Peek() \{` \newline % Row Count 29 (+ 1) `if(top == NULL) \{` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Stacks (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ `printf("No data in the stack.\textbackslash{}n");` \newline % Row Count 1 (+ 1) `return -1;\}` \newline % Row Count 2 (+ 1) `else \{` \newline % Row Count 3 (+ 1) `printf("\%d is at the top of the stack.\textbackslash{}n", top-\textgreater{}data);` \newline % Row Count 5 (+ 2) `return top-\textgreater{}data;\}\}`% Row Count 6 (+ 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}{Linked List}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{They are structures in which objects of the same type are stored in a linear order and interconnected. The objects in the linked list are called nodes, and nodes are connected to each other by pointers that point to the next node. There is also a head pointer that points to the beginning of the list.Nodes consist of two parts. \newline % Row Count 7 (+ 7) {\bf{Singly linked list: }}Navigation in the list is forward only. \newline % Row Count 9 (+ 2) {\bf{Doubly linked list: }}Navigation in the list is forward and back. \newline % Row Count 11 (+ 2) {\bf{Circular linked list: }}The next pointer of the last node points to the first node of the list. \newline % Row Count 13 (+ 2) {\bf{Create Node: }} \newline % Row Count 14 (+ 1) `struct Node \{` \newline % Row Count 15 (+ 1) `int data;` \newline % Row Count 16 (+ 1) `struct Node* next;` \newline % Row Count 17 (+ 1) `\};` \newline % Row Count 18 (+ 1) `struct Node* head = NULL;` \newline % Row Count 19 (+ 1) {\bf{Insertion: }} \newline % Row Count 20 (+ 1) `void Insert(int a)\{` \newline % Row Count 21 (+ 1) `struct Node{\emph{ t = (struct Node}}) malloc(sizeof(struct Node));` \newline % Row Count 23 (+ 2) `t-\textgreater{}data = a;` \newline % Row Count 24 (+ 1) `t-\textgreater{}next = head;` \newline % Row Count 25 (+ 1) `head= t;` \newline % Row Count 26 (+ 1) `\}` \newline % Row Count 27 (+ 1) {\bf{Deletion: }} \newline % Row Count 28 (+ 1) `void Delete()\{` \newline % Row Count 29 (+ 1) `if(head!=NULL)\{` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Linked List (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ `struct Node *t = head;` \newline % Row Count 1 (+ 1) `head = head-\textgreater{}next;` \newline % Row Count 2 (+ 1) `free(t);` \newline % Row Count 3 (+ 1) `\}``\}` \newline % Row Count 4 (+ 1) {\bf{Traversal: }} \newline % Row Count 5 (+ 1) `void traverse()\{` \newline % Row Count 6 (+ 1) `struct Node*t = head;` \newline % Row Count 7 (+ 1) `while(t!=NULL)\{` \newline % Row Count 8 (+ 1) `printf("\%d ", t-\textgreater{}data);` \newline % Row Count 9 (+ 1) `t = t-\textgreater{}next;` \newline % Row Count 10 (+ 1) `\}``\}` \newline % Row Count 11 (+ 1) {\bf{Example: }} \newline % Row Count 12 (+ 1) `int i, j;` \newline % Row Count 13 (+ 1) `for(i = 1; i\textless{}=10; i++)\{` \newline % Row Count 14 (+ 1) `Insert(i);``\}` \newline % Row Count 15 (+ 1) `for(j = 1; j\textless{}=4; j++)\{` \newline % Row Count 16 (+ 1) `Delete();``\}` \newline % Row Count 17 (+ 1) `traverse();`% Row Count 18 (+ 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}{Queue}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{It ensures that the data is kept in linear order. They are referred to by the first in first out (FIFO) rule. \newline % Row Count 3 (+ 3) {\bf{Queue With Array: }} \newline % Row Count 4 (+ 1) `void Enqueue(int a)\{` \newline % Row Count 5 (+ 1) `if(count == 6)\{` \newline % Row Count 6 (+ 1) `printf("No place in the queue.\textbackslash{}n");``\}` \newline % Row Count 7 (+ 1) `else\{` \newline % Row Count 8 (+ 1) `queue{[}rear{]} = a;` \newline % Row Count 9 (+ 1) `rear++;` \newline % Row Count 10 (+ 1) `if(rear == 6) rear = 0;` \newline % Row Count 11 (+ 1) `count++;` \newline % Row Count 12 (+ 1) `printf("\%d added in the queue.\textbackslash{}n", a);``\}``\}` \newline % Row Count 13 (+ 1) `void Dequeue()\{` \newline % Row Count 14 (+ 1) `if(count == 0)\{` \newline % Row Count 15 (+ 1) `printf("No data in the queue.\textbackslash{}n");\}` \newline % Row Count 16 (+ 1) `else\{` \newline % Row Count 17 (+ 1) `int a = queue{[}front{]};` \newline % Row Count 18 (+ 1) `front++;` \newline % Row Count 19 (+ 1) `if(front == 6) front = 0;` \newline % Row Count 20 (+ 1) `count-{}-;` \newline % Row Count 21 (+ 1) `printf("\%d removed from the queue.\textbackslash{}n", a);\}\}` \newline % Row Count 22 (+ 1) {\bf{Queue With Linked List: }} \newline % Row Count 23 (+ 1) `struct Node \{` \newline % Row Count 24 (+ 1) `int data;` \newline % Row Count 25 (+ 1) `struct Node* next;\};` \newline % Row Count 26 (+ 1) `struct Node* front = NULL;` \newline % Row Count 27 (+ 1) `struct Node* rear = NULL;` \newline % Row Count 28 (+ 1) `void Enqueue(int a)\{` \newline % Row Count 29 (+ 1) `struct Node* t = (struct Node*) malloc(sizeof(struct Node));` \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Queue (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ `t-\textgreater{}data = a;` \newline % Row Count 1 (+ 1) `t-\textgreater{}next = NULL;` \newline % Row Count 2 (+ 1) `if(front == NULL \&\& rear == NULL)\{` \newline % Row Count 3 (+ 1) `front = rear = t;\}` \newline % Row Count 4 (+ 1) `else\{` \newline % Row Count 5 (+ 1) `rear-\textgreater{}next = t;` \newline % Row Count 6 (+ 1) `rear=t;\}` \newline % Row Count 7 (+ 1) `printf("\%d added in the queue.\textbackslash{}n", a); \}` \newline % Row Count 8 (+ 1) `void Dequeue()\{` \newline % Row Count 9 (+ 1) `if(front == NULL)\{` \newline % Row Count 10 (+ 1) `printf("No data in the queue.\textbackslash{}n");\}` \newline % Row Count 11 (+ 1) `else\{` \newline % Row Count 12 (+ 1) `struct Node* t= front;` \newline % Row Count 13 (+ 1) `if(front == rear)\{` \newline % Row Count 14 (+ 1) `front = rear = NULL;\}` \newline % Row Count 15 (+ 1) `else\{` \newline % Row Count 16 (+ 1) `front = front-\textgreater{}next;\}` \newline % Row Count 17 (+ 1) `printf("\%d removed from the queue.\textbackslash{}n", t-\textgreater{}data);\}\}`% Row Count 19 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}