General
Computer Science
-
1. Introduction to Computer Science
-
Introduction to Computer Science
-
History of Computer Science
-
Fundamentals of Computer Science
-
Algorithms
-
Data Structures
-
Programming Concepts
-
Web Development
-
Databases and SQL
-
Networking and Security
-
Artificial Intelligence and Machine Learning
-
Mobile App Development
-
Game Development
-
Future of Computer Science
-
Careers in Computer Science
Legacy Course
Stacks
A stack is a linear data structureThe organisation and order of information in a text. that follows the Last In First Out (LIFO) principle. It consists of a collection of elements, where the most recently added element is the first one to be removed (i.e., the last element in the stack is the first one to be removed). The top of the stack is the element that is most likely to be removed next.
There are three main operations that can be performed on a stack: push, pop, and peek.
- The push operation adds an element to the top of the stack.
- The pop operation removes the top element from the stack.
- The peek operation returns the top element of the stack without removing it.
Stacks can be represented using an array or a linked list. The array representation is a fixed-size array and the linked list representation is a singly linked list with a head pointing to the top of the stack.
Stacks are commonly used in a wide range of applications, some of the most common uses are:
Expression EvaluationMaking judgements about how effective a text or technique is.: When evaluating expressions, operators and operands are pushed onto the stack as they are encountered. When an operator is encountered, the necessary operands are popped from the stack, the operation is applied and the result is pushed back onto the stack.
Backtracking: In certain problems like the depth-first search algorithm, the stack is used to keep track of the current path, so that when the search reaches a dead-end, it can backtrack by popping the last move from the stack and try a different path.
Function Calls: Stacks are used to keep track of function calls during the execution of a program. When a function is called, its address is pushed onto the stack, and when the function returns, its address is popped from the stack.
Continue the lesson
This section is available to learners with course access. Continue learning with Knowness to unlock the full explanation, examples, revision tools, and progress tracking.
The remaining lesson content includes further guided explanation, important learning points, and supporting interactive material designed to help you understand and revise this topic.
Unlock this topic to view the full activity, worked examples, common mistakes, and additional revision support.
More content available
Knowness lessons are structured to build understanding step by step. Create an account or upgrade your access to continue from this point.
This preview does not include the hidden lesson text, answers, explanations, or embedded interactions.
Continue learning with Knowness
Sign up to access the full lesson, predicted grades, revision tools, progress tracking, and more.
Create a free account