Last active
September 6, 2018 06:34
-
-
Save meldsza/637685521c53dbf772bb3b4e931033e8 to your computer and use it in GitHub Desktop.
QUEUE
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
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX_SIZE 3 | |
struct{ | |
int front; | |
int rear; | |
int a[MAX_SIZE]; | |
int size; | |
} typedef Queue; | |
void insert(Queue*, int); | |
int delete(Queue*); | |
int is_empty(Queue*); | |
int is_full(Queue*); | |
void display(Queue*); | |
int circ(int a){ | |
return a%MAX_SIZE; | |
} | |
void init(Queue*); | |
//end of header | |
void init(Queue* q) | |
{ | |
q->front = 0; | |
q->rear = -1; | |
q->size = 0; | |
} | |
void insert(Queue* q, int item) | |
{ | |
q->size++; | |
q->rear = circ(++q->rear); | |
q->a[q->rear] = item; | |
} | |
int delete(Queue* q) | |
{ | |
q->size--; | |
int item; | |
item = q->a[q->front++]; | |
q->front = circ(q->front); | |
return item; | |
} | |
void display(Queue* q) | |
{ | |
int i; | |
if(is_empty(q)) | |
{ | |
puts("Queue is empty"); | |
} | |
else | |
{ | |
puts("Elements in the queue"); | |
for(i=0;i<q->size;i++) | |
printf("%d ",q->a[circ(q->front+i)]); | |
puts(""); | |
} | |
} | |
int is_empty(Queue* q) | |
{ | |
return q->size <=0; | |
} | |
int is_full(Queue* q) | |
{ | |
return q->size >= MAX_SIZE; | |
} | |
int main() | |
{ | |
int ex=0, choice, item; | |
Queue q; | |
init(&q); | |
while(!ex) | |
{ | |
puts("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit"); | |
scanf("%d",&choice); | |
switch(choice) | |
{ | |
case 1: | |
if(is_full(&q)) | |
{ | |
puts("Queue is full"); | |
} | |
else | |
{ | |
puts("Enter the element"); | |
scanf("%d", &item); | |
insert(&q,item); | |
} | |
break; | |
case 2: | |
if(is_empty(&q)) | |
{ | |
puts("Queue is empty"); | |
} | |
else | |
{ | |
printf("Deleted element %d \n", delete(&q)); | |
} | |
break; | |
case 3: | |
display(&q); | |
break; | |
default: | |
ex=1; | |
} | |
} | |
return 0; | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX_SIZE 5 | |
struct | |
{ | |
int front; | |
int rear; | |
int a[MAX_SIZE]; | |
} typedef Queue; | |
void init(Queue*);//initializes the queue | |
int delete(Queue*, int);//deletes element from queue | |
void display(Queue*);//displays queue | |
void insert(Queue*, int);//inserts an element into queue | |
int is_full(Queue*);//queue is full or not | |
int is_empty(Queue*);//queue is empty or not | |
//end of header | |
/** | |
* @function init | |
* @param q(Queue*) | |
* Initialises the queue | |
*/ | |
void init(Queue* q) | |
{ | |
q->front = 0; | |
q->rear =-1; | |
} | |
/** | |
* @function delete | |
* @param q(Queue*) | |
* @param item(int) | |
* @returns (int) Deletion was sucessfull or not | |
* | |
*/ | |
int delete(Queue* q, int item) | |
{ | |
int i=q->front,j; | |
while(q->a[i]!=item && (i<=q->rear)) | |
i++; | |
if(i==(q->rear+1)) | |
return 0; | |
for(j=i;j<=q->rear;j++) | |
{ | |
q->a[j]=q->a[j+1]; | |
} | |
q->rear--; | |
return 1; | |
} | |
void insert(Queue* q, int item) | |
{ | |
int j=q->rear; | |
while((j >= q->front)&&(item>q->a[j])) | |
{ | |
q->a[j+1]= q->a[j]; | |
j--; | |
} | |
q->a[j+1] = item; | |
q->rear++; | |
} | |
int is_empty(Queue* q) | |
{ | |
return q->front > q->rear; | |
} | |
int is_full(Queue* q) | |
{ | |
return q->rear >= (MAX_SIZE-1); | |
} | |
void display(Queue* q) | |
{ | |
int i; | |
if(is_empty(q)) | |
{ | |
puts("Queue is empty"); | |
} | |
else | |
{ | |
for(i=q->front;i<= q->rear;i++) | |
printf("%d ", q->a[i]); | |
puts(""); | |
} | |
} | |
int main() | |
{ | |
int choice, ex=0, item; | |
Queue q; | |
init(&q); | |
puts("1 Insert\n2 Delete\n3 Display\n4 Exit"); | |
while(!ex) | |
{ | |
puts("Enter your choice"); | |
scanf("%d",&choice); | |
switch(choice) | |
{ | |
case 1: | |
puts("Enter value"); | |
scanf("%d", &item); | |
if(is_full(&q)) | |
{ | |
puts("Queue overflow"); | |
} | |
else | |
{ | |
insert(&q,item); | |
} | |
break; | |
case 2: | |
if(is_empty(&q)) | |
{ | |
puts("There are no elements to delete"); | |
} | |
else | |
{ | |
puts("Enter value to delete"); | |
scanf("%d", &item); | |
if(!delete(&q,item)) | |
printf("%d not found\n", item); | |
} | |
break; | |
case 3: | |
display(&q); | |
break; | |
default: | |
ex=1; | |
} | |
} | |
return 0; | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX_SIZE 5 | |
struct{ | |
int front; | |
int rear; | |
int a[MAX_SIZE]; | |
}typedef Queue; | |
void insert(Queue*,int); | |
int delete(Queue*); | |
void display(Queue*); | |
void initialize_Queue(Queue*); | |
int is_full(Queue*); | |
int is_empty(Queue*); | |
//end of header | |
int main() | |
{ | |
int choice,item,ex=0; | |
Queue q; | |
initialize_Queue(&q); | |
while(!ex) | |
{ | |
puts("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit"); | |
scanf("%d",&choice); | |
switch(choice) | |
{ | |
case 1: | |
if(is_full(&q)) | |
{ | |
puts("Queue is full"); | |
} | |
else | |
{ | |
puts("Enter the element to be inserted"); | |
scanf("%d",&item); | |
insert(&q,item); | |
} | |
break; | |
case 2: | |
if(is_empty(&q)) | |
{ | |
puts("Queue is empty"); | |
} | |
else | |
{ | |
printf("%d deleted\n",delete(&q)); | |
} | |
break; | |
case 3: | |
display(&q); | |
break; | |
default: | |
ex = 1; | |
} | |
} | |
return 0; | |
} | |
void initialize_Queue(Queue* q) | |
{ | |
q->rear = -1; | |
q->front = 0; | |
} | |
int is_full(Queue* q) | |
{ | |
return q->rear == (MAX_SIZE -1); | |
} | |
int is_empty(Queue* q) | |
{ | |
return q->front > q->rear ; | |
} | |
void insert(Queue* q, int item) | |
{ | |
q->a[++q->rear] = item; | |
} | |
int delete(Queue*q) | |
{ | |
return q->a[q->front++]; | |
} | |
void display(Queue* q) | |
{ | |
int i; | |
if(is_empty(q)) | |
{ | |
puts("Queue is empty"); | |
} | |
else | |
{ | |
puts("Elements in the queue are"); | |
for(i=q->front;i<=q->rear;i++) | |
printf("%d ",q->a[i]); | |
puts(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment