Array-based stack
A stack is a data structure that operates on the LIFO (Last In, First Out) principle. Graphically, a stack can be represented as a column or stack of objects:

The stack has a top, which is formed by the most recently added element. When the new element is placed on top of the stack, forming a new top. When deleting, the element at the top of the stack is removed, and the previous element forms the new top.
So, in the figure above, the top of the stack is initially “Tom.” After adding a new element, “Bob,” this element is positioned on top of “Tom,” becoming the new top.
The .NET class library already has a class that functions as a stack. This class is System.Collections.Generic.Stack. But let’s look at how we can implement a stack-like structure ourselves.
The stack structure, regardless of programming language, has a certain common functionality, which consists of a method for adding an element (usually called push()) and a method for popping an element from the top of the stack (usually called pop()). Furthermore, stack implementations often include a method for getting an element from the top without popping it, a method for determining the stack size, and several others.
To begin with, to implement the data structure, we define the following class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public class FixedStack<T>{ private T[] items; // stack elements private int count; // number of elements const int n = 10; // default array size
items[count++] = item;
T item = items[–count];
return items[count – 1]; |
This implementation represents a generic class, allowing it to store elements of any type. The elements themselves will actually be stored in the items array. The variable is used to store the actual number of elements added to the stack count.
Two constructors are used to initialize the stack. The constructor without parameters creates an array named items with a length of 10. The constructor with a parameter allows you to manually set the array length.
In the method, Push()we add an element to the array, incrementing the count variable. If the stack (or, in fact, the items array) is already full, we throw an exception.
In the method, Pop()We pop an element from the top of the stack, decrementing the count variable. If there are no elements on the stack, we throw an exception.
Stack usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | static void Main(string[] args){ try { FixedStack<string> stack = new FixedStack<string>(8); // adding four elements
// simply getting the top of the stack without extracting it Console.Read(); |
Schematically, all operations can be expressed as follows:

However, it should be noted that this stack has one significant drawback: if we don’t initially know exactly how many elements the stack will hold—10, 15, 100, 200, etc.—the fixed array length imposes a limitation on stack manipulation. To solve this problem, we need to dynamically change the array length as it grows or shrinks, up to a certain point. So, let’s define the following stack class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | public class Stack<T>{ private T[] items; private int count; const int n = 10; public Stack() public Stack(int length)
public int Count
public T Pop() T item = items[–count];
public T Peek() return items[count – 1];
|
In this case, compared to the previous example, a method has been added to the stack class Resizethat resizes the array to the max value. To do this, the method simply creates a new array and copies the data from the old one into it.
Now, when adding items to the method, Push()we no longer need to check the stack for overflow. If there’s suddenly no more room in the items array, we’ll call the Resize function, which will increase the array by 10 elements:
1 2 3 | if (count == items.Length) |
When deleting an element in the Pop method, we similarly change the size if more than 10 empty cells are formed in the items array.
Here, when adding and removing elements, the number of elements changes by 10. This can be increased or decreased by a factor of two, etc. However, it’s important to remember that initially allocating large amounts of memory will increase the total memory consumed by the application. However, there’s no guarantee that even half of this memory will be used.
At the same time, if you gradually increase/decrease the array size, this will increase the frequency of memory reallocation, which ultimately leads to a decrease in performance.
Thus, the stack has become more flexible in terms of array sizes, but it retains a number of the drawbacks associated with arrays, such as the need to reallocate memory when adding or deleting data, and increased complexity of the computational algorithm. We’ll discuss how to address these drawbacks below.
Explore More IT Terms
#
- Using an integrating factor
- Equations in total differentials
- Bernoulli's equation
- Linear differential equations of the first order
- 10 Mixed C++ Challenges to Test Your Skills
- 11 Tricky Non-Technical Questions to Ask in a Recruiting Interview
- 50 Terms Every Programmer Should Know
- 52 Java Thread Interview Questions
- 7 Levels of Using the Zip Function in Python
- 7 Python Code Bugs You Need to Fix
- 70+ Free Resources for Learning Programming
A
- A Comprehensive Guide to HTML and CSS
- A Comprehensive Guide to MODX CMS
- A computer science question tests your ability to work with IP addresses and network masks
- A Guide to SQL Query Formatting for Beginners
- A/B testing
- Abstract Data Type (ADT)
- AES Encryption Algorithm: How It Works and Where It's Used
- Agile
- Algorithm
- Algorithm Analysis
- Algorithm Complexity-Key Points
- Algorithm complexity: deep parsing O(log n)
- Algorithm vs. Program
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Analysis of graphs and adjacency matrices question
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Applications of microcontrollers: From simple circuits in electronics to complex systems
- Applications of the derivative
- Arduino: How to Program It: Basics for Beginners
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
- C++ Lessons
- Cache
- Calculating Memory Capacity
- Character sets and encodings
- Characteristics of a good Algorithm
- Circular singly linked list
- Coding Tests with Answers
- Combinational Circuits Solved Questions
- Compiler
- Complexity of algorithms-Tutorial
- Computer Science Fundamentals Test with Answers (47 questions)
- Computer science tests with answers (More than 100 Questions)
- Constants
- Creating a Table in HTML
- Creating Forms in HTML
- Creating Lists
- Cybernetics and informatics
- Cycles
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data Structures and Algorithms (DSA)
- Data types vs. Data structures
- Database Tests with Answers
- Deep Learning
- Defining Aliases
- Defining Arrays
- Deque
- Detailed articles and tutorials on PHP
- Developing a Website from Scratch
- Differential Equations
- Differentiation of functions
- Digital data: understand the importance of this asset for businesses.
- Double integrals
- Doubly linked lists
- DSA Tutorial
E
F
G
H
- Handling errors and exceptions
- Heads or Tails? How Probability Theory Is Used in IT
- History of the development of computer science
- Homogeneous equations
- Homogeneous vs. non-homogeneous structures
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
- HTML Quizzes with Answers
I
- if..else construction
- Infinite sequences and series
- Information properties
- Inheritance in Java: A Complete Guide to Principles and Implementation
- Inserting an Image
- Integration of functions
- Interactive Python Tutorial – Learn Programming from Scratch
- Interpreter
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to Networking | Network Fundamentals Part 1
- Introduction to Number Systems (Binary, Octal, Hexadecimal) | Math for CS Foundations #1
- IT Specialist Resume (CV)
J
K
L
M
- Machine Learning
- Machine Learning Basic Tool: NumPy
- Machine Learning Basic Tool: Pandas
- Machine Learning Mathematics
- Mathematics for programmers: what is really needed?
- MD5 encryption algorithm: What is it and why is it needed?
- Microcontroller and Microprocessor - what's the difference?
- ML Engineer: Who They Are, What They Do, How Much They Earn, and How to Become a Neural Network Specialist
- Monte Carlo Simulation: How It Works and What It's For
O
P
- PHP lessons
- Private DNS server and its configuration
- Program code
- Programmer's Dictionary
- Programming
- Programming with pseudocode
- Python Code Formatting Guide: PEP8
- Python for data analysis: how to do it and main libraries
- Python Lessons
- Python Superstar: 5 Ways to Use the * Operator
- Python vs. Julia: Should You Replace Python with Julia?
R
S
- SFML Graphics Library Tutorials
- Sorting Algorithms in Programming: Types, Descriptions, and Comparisons
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Static vs. dynamic data structures
- Statistical analysis: importance for decision making.
- String formatting in Python
- Structure of computer science
- Swift Lessons
- switch/match construct
- Syntax
T
- Terms in programming
- Text and paragraph formatting tags
- The Complete Guide to Bootstrap
- The Complete Guide to JavaScript
- The concept of information and its transmission
- The Future of Python: Key Trends and Insights from Global Researc
- The Infrastructure of Code: A Complete Guide to Repositories for Languages, Frameworks, and Compilers
- The pip package manager in Python
- The role of informatization in the development of society
- Transfers
- Tricky Java Questions Often Asked in Interviews
- Tutorials / Articles
- TypeScript: What It Is and Why Developers Need It
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a data structure?
- What is a GPU in a computer, in simple terms?
- What is a quantum computer: 100,500 problems in one second
- What Is an Algorithm?
- What is Arduino: How it Works and the Platform's Capabilities
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is FizzBuzz Challenge?
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is recursion, recursive and iterative process in programming?
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- What's the difference between x86 and ARM processors?
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview




