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