\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{shorttcircuitt (shortt\_circuitt)} \pdfinfo{ /Title (c-interview-questions.pdf) /Creator (Cheatography) /Author (shorttcircuitt (shortt\_circuitt)) /Subject (C Interview Questions 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{C Interview Questions Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{shorttcircuitt (shortt\_circuitt)} via \textcolor{DarkBackground}{\uline{cheatography.com/77499/cs/19018/}}} \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}shorttcircuitt (shortt\_circuitt) \\ \uline{cheatography.com/shortt-circuitt} \\ \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 4th March, 2019.\\ 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{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Count Bits Set in a Byte}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{void count\_bits(unsigned char number) \{ \newline int cnt = 0; \newline while (number !=0 ) \{ \newline cnt += number\&0x01; \newline number \textgreater{}\textgreater{}= 1; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{One alternative is a lookup table.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{2.32 cm} x{5.68 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Set, Clear, Toggle a bit in a byte}} \tn % Row 0 \SetRowColor{LightBackground} Set & `y |= 1\textless{}\textless{}n` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Clear & `y \&= \textasciitilde{}(1 \textless{}\textless{}n)` \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Toggle & `y \textasciicircum{}= (1\textless{}\textless{}n)` \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Operating on the n\textasciicircum{}th\textasciicircum{} bit of `y`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Reverse a Byte}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{uchar reverse\_byte(uchar byte) \{ \newline uchar new\_byte = 0; \newline int i = 0; \newline for (i = 0; i\textless{}7; i++) \{ \newline new\_byte += (byte \& 0x01); \newline new\_byte \textless{}\textless{}= 1; \newline byte \textgreater{}\textgreater{}= 1; \newline \} \newline return (new\_byte); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Strings - Reverse a String}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{void reverse(char *str) \newline \{ \newline // Non-null pointer; non-empty string \newline if (str != 0 \&\& {\emph{str != '\textbackslash{}0') \{ \newline char }}end = str + strlen(str) - 1; \newline while (str \textless{} end) \{ \newline char tmp = {\emph{str; \newline }}str++ = {\emph{end; \newline }}end-{}- = tmp; \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Strings - Reverse a String by Word}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Strings - Implement strstr}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Strings - atoi}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{int myAtoi(char {\emph{str) \newline \{ \newline int res = 0; // Initialize result \newline // Iterate through all characters of input string \newline // and update result \newline for (int i = 0; str{[}i{]} != '\textbackslash{}0'; ++i) \newline res = res}}10 + str{[}i{]} - '0'; \newline // return result. \newline return res; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Single Linked Lists - Data Structure}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Typedef struct Node \{ \newline int data; \newline struct Node *next; \newline \} node;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{This is Linked List data structure used for the Linked Lists problems.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Single Linked Lists - Insert Head}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{void insert\_node(node {\emph{*head, int data) \{ \newline // create new node and allocate memory \newline node }}new\_node = malloc (sizeof(node)); \newline if (new\_node == NULL) return 0; \newline // set the new node data \newline new\_node-\textgreater{}data = data; \newline // we are adding the new node as head \newline // update the next to point to current head \newline new\_node-\textgreater{}next = {\emph{head; \newline // update the head to point to the new node \newline }}head = new\_node; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{We pass a pointer to a pointer (**head) so when we update the pointer, we don't update the local copy only.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Single Linked Lists - Delete Node}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{void delete\_node(node {\emph{*head, int del) \{ \newline node }}tmp; \newline if ({\emph{head == NULL) return; \newline tmp = }}head; \newline if (tmp-\textgreater{}data == del) \{ \newline //delete head case \newline {\emph{head = tmp-\textgreater{}next; \newline free(tmp); \newline return; \newline \} else \{ \newline while (tmp \&\& tmp-\textgreater{}next != NULL) \{ \newline if (tmp-\textgreater{}next-\textgreater{}data == del) \{ \newline // save the next node to be free'd \newline node }}n = tmp-\textgreater{}next; \newline // make the current next pointer \newline // point to the deleted node next \newline tmp-\textgreater{}next = n-\textgreater{}next; \newline free(n); \newline return; \newline \} \newline tmp = tmp-\textgreater{}next; \newline \} \newline return; \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Single Linked Lists - Find Lopp}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{int \seqsplit{detectLoopandFindBegin(node} *head) \newline \{ \newline node *slowPtr = head, *fastPtr = head; \newline while(slowPtr \&\& fastPtr \&\& fastPtr-\textgreater{}next) \{ \newline slowPtr = slowPtr-\textgreater{}next; \newline fastPtr = fastPtr-\textgreater{}next-\textgreater{}next; \newline if(slowPtr == fastPtr) \newline return 1; \newline \} \newline \} \newline return 0; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}