chore(C): add circular queue (#599)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/594/head
Shyam-12 2021-10-25 18:19:58 +05:30 committed by GitHub
parent e8e60ed18e
commit 998be025fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 0 deletions

View File

@ -34,6 +34,7 @@
## Queues
- [Double Ended Queue using array](queues/double-ended-queue-using-array.c)
- [Circular Queue using array](queues/circular-queue-using-array.c)
## Sorting

View File

@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX 10 // size of the array is defined
// global variable
// using front and rear to keep track of the elements in the queue
int front = -1, rear = -1, circQueue[MAX];
// function to insert element in the queue
void enqueue(){
int num;
printf("Enter the number to be inserted: ");
scanf("%d", &num);
// this means the queue is full and no more element can be inserted
if(rear == MAX-1){
printf("Overflow");
}
// queue is empty
else if(front == -1 && rear == -1){
front = 0;
rear = 0;
}
// reached the end and the beginning index is empty
else if(rear == MAX - 1 && front != 0){
rear = 0;
}
else {
rear += 1;
}
//inserting the element at the rear position
circQueue[rear] = num;
}
// function to delete an element from the queue
void dequeue(){
// this means the queue is empty and you cannot take out more elements out of it
if(front == -1){
printf("Underflow");
}
// reached the last element
else if(front == rear){
front = -1;
rear = -1;
}
// reached end of the array since it is circular so front = 0 again.
else if(front == MAX - 1){
front = 0;
}
else {
front += 1;
}
}
// to print the queue
void display(){
int i;
if(front == -1 || rear == -1)
{
printf("\nEmpty queue\n");
}
else {
printf("The queue is: ");
for (i = front; i <= rear; i++){
printf("%d ", circQueue[i]);
}
printf("\n");
}
}
int main()
{
// asking the user for the choice
int choice;
while(1){
printf("Enter your choice: \n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit\n");
scanf("%d", &choice);
switch(choice){
case 1:
enqueue(); // if user inserts one it will call the enqueue function
break;
case 2:
dequeue(); // if user inserts two it will call the dequeue function
break;
case 3:
display(); //if user inserts three it will print the result
break;
case 4:
return 0; // exits from the program
default:
printf("Enter valid choice");
break;
}
}
return 0;
}
/*
Sample Input:
1
3
1
3
1
3
2
3
Sample Output:
Enter the number to be inserted: 3
The queue is: 3
Enter the number to be inserted: 5
The queue is: 5 3
Enter the number to be inserted: 7
The queue is: 7 5 3
The queue is: 7 5
Time Complexity of Insertion, Deletion: O(1) that is constant
To run the program simply copy the code in a notepad or code editor then save it with extension .c and run it in the terminal using gcc file_name.c or simply run in it in a code editor.
*/

View File

@ -1,6 +1,7 @@
# Java
## Arrays
- [Counting Inversions](arrays/counting-inversions.java)
- [Kadanes Algorithm](arrays/kadanes-algorithm.java)
- [Left Rotation](arrays/left-rotation.java)
@ -13,9 +14,11 @@
- [Merge Without Extra Space](arrays/merge-without-extra-space.java)
## Graphs
- [Dijkstras](graphs/Dijkstras.java)
## Linked Lists
- [Circular](linked-lists/circular.java)
- [Clone Linked List](linked-lists/clone-linkedlist.java)
- [Doubly](linked-lists/doubly.java)
@ -24,20 +27,24 @@
- [Fold Linked List](linked-lists/fold-linked-list.java)
## Maths
- [Factorial](Maths/factorial_using_big_integer.java)
- [Catalan Numbers](Maths/catalan-numbers.java)
- [Nth Geek Onacci Number](Maths/nth-geek-onacci-number.java)
## Queues
- [Circular Queue using Linked List](queues/circular-queue-linked-list.java)
- [Queue using Linked List](queues/queue-linked-list.java)
- [Priority Queue using Array](queues/priority-queue-array.java)
## Scheduling
- [Multi-Level Queue Scheduling](scheduling/multi-level-queue-scheduling.java)
- [Round Robin](scheduling/round-robin.java)
## Searching
- [Binary Search](searching/binary-search.java)
- [Jump Search](searching/jump-search.java)
- [Linear Search](searching/linear-search.java)
@ -46,6 +53,7 @@
- [Interpolation Search](searching/interpolation-search.java)
## Sorting
- [Bubble Sort](sorting/bubble-sort.java)
- [Counting Sort](sorting/counting-sort.java)
- [Heap Sort](sorting/heap-sort.java)
@ -57,6 +65,7 @@
- [Cyclic Sort](sorting/cyclic-sort.java)
## Stacks
- [Balanced Parenthesis](stacks/balanced-paranthesis.java)
- [Stack](stacks/stack.java)
- [The Stock Span Problem](stacks/the-stock-span-problem.java)
@ -64,6 +73,7 @@
- [Sliding Window Maximum](stacks/sliding-window-maximum.java)
## Strings
- [KMP](strings/kmp.java)
- [Palindrome](strings/palindrome.java)
- [Rabin Krap](strings/rabin-karp.java)
@ -77,6 +87,7 @@
- [First Non Repeating Character](strings/first-non-repeating-char.java)
## Trees
- [Pre in Post Traversal](trees/pre-in-post-traversal.java)
- [Left View of a Tree](trees/left-view.java)
- [Right View of a Tree](trees/right-view.java)