Table of contents
1.
Introduction
2.
Adding an Element onto the Stack (Push Operation)
2.1.
C
3.
Deletion of an Element from a Stack (Pop Operation)
3.1.
C
4.
Visiting Each Element of the Stack (Peek Operation)
4.1.
C
5.
Implementation of Stack Using Array in C Language
5.1.
C
6.
Frequently Asked Questions
6.1.
What happens if I try to push an element when the stack is full?
6.2.
Can I peek at elements other than the top element in the stack?
6.3.
Is it possible to increase the size of the stack once it's full?
7.
Conclusion
Last Updated: Mar 30, 2024
Easy

Stack Implementation Using Array in C

Introduction

Stacks operate on a simple principle: last in, first out, much like a stack of books. You can only add or remove the book at the top. In coding, this concept is powerful for managing data in a structured way.

Stack Implementation Using Array in C

 We're focusing on implementing stacks using arrays in C, a fundamental approach in programming. This article will help you learn adding, removing, and peeking at elements in the stack with clear examples and code in C. 

Adding an Element onto the Stack (Push Operation)

Adding an element to a stack is like adding a book on top of a stack of books. In programming, we call this action a "push operation." In C language, we use arrays to create a stack & then add elements to it.

Here's how you can do it:

  • Check if the stack is full: Before adding a new element, we need to see if there's space in the stack.
     
  • Add the element: If there's space, we add the new element to the top of the stack.
     
  • Update the top position: After adding the new element, we move the top position up by one to point to the new top element.

Let's see this in action with some code:

  • C

C

#include <stdio.h>

#define MAX_SIZE 5  // Setting the maximum size of the stack

int stack[MAX_SIZE];  // The stack

int top = -1;  // This tells us the top position of the stack, -1 means the stack is empty

// Function to add an element to the stack

void push(int element) {

   if (top < MAX_SIZE - 1) {  // Check if the stack is not full

       top++;  // Move the top up

       stack[top] = element;  // Add the new element at the top

       printf("%d pushed to stack\n", element);

   } else {

       printf("Stack is full. Cannot push %d into it\n", element);

   }

}

int main() {

   push(10);  // Let's add some elements to the stack

   push(20);

   push(30);

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

10 pushed to stack
20 pushed to stack
30 pushed to stack


In this code, we first define our stack with a maximum size. We then have a function called push that adds an element to the top of the stack, provided there's space. The main function shows how to use the push function to add elements.

Deletion of an Element from a Stack (Pop Operation)

Removing an element from a stack is known as a "pop operation". It's like taking the top book off a stack of books. In C language, we do this with a few steps:

  • Check if the stack is empty: We can't remove an element from an empty stack, so we first check if there's anything to remove.
     
  • Remove the element: If the stack isn't empty, we remove the element from the top of the stack.
     
  • Update the top position: After removing the element, we decrease the top position by one, to move it down to the new top element.
     

Here's a code example to show how this works:

  • C

C

#include <stdio.h>
#define MAX_SIZE 5

int stack[MAX_SIZE];
int top = -1;

// Function to add an element to the stack
void push(int element) {
if (top < MAX_SIZE - 1) { // Check if the stack is not full
top++; // Move the top up
stack[top] = element; // Add the new element at the top
printf("%d pushed to stack\n", element);
} else {
printf("Stack is full. Cannot push %d into it\n", element);
}
}


// Function to remove an element from the stack
int pop() {
if (top >= 0) { // Check if the stack is not empty
int poppedElement = stack[top]; // Get the top element
top--; // Move the top down
printf("%d popped from stack\n", poppedElement);
return poppedElement;
} else {
printf("Stack is empty. Cannot pop.\n");
return -1; // Return -1 to indicate the stack is empty
}
}

int main() {
// Assume we already have some elements in the stack
push(10);
push(20);
push(30);

pop(); // Now let's remove an element
pop(); // And another one

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output


10 pushed to stack
20 pushed to stack
30 pushed to stack
30 popped from stack
20 popped from stack


In this code, we have a pop function that removes the top element from the stack if it's not empty. The main function shows how you can use pop to remove elements. After popping, it prints the removed element.

Visiting Each Element of the Stack (Peek Operation)

The "peek operation" in a stack allows us to see the top element without removing it. It's like checking the book at the top of a stack without taking it off. This is really useful when you just want to know what's at the top without changing the stack.

Here's how it's done:

  • Check if the stack is empty: Just like with pop, we can't peek at an empty stack.
     
  • Show the top element: If the stack isn't empty, we can see what the top element is.
     

Let's look at some C code that does this:

  • C

C

#include <stdio.h>
#define MAX_SIZE 5

int stack[MAX_SIZE];
int top = -1;

// Function to peek at the top element of the stack
int peek() {
if (top >= 0) { // Check if the stack is not empty
printf("Top element is %d\n", stack[top]);
return stack[top]; // Return the top element
} else {
printf("Stack is empty. Cannot peek.\n");
return -1; // Return -1 to indicate the stack is empty
}
}
// Function to add an element to the stack
void push(int element) {
if (top < MAX_SIZE - 1) { // Check if the stack is not full
top++; // Move the top up
stack[top] = element; // Add the new element at the top
printf("%d pushed to stack\n", element);
} else {
printf("Stack is full. Cannot push %d into it\n", element);
}
}


// Function to remove an element from the stack
int pop() {
if (top >= 0) { // Check if the stack is not empty
int poppedElement = stack[top]; // Get the top element
top--; // Move the top down
printf("%d popped from stack\n", poppedElement);
return poppedElement;
} else {
printf("Stack is empty. Cannot pop.\n");
return -1; // Return -1 to indicate the stack is empty
}
}

int main() {
// Assume we already have some elements in the stack
push(10);
push(20);
push(30);

peek(); // Let's see what's on top without removing it

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

10 pushed to stack
20 pushed to stack
30 pushed to stack
Top element is 30

Implementation of Stack Using Array in C Language

To put everything together, let's implement a stack using an array in C. We'll include the functions for pushing (adding), popping (removing), and peeking (viewing) elements. This way, you'll have a complete stack implementation that you can use and modify for your programs

Here's the full code:

  • C

C

#include <stdio.h>
#define MAX_SIZE 5 // Define the maximum size of the stack

int stack[MAX_SIZE]; // The stack
int top = -1; // Top of the stack, -1 indicates the stack is empty

// Function to add an element to the stack
void push(int element) {
if (top < MAX_SIZE - 1) { // Check if there's space in the stack
top++; // Move the top up
stack[top] = element; // Place the element at the new top
printf("%d pushed to stack\n", element);
} else {
printf("Stack is full!\n");
}
}

// Function to remove the top element from the stack
int pop() {
if (top >= 0) { // Check if the stack isn't empty
int poppedElement = stack[top]; // Get the top element
top--; // Move the top down
printf("%d popped from stack\n", poppedElement);
return poppedElement;
} else {
printf("Stack is empty!\n");
return -1; // Indicates stack is empty
}
}

// Function to view the top element of the stack without removing it
int peek() {
if (top >= 0) { // Ensure the stack isn't empty
printf("Top element: %d\n", stack[top]);
return stack[top];
} else {
printf("Stack is empty!\n");
return -1; // Indicates stack is empty
}
}

int main() {
push(10); // Adding elements to the stack
push(20);
push(30);

peek(); // Viewing the top element
pop(); // Removing the top element
pop(); // Removing another element

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

10 pushed to stack
20 pushed to stack
30 pushed to stack
Top element: 30
30 popped from stack
20 popped from stack


In this implementation, the push function adds an element to the stack if there's space. The pop function removes the top element if the stack isn't empty. The peek function lets you see the top element without removing it. The main function shows how these operations work together.

This code provides a basic but complete stack implementation using arrays in C. You can build on this foundation for more complex applications.

Frequently Asked Questions

What happens if I try to push an element when the stack is full?

If you try to push an element onto a full stack, the operation won't be successful, and you'll typically see a message indicating that the stack is full. The stack's state won't change.

Can I peek at elements other than the top element in the stack?

The peek operation is designed to only view the top element of the stack. To access other elements, you would need to pop the elements above it first. However, you could modify the stack implementation to allow direct access to any element if necessary.

Is it possible to increase the size of the stack once it's full?

In this static array implementation, the size is fixed. However, you can create a dynamic stack using pointers and dynamic memory allocation (using malloc and realloc in C) to allow the stack size to grow as needed.

Conclusion

In this article, we've learned how to implement a stack using an array in the C programming language. We started with the basics of adding an element to the stack with the push operation, followed by removing elements using the pop operation, and viewing the top element with the peek operation. We wrapped up with a complete example that brought these operations together into a fully functioning stack.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass