Deleting the first element took 18 moves, according to my own program
Section 2.2 of the textbook is a page and a half. Two functions, ListInsert_Sq and ListDelete_Sq, under forty lines between them, and one closing sentence: “on average about half the elements move, so the time complexity is O(n)“. I turned the page thinking there was nothing here to write about.
First version I typed straight out of the book. Compiled on the first try, and the output came out tidy. That made me suspicious, so I put a g_move++ inside the insert function to see how many elements actually moved.
Then one line came out: “[delete i=1] moved 18 times”.
Deleting the first element out of eleven leaves at most ten moves. So where did 18 come from.
I stared at that line for a good two minutes before it came back to me: I had written g_move = 0 at the top of ListInsert_Sq. At the time it seemed obviously right — clear the counter before every insert. What actually happened is that when the delete function started counting, it was still carrying the eight moves left over from the previous nine inserts. 18 was 8 plus 10.
I moved the reset back into main, once before each measurement, and the number dropped to 10 immediately.
I had not planned to write this part down. But it makes a point: “number of moves” in the textbook is not an abstract formula. It is something you can miscount, and the program will keep running while quietly telling you the wrong number.
The three fields
A sequential list is just a table packed into one contiguous block of memory. Nothing mysterious about it. The part that actually trips people up is the three fields in the struct.

elem is the address of that block, length is how many items are standing in it right now, listsize is how many it can hold at most.
Mix those two counts up and the whole realloc section becomes decoration: when length reaches 20 you will assume the table is full, but the code compares against listsize, which may not have moved at all, and the 21st element goes straight past the end. The program still does not crash. You just see a line of nonsense in the printout — several dozen lines later.
The typedef int Status wrapper looked like textbook fussiness at first. It only made sense later, when the caller has to tell “the argument was wrong” apart from “the allocation failed”; that is why OVERFLOW is defined separately as -2. If both came back as -1, at the call site you could not tell whether to retry or to go fix your code.
Three lines of initialization:
1 | |
Drop the sizeof(ElemType) and the allocation is a quarter of the size it should be — I worked it out for int: twenty elements need eighty bytes, ask for twenty and that is all you get. It still “works”, because the first four elements have not yet overwritten anyone else’s memory. With double it shows up faster. The compiler has no opinion about any of this.
The textbook sets LIST_INIT_SIZE to 100. I used 20, for a practical reason: with 100 the terminal prints a dozen lines of numbers, the screenshot needs two screens of scrolling, and nobody can see the point. Twenty fits on one screen and still shows two growth steps.
The direction of the loop
This is the one part of the section that actually bites.
1 | |
p starts at the last element, walks backwards one step at a time until it reaches the insertion slot, and on each step copies the value under it one cell to the right.
I did not work this out in my head. I wrote a thirty-line program that ran “back to front” and “front to back” on the same array. The array is 10 20 30 40 50 60 70 80, and 77 goes into position 3:

Reverse the direction and the result is 10 20 77 30 30 30 30 30 30. Everything from 40 to 80 is gone and 30 has been copied six times.
The reason is simple enough: going front to back, the very first step *(q+1) = *q overwrites the value that was sitting at q+1, and that value no longer exists. Every subsequent cell reads the number that was just written there.
What I want to stress is that nothing reports an error. No segfault, no warning, and length is incremented as usual. All you get is a feeling that the table looks a bit odd.
So: shift right from the back, shift left from the front. That sentence is easy to memorize, but only after you have run the wrong version once does your hand hesitate the next time you write it.
Deletion

1 | |
*e = *p grabs the value before it is lost. A C function returns exactly one value, so if it has to return both a status and the deleted element, an output parameter is the only way; the textbook has no choice here.
++p in the for-initializer is easy to miss. Miss it and the first action becomes *(p-1) = *p, which writes the value you just saved right back where it came from — length goes down, the value is still there, and the table has one duplicate at the end.
The --L->length after the loop cannot be forgotten. Forget it and length is one higher than the number of real elements, and the next insert writes into a slot that no longer belongs to this table.
Checking the numbers
Compiled with clang -std=c99 -Wall -Wextra, zero warnings. The clang that ships with macOS is Apple clang 17, nothing else to install.

Line the printed numbers up against the textbook formulas:
| Operation | i |
n before |
Moves reported | Textbook formula |
|---|---|---|---|---|
| insert 5 at head | 1 | 8 | 8 | n-i+1 = 8 |
| insert 99 at tail | 10 (n+1) |
9 | 0 | n-i+1 = 0 |
| insert 77 in middle | 3 | 10 | 8 | n-i+1 = 8 |
| delete first | 1 | 11 | 10 | n-i = 10 |
| delete last | 10 (n) |
10 | 0 | n-i = 0 |
All five match.
The moment they matched was honestly a bit boring, because they were supposed to match. The useful part is that “half on average” is now something I can derive instead of recite: an insert has n+1 possible positions with moves ranging from 0 to n, so n/2 on average; a delete has n positions with moves from 0 to n-1, so (n-1)/2.
Which means insert-at-head and delete-at-head are the two most expensive operations, while insert-at-tail and delete-at-tail are O(1). That is exactly why using a sequential list as a stack is not a bad deal.
Growing the array is not an error, it is a move
The textbook does not return ERROR here; it calls realloc on the spot.
1 | |
The run looks like this (the program prints in Chinese; output copied as-is):
1 | |
The second growth step I forced. Growing by 10 at a time means ten more inserts before it fills again, so I added another fill-then-insert loop just to get the number 40 into the screenshot.
Both traps live in those five lines: realloc may move the whole block somewhere else, so any pointer you saved earlier into elem is void on the spot and you must overwrite L->elem with the return value; and listsize has to be updated in the same breath, otherwise the next “is it full” check compares against the old value and writes out of bounds.
Neither mistake crashes right away. They crash dozens of lines later, and then you spend an hour looking at a completely unrelated location.
Boundaries
1 | |
i is 1-based; the first element lives at elem[0]. A valid insert is 1 <= i <= n+1, a valid delete is 1 <= i <= n.
The off-by-one exists because an insert may land after the last element. Thinking in array indices, it is easy to write i > L->length, and then “append at the end” — the cheapest operation of all — gets rejected as illegal.
Three mistakes the compiler will not catch
- Forgetting the
-1oni. Use&(L->elem[i])directly and inserting at position 2 actually writes position 3. The compiler does not care. - Using
lengthas the capacity.for (k = 0; k < L->listsize; k++)prints uninitialized memory too, which looks like “the data got scrambled”. - Not updating the pointer after
realloc. The returned address is thrown away and you keep writing through a pointer to memory that may already be freed.
All three show up only in the output, and very quietly.
Off-topic: the panel in Xcode that will not close
The screenshots were more work than the code.
Open a single .c file in Xcode, hit Cmd+0 to collapse the navigator, then turn off Minimap from the Editor menu — leave it on and the right edge of every screenshot is a strip of colour that looks like you failed to crop.
What actually annoyed me is the Coding Intelligence onboarding panel that pops up on the left the first time you open a C file. Esc does not close it. Clicking empty space does not close it. It wants you to press Set Up. I did not want to.
The fix was to set the window position to a negative number, {-250, 30}, which pushes the whole panel off the left edge of the screen, and then capture starting at x=133. The panel is not in the images, so it looks like I had a clean setup from the start.
Nothing like this belongs in a tutorial, but skip it and every screenshot carries it.
Next
2.3 is the linked representation. The pain of a sequential list is “insert one element, move a batch”; a linked list trades those moves for pointers, and the price is losing random access.
That post will also make the program count for itself, spreading out the O(1) and O(n) of GetElem — and, while we are at it, measuring how much sequential access and random jumping differ on the same list.
Reproducing it: the sources are here — sqlist.c, reversed.c, run.sh. The screenshots are untouched — screencapture -R on a window region, then sips scaled to 1500px wide, nothing else.