\chapter{Adding elements to Eina\+\_\+\+List}
\hypertarget{eina_list_01_example_page}{}\label{eina_list_01_example_page}\index{Adding elements to Eina\_List@{Adding elements to Eina\_List}}


Creating an \doxylink{group__Eina__List__Group_ga3295e1210efea1fb0fd6acbf3cbc1f29}{Eina\+\_\+\+List} and adding elements to it is very easy and can be understood from an example\+: First thing is always to include \doxylink{Eina_8h}{Eina.\+h}, for this example we also include stdio.\+h so we can use printf. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}

\end{DoxyCodeInclude}
 Just some boilerplate code, declaring some variables and initializing eina. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}\ **argv)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ (void)argc;}
\DoxyCodeLine{\ \ \ (void)argv;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *list\ =\ NULL;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *l;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{void}\ *list\_data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}

\end{DoxyCodeInclude}
Here we add a sequence of elements to our list. By using append we add elements to the end of the list, so the list will look like this\+:~\newline
  
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{eina_list_example_01_a}}
\end{DoxyImageNoCaption}
 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \textcolor{stringliteral}{"{}tigh"{}});}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \textcolor{stringliteral}{"{}adar"{}});}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \textcolor{stringliteral}{"{}baltar"{}});}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(list,\ \textcolor{stringliteral}{"{}roslin"{}});}

\end{DoxyCodeInclude}
There are a couple of interesting things happening here, first is that we are passing a NULL pointer to the first \doxylink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\+\_\+list\+\_\+append()} call, when this is done a list is created. The other {\bfseries{very}} important detail to notice is that the return value is attributed to the {\itshape list} variable, this needs to be done every time we use a a function that alters the contents of the list.

Now that we have a list with some elements in it we can look at its contents. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(list,\ l,\ list\_data)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keywordtype}{char}*)list\_data);}

\end{DoxyCodeInclude}
 There are many ways of accessing elements in the list, including by its index\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ l\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga4f42c9edc62c6c1e15582ee9c50b7b57}{eina\_list\_nth\_list}}(list,\ 1);}

\end{DoxyCodeInclude}
\begin{DoxyNote}{Note}
It should be noted that the index starts at 0.
\end{DoxyNote}
\doxylink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\+\_\+list\+\_\+append()} is not the only way to add elements to a a list. A common requirement is to add an element in a specific position this can be accomplished using \doxylink{group__Eina__List__Group_ga2cc230dec347eaf4dee2e77637063ba5}{eina\+\_\+list\+\_\+append\+\_\+relative()} and \doxylink{group__Eina__List__Group_gaaf5cc0fb74a42e16757e5523bacdee53}{eina\+\_\+list\+\_\+append\+\_\+relative\+\_\+list()}\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_gaaf5cc0fb74a42e16757e5523bacdee53}{eina\_list\_append\_relative\_list}}(list,\ \textcolor{stringliteral}{"{}cain"{}},\ l);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga2cc230dec347eaf4dee2e77637063ba5}{eina\_list\_append\_relative}}(list,\ \textcolor{stringliteral}{"{}zarek"{}},\ \textcolor{stringliteral}{"{}cain"{}});}

\end{DoxyCodeInclude}
First {\itshape "{}cain"{}} is added after the second element (remember that indexes are 0 based) and then we add {\itshape "{}zarek"{}} after {\itshape "{}cain"{}}.

\doxylink{group__Eina__List__Group_ga3295e1210efea1fb0fd6acbf3cbc1f29}{Eina\+\_\+\+List} also has prepend analogs to append functions we have used so far\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga4a2716e8fba1c3f60e3b112caef26fb9}{eina\_list\_prepend}}(list,\ \textcolor{stringliteral}{"{}adama"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga6714b4c62fe6022f6bfe526bb5449b54}{eina\_list\_prepend\_relative}}(list,\ \textcolor{stringliteral}{"{}gaeta"{}},\ \textcolor{stringliteral}{"{}cain"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ list\ =\ \mbox{\hyperlink{group__Eina__List__Group_gac2ae5d9426f552b9f70eb573582b491c}{eina\_list\_prepend\_relative\_list}}(list,\ \textcolor{stringliteral}{"{}lampkin"{}},\ l);}

\end{DoxyCodeInclude}
With this additions our list now looks like this\+:~\newline
  
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{eina_list_example_01_b}}
\end{DoxyImageNoCaption}


Once done using the list it needs to be freed, and since we are done with eina that also need to be shutdown\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(list,\ l,\ list\_data)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}},\ (\textcolor{keywordtype}{char}*)list\_data);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_ga82a1bfa543e21853f9ea3ff88dd1250b}{eina\_list\_free}}(list);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The full source code can be found on the examples folder on the \doxysectlink{eina_list_01_c}{eina\+\_\+list\+\_\+01.c}{0} file. 