Loading…
Loading…
Topic
Articles and problems tagged Linked Lists.
main receives its command-line arguments as argc and argv, an array of pointers to strings. And a function such as printf can take any number of arguments using stdarg.h. Learn both, including default promotions, va_end, and why a variadic function must be told how many arguments it got.
Eight solved problems on linked lists and binary trees: concatenate two lists, erase a list, count, compare, copy and append with recursion, traverse a tree in inorder with your own stack instead of recursion, and mirror a tree. Each solution is complete, runnable and explained line by line.
A graph is a set of vertices joined by edges — the structure behind road maps, networks and circuits. Learn the vocabulary, store a graph as an adjacency matrix or as adjacency lists built from linked lists, and visit every reachable vertex with depth first search and breadth first search.
In a circular linked list the last node links back to the first, so the list has no NULL end. Build a queue on a circular list, learn how to traverse a list that never ends, keep the circle closed on every deletion, and see why a single rear pointer is enough.
Sort a linked list with selection sort and bubble sort in two ways: by exchanging the data inside the nodes, and by rearranging the links so the nodes themselves change order. See when each is better, and how a pointer to a link turns the four special cases of node swapping into two.
Store a polynomial such as 4x^5 + 2.5x^4 - 3x^2 + 6x + 1 as a linked list of terms, then add two polynomials in one pass and multiply them term by term, combining equal powers and removing terms that cancel. Every tricky line is explained.
Why linked lists beat arrays when data grows and shrinks, then merge two sorted linked lists into a third one in a single pass, keeping common values only once. Every pointer move is explained, including the empty-list case that the classic version crashes on.
Five exercises with full solutions: a recursive determinant using calloc, reading and changing file attributes in a raw directory entry, a sorted directory listing with a linked list, and decoding a boot sector and partition table byte by byte.
Five exercises on structures and pointers: arrays of pointers to structures, a list of malloc'd nodes, 2-D char arrays in a structure, nested structures through a pointer, and calling a function before declaring it. Predict each output, then find the hidden bug.
In a doubly linked list each node points to both the next and the previous node, so you can walk either way and delete a node without searching for its predecessor. Build one with append, insert, delete and two-way display, with every link change drawn.
Build a stack (last in, first out), a queue (first in, first out) and a deque (both ends) on top of linked lists, so they can grow without a fixed size. Learn push, pop, enqueue and dequeue, why front and rear must stay in sync, and how to report an empty structure safely.
Keep a linked list in ascending order as nodes are added, by finding the right gap and splicing the new node in. Then reverse a whole list in place with three pointers. Includes a short-circuit bug that crashes the classic version, and a neat pointer-to-pointer technique.
A linked list stores data in nodes scattered through memory, chained together by pointers. Build one step by step: append, add at the beginning, insert after a node, display, count, delete and free, with diagrams of every link change and the reason functions take a struct node **.