<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linked List :: CS3 course</title><link>https://robark.gitlab.io/linkedlist/index.html</link><description>Linked Lists</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 21 Jun 2021 22:56:17 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/linkedlist/index.xml" rel="self" type="application/rss+xml"/><item><title>Intro</title><link>https://robark.gitlab.io/linkedlist/intro/index.html</link><pubDate>Thu, 17 Jun 2021 22:41:03 +0000</pubDate><guid>https://robark.gitlab.io/linkedlist/intro/index.html</guid><description>Linked Lists Introduction #include &lt;iostream&gt; using namespace std; struct node{ string s; node* next; }; void walk(node* np){ //recursive function //if (np==0) return; //if (np !=0 ) //remember 0 is false and anthing else is true if (np) { cout&lt;&lt;np-&gt;s&lt;&lt;endl; walk(np-&gt;next); } } int main(){ node A,B,C; A.s="adam"; B.s="bob"; C.s="cathy"; A.next= &amp;B; B.next= &amp;C; C.next=0; //nullptr node* root=&amp;A; walk(root); /* while (root!=0) { //cout&lt;&lt;(*root).s&lt;&lt;endl; //root=(*root).next; cout&lt;&lt; root-&gt;s &lt;&lt;endl; root = root-&gt;next; } */ return 0; }</description></item><item><title>In_depth</title><link>https://robark.gitlab.io/linkedlist/in_depth/index.html</link><pubDate>Mon, 21 Jun 2021 22:56:17 +0000</pubDate><guid>https://robark.gitlab.io/linkedlist/in_depth/index.html</guid><description>Linked list functions using recursion #include &lt;iostream&gt; using namespace std; struct node{ node(string , node*); //this is the ctor function. Note:only data types are required string st; node* next; }; /* node* NewNode(string s){ node* np = new node; np-&gt;st = s; np-&gt;next = 0; return np; } */ //better to set ctor node::node(string s, node* np=0){ st = s; next = np; } void walk(node* nd){ /* cout&lt;&lt; nd-&gt;st &lt;&lt;" "; //segfaults if nd=0 if ( nd == 0 ) return; if (nd-&gt;next !=0 ) walk(nd-&gt;next); */ //alternate better solution if (nd==0) return; cout&lt;&lt; nd-&gt;st &lt;&lt;" "; walk(nd-&gt;next); } /* void length(node* nd, int&amp; count){ count += 1; if (nd-&gt;next != 0) length(nd-&gt;next, count); return; } */ //better length function which returns int int length(node* nd ){ if (nd == 0) return 0; return 1 + length(nd-&gt;next); } //Must pass nd by ref or seg fault. We must change root node address in main. void pushfront(node* &amp;nd, string s){ //usage: pushfront(root, "bill"); //node* nwn = NewNode(s); //nwn-&gt;next = nd; //nd = nwn; //alternate using node ctor: better nd = new node(s,nd); //node* nwn = new node(s, nd); //nd=nwn; } /* //alternate pushfront without by ref node* pushfront(node* nd, string s){ //creates new node and returns that node pointer // usage: root=pushfront(root, "bill"); return ( new node(s, nd)); } */ void popfront(node* &amp;nd){ //usage: popfront(root) if (nd != 0) //can't popfront if nothing to pop { node* temp = nd-&gt;next; delete nd; nd = temp; } } //alternate popfront. Same usage but not by ref. Ok in C /* void popfront(node** ndp){ //instead of passing by ref //usage: popfront(&amp;root) if (*ndp != 0) { node* temp = (*ndp)-&gt;next; delete *ndp; *ndp = temp; } } */ //alternate popfront. Different usage: not by ref. /* node* popfront(node* nd){ //usage: root = popfront(root) if (nd != 0) { node* temp = nd-&gt;next; delete nd; nd=0; //not necessary because nd not by ref return temp; } } */ /*can't pushback on root=0 since no node exists yet. So must check for it. copy ctor called for nd pointer so root in main not changed. This is okay if one node already exists since we just add to the end of it. But if no node exists yet (root=0) then we must change root addr so we have to pass by ref also. void pushback(node* &amp;nd, string s){ if (nd == 0) { nd = NewNode(s); return; } node* root = nd; while (nd-&gt;next != 0) nd = nd-&gt;next; nd-&gt;next = NewNode(s); nd = root; //must set nd back to root as we passed it by ref } */ //better pushback void pushback(node* &amp;nd, string s){ // better to pass string by ref so we do not copy s every recursive call if (nd == 0){ // or !nd nd = new node(s); //call new directly for defined struct ctor return; } pushback(nd-&gt;next, s); } void popback(node* &amp;nd){ if (nd == 0) return; //can't delete nothing /* if (nd-&gt;next == 0) { delete nd; nd = 0; } else { popback(nd-&gt;next); } */ //better way if (nd-&gt;next == 0) { delete nd; nd = 0; //important return; } popback(nd-&gt;next); } /* must pass nd by ref since we may need to insert at the root node and hence change the root addr */ void insert(node* &amp;nd, string s){ /* if (nd == 0) { nd = NewNode(s); return; } if (s &lt; nd-&gt;st) { pushfront(nd, s); return; } else { insert(nd-&gt;next, s); return; } */ //better alternative if (nd == 0 || (s &lt;= nd-&gt;st)) // !nd is the same as nd == 0 //note: compound if statement order matters. Second comparison is skipped if first is true. //this is called "short circuit evaluation" which is necessary to prevent runtime error // of derefencing null pointer with nd-&gt;st when nd == 0 { pushfront(nd, s); // works because pushfront is by ref // equivalent nd = new node(s, nd); //when node ctor is overloaded return; } insert(nd-&gt;next, s); } void lfree(node* nd){ /* if (nd == 0) return; //don't crash if nothing in linked list if (nd-&gt;next != 0) lfree(nd-&gt;next); delete nd; nd = 0; //sets all linked list pointers to zero (including the first one, root=0) */ //alternate better solution: recurses down to the null pointer if (nd == 0) return; lfree(nd-&gt;next); //recursion first cout&lt;&lt;"free node with string: "&lt;&lt; nd-&gt;st &lt;&lt;endl; delete nd; nd = 0; } int main(){ const char* sarray[6] = {"b","d","a","e","f","c"}; node* root=0; int x; for (x=0; x&lt;6 ; x++) { //pushback(root, sarray[x]); //pushfront(root, sarray[x]); cout&lt;&lt;"insert: "&lt;&lt;sarray[x]&lt;&lt;endl; insert(root, sarray[x]); cout&lt;&lt;"walk"&lt;&lt;endl; walk(root); cout&lt;&lt;endl; } for (x=0; x&lt;3 ; x++) { cout&lt;&lt;"popfront"&lt;&lt;endl; popfront(root); cout&lt;&lt;"walk"&lt;&lt;endl; walk(root); cout&lt;&lt;endl; } cout&lt;&lt;"insert: z "&lt;&lt;endl; insert(root, "z"); cout&lt;&lt;"walk"&lt;&lt;endl; walk(root); cout&lt;&lt;endl; for (x=0; x&lt;3 ; x++) { cout&lt;&lt;"popback"&lt;&lt;endl; popback(root); walk(root); cout&lt;&lt;endl; } cout&lt;&lt;"pushback aaa"&lt;&lt;endl; pushback(root, "a"); cout&lt;&lt;"walk"&lt;&lt;endl; walk(root); cout&lt;&lt;endl; cout&lt;&lt;"free memory"&lt;&lt;endl; lfree(root); return 0; }</description></item></channel></rss>