C DSALesson 60Free preview
C Stacks
This lesson is part of C · All levels
Summary
A stack is a LIFO (last in, first out) data structure in which the last element entered through push is the first to exit through pop, similar to a plate stack. Its main operation is push, pop, and the peek always interacts with the top element. Stacks are widely used for undo, expression evaluation, graph travel, and even represent function naming in the language itself.
Key points
- LIFO: push adds elements to the top and pop takes elements from the top.
- The array implementation uses the top variable that marks the top element position.
- Overflow occurs when push on a full stack and underflow when pop on an empty stack, both of which must be checked.
- Stacks can also be implemented with linked lists, i.e. push and pop are performed at the list head.
- Real applications: undo and redo, evaluator of arithmetic expressions, and deep-first search.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.