Priority Queue in C: Binary Heap Implementation
A priority queue returns the highest-priority element first. See a tested C binary heap with O(log n) push and pop, plus design pitfalls.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
A priority queue returns the highest-priority element first. See a tested C binary heap with O(log n) push and pop, plus design pitfalls.
A graph models vertices and edges. This guide implements an undirected graph in C with an adjacency list, memory cleanup, BFS and DFS, and compares it to a matrix.
Compare hash tables and binary search trees in C by speed, ordering and memory, with complete working implementations and guidance on when to use each.
A stack removes the newest item first (LIFO) and a queue removes the oldest first (FIFO). See complete C implementations and when to use each.
A heap keeps the largest or smallest item at the root. See how to implement a max heap in C with push, pop, build and heap sort.
A binary search tree keeps keys ordered so search, insert and delete take O(h) time. This guide gives a complete, leak-free C implementation.
A tree in C is a self-referencing struct linked by pointers. See a complete binary search tree with insert, search, traversals and memory cleanup.
Learn how to implement a FIFO queue in C with a circular array and a linked list, including enqueue, dequeue, peek and memory cleanup.
Learn what a stack is in C and implement it two ways, with a fixed array and with a linked list, including push, pop, peek and error handling.
A linked list in C is a chain of heap-allocated nodes joined by next pointers. See a complete singly linked list, doubly and circular variants, and costs versus arrays.
There is no new in C. Malloc only reserves bytes, so you write initialisation and cleanup yourself. See the create and destroy pattern.
C has no constructors, so copy and move are plain functions. Learn how to deep copy owned memory and transfer ownership safely, with examples.
C has no copy constructors, so you write clone functions. Learn shallow vs deep copy and avoid double frees when copying structs in C.
C has no constructors, so you imitate them with functions. See the C equivalents of default, parameterised and copy constructors and how they differ from C++.
Constructors and destructors do not exist in C; they are C++ features. See the C init and destroy pattern and the GCC attribute extensions.
C has no virtual keyword, yet virtual dispatch matters in C code. Learn how function-pointer tables decouple callers and why large C systems use them.
C does not allow overloading operators for structs. Learn the reasons and the practical alternatives: named functions, _Generic dispatch and function pointers.
C has no classes, only structs that hold data. See what a struct can do, how to get class-like design in C, and how C++ class and struct differ.
C has no this pointer, but you can get the same effect with an explicit struct pointer. Compare it to C++ with working examples.
Compare C and C++ syntax and features with examples: structs vs classes, overloading, templates, memory handling and what breaks when C compiles as C++.