
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain linear data structure queue in C language
Data structure is collection of data organized in a structured way. It is divided into two types as explained below −
Linear data structure − Data is organized in a linear fashion. For example, arrays, structures, stacks, queues, linked lists.
Nonlinear data structure − Data is organized in a hierarchical way. For example, Trees, graphs, sets, tables.
Queue
It is a linear data structure, where the insertion is done at rear end and the deletion is done at the front end.
The order of queue is FIFO – First In First Out
Operations
- Insert – Inserting an element into a queue.
- Delete – Deleting an element from the queue.
Conditions
Queue over flow − Trying to insert an element into a full queue.
Queue under flow − Trying to delete an element from an empty queue.
Algorithms
Given below is an algorithm for insertion ( ) −
- Check for queue overflow.
if (r==n) printf ("Queue overflow")
- Otherwise, insert an element in to the queue.
q[r] = item r++
Given below is an algorithm for deletion ( ) −
- Check for queue under flow.
if (f==r) printf ("Queue under flow")
- Otherwise, delete an element from the queue.
item = q[f] f++
Given below is an algorithm for display ( ) −
- Check whether the queue is empty or not.
if (f==r) printf("Queue is empty")
- Otherwise, print all the elements from ‘f’ to ‘r’.
for(i=f; i<r; i++) printf ("%d", q[i]);
Program
Following is the C program for implementation of queue by using arrays −
#include<limits.h> #include<stdio.h> #include <stdlib.h> struct Queue { int front, rear, size; unsigned capacity; int* array; }; struct Queue* createQueue(unsigned capacity){ struct Queue* queue = (struct Queue*)malloc( sizeof(struct Queue)); queue->capacity = capacity; queue->front = queue->size = 0; queue->rear = capacity - 1; queue->array = (int*)malloc( queue->capacity * sizeof(int)); return queue; } //if queue is full int isFull(struct Queue* queue){ return (queue->size == queue->capacity); } // Queue is empty int isEmpty(struct Queue* queue){ return (queue->size == 0); } void Equeue(struct Queue* queue, int item){ if (isFull(queue)) return; queue->rear = (queue->rear + 1) % queue->capacity; queue->array[queue->rear] = item; queue->size = queue->size + 1; printf("%d entered into queue
", item); } int Dqueue(struct Queue* queue){ if (isEmpty(queue)) return INT_MIN; int item = queue->array[queue->front]; queue->front = (queue->front + 1) % queue->capacity; queue->size = queue->size - 1; return item; } // Function to get front of queue int front(struct Queue* queue){ if (isEmpty(queue)) return INT_MIN; return queue->array[queue->front]; } // Function to get rear of queue int rear(struct Queue* queue){ if (isEmpty(queue)) return INT_MIN; return queue->array[queue->rear]; } int main(){ struct Queue* queue = createQueue(1000); Equeue(queue, 100); Equeue(queue, 200); Equeue(queue, 300); Equeue(queue, 400); printf("%d is deleted element from queue
", Dqueue(queue)); printf("1st item in queue is %d
", front(queue)); printf("last item in queue %d
", rear(queue)); return 0; }
Output
When the above program is executed, it produces the following result −
100 entered into queue 200 entered into queue 300 entered into queue 400 entered into queue 100 is deleted element from queue 1st item in queue is 200 last item in queue 400
- Related Articles
- Circular Queue Data Structure in C++
- Explain the Difference Between Linear and Non-linear Data Structure
- Queue Data Structure in Javascript
- Explain queue by using linked list in C language
- Linear Probing in Data Structure
- Basic Operations for Queue in Data Structure
- Explain the accessing of structure variable in C language
- Explain deleting an element in a queue by using C language
- Differences between stack and queue data structure
- Explain bit field in C language by using structure concept
- Java Program to Implement the queue data structure
- Golang program to implement the Queue data structure
- Structure declaration in C language
- Explain top-down design and structure chart of function in C language
- Explain the dynamic memory allocation of pointer to structure in C language
