Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C
  4. Understanding Stacks and Queues in C: A Beginner's Guide

Understanding Stacks and Queues in C: A Beginner's Guide

Date- Mar 14,2026 94
stacks queues

Overview of Stacks and Queues

Stacks and queues are essential data structures that manage collections of elements in specific ways. A stack follows the Last In First Out (LIFO) principle, meaning the last element added is the first to be removed. On the other hand, a queue operates on the First In First Out (FIFO) principle, where the first element added is the first to be removed. Understanding these concepts is crucial for effective problem-solving in programming, as they are widely used in algorithms and real-world applications.

Prerequisites

  • Basic understanding of C programming
  • Familiarity with functions and pointers
  • Knowledge of dynamic memory allocation
  • Concept of arrays and structures in C

Implementing a Stack in C

A stack can be implemented using an array or a linked list. In this section, we will create a stack using an array.

#include 
#include 

#define MAX 100

struct Stack {
    int top;
    int items[MAX];
};

struct Stack* createStack() {
    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    stack->top = -1;
    return stack;
}

int isFull(struct Stack* stack) {
    return stack->top == MAX - 1;
}

int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

void push(struct Stack* stack, int item) {
    if (isFull(stack)) {
        printf("Stack is full!\n");
        return;
    }
    stack->items[++stack->top] = item;
}

int pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty!\n");
        return -1;
    }
    return stack->items[stack->top--];
}

void display(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty!\n");
        return;
    }
    for (int i = stack->top; i >= 0; i--) {
        printf("%d \n", stack->items[i]);
    }
}

int main() {
    struct Stack* stack = createStack();
    push(stack, 10);
    push(stack, 20);
    push(stack, 30);
    printf("Elements in stack:\n");
    display(stack);
    printf("Popped element: %d\n", pop(stack));
    return 0;
}

This code implements a stack using an array. Here's a breakdown of the code:

  • #include and #include : Import standard libraries for input/output and memory allocation.
  • #define MAX 100: Define a constant for the maximum stack size.
  • struct Stack {...}: Declare a structure to represent the stack, including an array and a top index.
  • createStack: Allocate memory for a new stack and initialize the top index.
  • isFull: Check if the stack is full.
  • isEmpty: Check if the stack is empty.
  • push: Add an element to the stack if it is not full.
  • pop: Remove and return the top element of the stack if it is not empty.
  • display: Print all elements in the stack.
  • main: Create a stack, push some elements, display them, and pop an element.

Implementing a Queue in C

Queues can also be implemented using arrays or linked lists. Here, we'll implement a queue using an array.

#include 
#include 

#define MAX 100

struct Queue {
    int items[MAX];
    int front, rear;
};

struct Queue* createQueue() {
    struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
    queue->front = -1;
    queue->rear = -1;
    return queue;
}

int isFull(struct Queue* queue) {
    return (queue->rear + 1) % MAX == queue->front;
}

int isEmpty(struct Queue* queue) {
    return queue->front == -1;
}

void enqueue(struct Queue* queue, int item) {
    if (isFull(queue)) {
        printf("Queue is full!\n");
        return;
    }
    if (isEmpty(queue)) {
        queue->front = 0;
    }
    queue->rear = (queue->rear + 1) % MAX;
    queue->items[queue->rear] = item;
}

int dequeue(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty!\n");
        return -1;
    }
    int item = queue->items[queue->front];
    if (queue->front == queue->rear) {
        queue->front = queue->rear = -1;
    } else {
        queue->front = (queue->front + 1) % MAX;
    }
    return item;
}

void display(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty!\n");
        return;
    }
    for (int i = queue->front; i != queue->rear; i = (i + 1) % MAX) {
        printf("%d \n", queue->items[i]);
    }
    printf("%d \n", queue->items[queue->rear]);
}

int main() {
    struct Queue* queue = createQueue();
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    printf("Elements in queue:\n");
    display(queue);
    printf("Dequeued element: %d\n", dequeue(queue));
    return 0;
}

This code implements a queue using an array. Here’s a detailed explanation:

  • struct Queue {...}: Define a structure to represent the queue, including an array and front/rear indices.
  • createQueue: Allocate memory for a new queue and initialize front and rear indices.
  • isFull: Determine if the queue is full using circular logic.
  • isEmpty: Check if the queue is empty.
  • enqueue: Add an element to the queue if it is not full, updating the rear index.
  • dequeue: Remove and return the front element of the queue if it is not empty, updating the front index.
  • display: Print all elements from front to rear in the queue.
  • main: Create a queue, enqueue some elements, display them, and dequeue an element.

Applications of Stacks and Queues

Stacks and queues are widely used in various applications:

  • Stacks: Used in function calling, expression parsing, backtracking algorithms, and undo mechanisms in applications.
  • Queues: Used in scheduling, breadth-first search algorithms, print spooling, and handling requests in servers.

Best Practices and Common Mistakes

When working with stacks and queues, consider the following best practices:

  • Always check for overflow and underflow conditions when pushing or popping from stacks or enqueueing/dequeueing from queues.
  • Use dynamic memory allocation judiciously to avoid memory leaks.
  • Prefer using linked lists for stacks and queues when the maximum size is not known upfront.
  • Keep operations O(1) for both stacks and queues to maintain efficiency.

Conclusion

In this article, we explored stacks and queues, two fundamental data structures in C programming. We learned how to implement them using arrays and their practical applications. Understanding these structures will help you enhance your problem-solving skills and improve your programming efficiency. Remember to apply best practices to avoid common pitfalls when using these data structures.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Searching Algorithms in C: Linear and Binary Search Explained
Mar 14, 2026
Understanding Unions in C Programming: A Comprehensive Guide
Mar 12, 2026
Understanding Pointers in C Programming: A Comprehensive Guide
Mar 11, 2026
Understanding Arrays in C Programming: A Beginner's Guide
Mar 10, 2026
Previous in C
Mastering Linked Lists in C: A Comprehensive Guide
Next in C
Mastering Sorting Algorithms in C: Bubble, Selection, and Inserti…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,925 views
  • 2
    Error-An error occurred while processing your request in .… 11,259 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 216 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,449 views
  • 5
    Mastering Unconditional Statements in C: A Complete Guide … 21,488 views
  • 6
    Mastering JavaScript Error Handling with Try, Catch, and F… 147 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,217 views

On this page

More in C

  • Mastering Unconditional Statements in C: A Complete Guide wi… 21488 views
  • Understanding C: A Complete Guide with Examples 5147 views
  • Mastering Unconditional Statements in C: A Complete Guide wi… 4213 views
  • Mastering 2-D Arrays in C: A Complete Guide with Examples 3932 views
  • Introduction to C: A Step-by-Step Guide with Examples 3581 views
View all C posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor