From a72a2d1744a8c100965dd172610c8fe6f3cea87e Mon Sep 17 00:00:00 2001 From: NeelavaChatterjee Date: Thu, 1 Nov 2018 19:58:50 +0530 Subject: [PATCH] queue.c: Add queue in C Closes https://github.com/NITSkmOS/Algorithms/issues/56 --- README.md | 2 +- queue/C/queue.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 queue/C/queue.c diff --git a/README.md b/README.md index d0009fb1..3d64cdf4 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/queue/C/queue.c b/queue/C/queue.c new file mode 100644 index 00000000..26696743 --- /dev/null +++ b/queue/C/queue.c @@ -0,0 +1,61 @@ +#include + +// 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