SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Implementation of Queue
Queue implementation which has Enqueue, dequeue, check isqueueempty(), check isqueuefull() functions.
Implementation:
#include
#include
#define MAX 1000000 /* Structure of queue */ struct queue { int front,rear; int items[MAX]; }; /* Function to check whether the queue is empty or not */ int isempty(struct queue *q) { return q->front >= q->rear; } /* Function to enqueue elements at the rear */ void insert(struct queue *q,int data) { if(q->rear==MAX-1) { printf("Queue Full\n"); getchar(); exit(0); } q->rear++; q->items[q->rear]=data; } /* function to print the elements in the queue */ void printqueue(struct queue *q) { int visit=q->front+1; while(visit<=q->rear) { printf("%d ",q->items[visit]); visit++; } printf("\n"); } /* Function to dequeue an element from the front */ int dequeue(struct queue *q) { int data; if(isempty(&q)) { printf("Queue is empty\n"); getchar(); exit(0); } data=q->items[q->front]; q->front=q->front+1; return data; } int main(void) { struct queue q; int data; /* initialize the front and rear pointer to -1 denoting that the queue is empty */ q.front=-1,q.rear=-1; /* enqueue elements */ insert(&q,1); insert(&q,2); /* Print the elements of the queue */ printqueue(&q); /* store the dequeud data */ data=dequeue(&q); /* print the deleted item */ printf("%d is deleted\n",data); /* print the queue */ printqueue(&q); return 0; }
PREVIOUS
NEXT
HOME