\documentclass[12pt]{article}
 \usepackage{color, splat, inchmargs, headsup, verbatim, hyperref}

\setlength{\parskip}{\medskipamount}

 \nofiles % Uncomment this line if there are no cross references.

\begin{document}

\inputnospell{coursemacs}
\inputnospell{namemacs}
\inputnospell{genmacs}

\solutionsfalse\solutionsnull

\title{COMP 264 Lecture Notes}
 \author{R. I. Greenberg}
 \date{}%\date{\expandafter\uncompactdate\jobname}
 \maketitle

\section{More on Pointers and Linked Lists}

\newcommand{\glref}[1]{\href{http://rig.cs.luc.edu/~rig/courses/c264/general/lecsup/#1}{#1}}

I got the following from asking ChatGPT to ``Write a C program to read command line arguments into a linked list'':
\glref{argstoll/original.c}

\soln{Got same result with inclusion of "in linear time", but ChatGPT is mistaken about it being linear time.}

Here is the code with nice indentation\soln{ (from using M-C-q in emacs)}:
\\\glref{argstoll/reindented.c}

The above code does correctly handle freeing the linked list unlike the following naive approach one may have tried in
the \verb+freeList+ function: \glref{argstoll/freeingerror.c} .  We'll see how we can use GDB on this to see
where the segmentation fault occurs with the \verb+bt+ command.

Now, going back to the file \\\glref{argstoll/reindented.c} , let's just do some variable renaming so that the name
\verb+head+ will be used in a consistent fashion and use \verb+ptrtohead+ where appropriate.  Also slightly compacting
code by removing curly braces on a while loop body containing just one statement. That gives us
\\\glref{argstoll/consistenthead.c} .

Next, let's avoid having any occurrence of \verb+**+, making \verb+InsertEnd+ called like other procedures.  But if we are not careful, we can have a failure due to the subroutine not returning a modified head:
\\\glref{argstoll/nodoubleptr.c}

We can fix this by adding logic to \verb+main+ so that \verb+InsertEnd+ only needs to operate on a non-empty list, and
simplifying \verb+InsertEnd+:
\\\glref{argstoll/fixednodoubleptr.c}

Inserting at the end of the list isn't very efficient, since you have to march through the whole list each time.  Let's work towards a different approach by eliminating the InsertEnd procedure.  Here we just do the same thing inline in the main procedure:
\\\glref{argstoll/inline.c}

Now let's modify this to do the insertion at the front of the list,
which makes the program run in linear time but means the list is
reversed:
\\\glref{argstoll/insertfront.c}

Now let's try to do linear time with insertion at rear, but here there is an error causing a segmentation fault:
\\\glref{argstoll/linearinsertrearwerror.c}

 Here there is a \verb+printf+ to help with debugging, and we can note here how fflush helps to get printout even though
 there is about to be a segmentation fault.  Noting that the program crashes only when \verb+argc>2+ helps indicate
 where the error is.  You also could use gdb to pin down where the crash is occurring and use gdb print commands to
 check values of variables at that point; will demo in class.

I will let you see if you can fix the error.  Your upcoming homework also will give you a chance to do some exploration of linked list programming.

\soln{Other things I am going to not show:
\\linearinsertrear.c: Do the insertion at the rear of the list in linear time by maintaining tail pointer.
\\explainstrdup.c: Shows how argv array is not modified when data in linked list is modified.
\\nostrdup.c: Shows result of not using strdup and simplification of freeList.
\\eltcust.c and charptrcust.h: Do it with machinery to do other types in eltcust.h initially linked to charptrcust.h
\\eltcust.c and intcust.h: Now link eltcust.h to intcust.h to do with ints.
}

\end{document}
