Created
October 10, 2019 03:38
-
-
Save easleyschool/1a7212428cca933287cba42185a819d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Below program is written in "C" language. This is a sample implementation of simple Queue. | |
*/ | |
#include<stdio.h> | |
#include<process.h> | |
#include<stdlib.h> | |
#define SIZE 10 | |
int a[SIZE]; | |
int rear; //rear is same as tail; front - same as head | |
int rear = front = -1; | |
// declaring enqueue, dequeue and display functions | |
void enqueue(int x); | |
int dequeue(); | |
void display(); | |
// function enqueue - to add data to queue | |
void enqueue(int x) | |
{ | |
if(front == -1) { | |
front++; | |
} | |
if( rear == SIZE-1) | |
{ | |
printf( "Queue is full"); | |
} | |
else | |
{ | |
a[++rear] = x; | |
} | |
} | |
// function dequeue - to remove data from queue | |
int dequeue() | |
{ | |
return a[++front]; // following approach [B], explained above | |
} | |
// function to display the queue elements | |
void display() | |
{ | |
int i; | |
for( i = front; i <= rear; i++) | |
{ | |
printf( a[i] ); | |
} | |
} | |
// the main function | |
int main() | |
{ | |
enqueue(10); | |
enqueue(100); | |
enqueue(1000); | |
enqueue(1001); | |
enqueue(1002); | |
dequeue(); | |
enqueue(1003); | |
dequeue(); | |
dequeue(); | |
enqueue(1004); | |
display(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment