• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Write a C Program to Implement Stack using Array

Aniruddha Chaudhari/33717/4
C / C++Code

The stack is first in last out data structure to store the elements. Elements are accessed by push pop operations. Push operation adds a new element in the stack at the top, whereas pop operation deletes the topmost element from the stack. The stack can be implemented using array. Stack using array is the easiest way to understand, how stack actual work.

To implement the stack using array, we need to keep track of the topmost element in the array.

In this program, we have written two functions namely push, and pop that will work as push, pop operation in the stack using array.

Difficulty Level: Low

Operations to be performed on Stack using Array:

Push Operation on Stack: 

Add the new element at the top of the stack.

Pop Operation on Stack:

Read and delete topmost elements from the stack.

Stack Overflow:
If the number of elements in the stack exceeds, a stack overflow occurs.

Stack Underflow: 

While performing the pop operation on the stack, if there is no more element to pop, stack underflow occurs.

Code to implement Stack using Array

#include<stdio.h>

int nTop=-1;
int *pStack = NULL;
/*
nTop: To refer topmost element in array.

pStack: pointer to array that 
can be implemented as stack pointer
*/
/*
Push new element 
at the top of linked list
*/
void push(int n)
{
  printf("\nPush element: %d", n);
  if(nTop>9)
  printf("Overflow");
  else
  {
    nTop++;
    pStack[nTop] = n;
  }
}
/*
Pop topmost element 
from stack
*/

void pop()
{
  printf("\nPop topmost element");
  if(nTop<0)
  printf("\nUnderflow");
  else
  {
    pStack[nTop] = -1;
    nTop--;
   }
}

void DisplayStack()
{
  int i=0;
  if(nTop<0)
  printf("\nStack is empty");
  else
  {
    printf("\nDisplay Stack: ");
    for(; i<=nTop;i++)
    printf("%d ", pStack[i]);
  }
}

int main()
{
  int nSize=10;
  /*
    allocate dynamic memory of size 10 int
    to store 10 elements 
  */
  pStack = (int *)malloc(sizeof(int)*10); 
	
  push(10);
  push(12);
  DisplayStack();
  pop();
  DisplayStack();
  push(45);
  push(24);
  push(12);
  DisplayStack();
  pop();
  pop();
  DisplayStack();
}

Output:

Push element: 10
Push element: 12
Display Stack: 10 12
Pop topmost element
Display Stack: 10
Push element: 45
Push element: 24
Push element: 12
Display Stack: 10 45 24 12
Pop topmost element
Pop topmost element
Display Stack: 10 45

The stack can be implemented from the linked list, but stack using an array is the easiest way to understand and implement. I will write code to implement a stack using a linked list in the upcoming article.

Other similar Program: Implement Linked List in C

For any questions, feel free to use the comment section below.

Stay tuned!

arraycppstack
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Drehu Medhan
    April 28, 2016 at 9:28 pm

    Hello,
    Nice and good stuff 🙂

    • Reply
      Aniruddha Chaudhari
      March 9, 2019 at 10:30 am

      Thanks, Drehu! 🙂

  • Reply
    Anoushka
    March 15, 2022 at 9:47 am

    Thanks for the code. It helped me a lot!

    • Reply
      Aniruddha Chaudhari
      March 20, 2022 at 10:55 pm

      You’re welcome!

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard