Skip to content

Commit

Permalink
queue.c: Add queue in C
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
NeelavaChatterjee committed Dec 1, 2018
1 parent 52bd1ad commit d62a1f9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This repository contains examples of various algorithms written on different pro

| Data Structure | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | [:octocat:](queue/C) | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:octocat:](stack/C ) | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | [:octocat:](linked_list/Java) | [:octocat:](linked_list/Python) |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | [:octocat:](avl_tree/Cpp) | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |
Expand Down
61 changes: 61 additions & 0 deletions queue/C/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<stdio.h>

// Queue structure
struct queue {
int arr[10];
int front, end;
};

// Initialize the queue
void init(struct queue *st) {
st->front=0;
st->end=0;
}

// Method to check if the queue st is full or empty
void check(struct queue *st) {
if(st->front==0 && st->end==0)
printf("The queue is empty!!!!\n");
else if(st->end==10)
printf("The queue is full!!!!\n");
}

// Method to insert elements in the queue st
void enqueue(struct queue *st, int ele) {
check(st);
st->arr[(st->end)++]=ele;
}

// Method to remove first element from the queue st
int dequeue(struct queue *st) {
check(st);
return st->arr[(st->front)++];
}

// Method to show elements in the queue st
void show(struct queue st) {
for(int i=st.front; i<st.end; i++)
printf("%d",st.arr[i]);
}

// Driver method
void main(){
struct queue st;
init(&st);
for(int i=1; i<9; i++)
enqueue(&st, i);
show(st);
putchar('\n');
printf("%d\n", dequeue(&st));
enqueue(&st, 6);
show(st);
putchar('\n');
enqueue(&st, 9);
show(st);
putchar('\n');
printf("%d\n", dequeue(&st));
printf("%d\n", dequeue(&st));
printf("%d\n", dequeue(&st));

putchar('\n');
}

0 comments on commit d62a1f9

Please sign in to comment.