Created
April 16, 2022 19:11
-
-
Save ksl-fourwalls/7791728badaa4afb3fb0ffb1ec016056 to your computer and use it in GitHub Desktop.
Stack of queue implementation in C
This file contains hidden or 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
| /* using standard IO and standard bool header file */ | |
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #define QUEUE_LEN 10 | |
| #define STACK_LEN 10 | |
| /* | |
| Stack Of Queues: | |
| means multiple queues are stack on each other | |
| Stack | |
| ````` | |
| ------------------------- <======= n - 1 | |
| | Queue[size] | | |
| |-----------------------| | |
| | Queue[size] | | |
| |-----------------------| <------- top | |
| | Queue[size] | | |
| |-----------------------| ----------------- <========= n - 1 | |
| | Queue[size] | | buffer[3] | | |
| |-----------------------| |---------------| <--------- rear | |
| | Queue[size] | | buffer[2] | | |
| |-----------------------|-----------------------------------------------| | |
| | Queue[size] | | buffer[1] | | |
| |-----------------------|-----------------------------------------------| <--------- front | |
| | Queue[size] | | buffer[0] | | |
| |-----------------------| ----------------- <========= -1 | |
| | Queue[size] | | |
| ------------------------- <======== -1 | |
| */ | |
| /* | |
| Queue: contains our real | |
| buffer where we are going write | |
| our data | |
| front: it is front of queue | |
| in fifo data organisation | |
| where our data is deleted from | |
| rear: is last element of queue | |
| where our data is inserted from | |
| */ | |
| struct Queue | |
| { | |
| char buffer[QUEUE_LEN]; | |
| int front; | |
| int rear; | |
| }; | |
| void queue_enqueue(struct Queue *, char); | |
| /* | |
| enqueue one element: cause | |
| fullness status of queue is already | |
| been check in stack | |
| */ | |
| char queue_dequeue(struct Queue *); | |
| /* | |
| dequeue one element: from | |
| queue and emptyness is already been | |
| checked in stack | |
| */ | |
| bool queue_isEmpty(struct Queue *); | |
| /* | |
| queue_isEmpty: checks whether the | |
| front is not equals to | |
| -1 or front not equals to | |
| rear | |
| */ | |
| bool queue_isFull(struct Queue *); | |
| /* | |
| queue_isFull: checks whether the | |
| rear is not equals to n - 1 | |
| if it does it will return true | |
| else false | |
| */ | |
| char queue_peek(struct Queue *); | |
| /* | |
| queue_peek: returns element at front | |
| index | |
| */ | |
| void queue_traverse(struct Queue *); | |
| /* | |
| queue_traverse: traverse from each element | |
| of queue and prints in console | |
| */ | |
| /* | |
| struct stack: | |
| contains total STACK_LEN number of | |
| queues in it | |
| top is the stack pointer | |
| */ | |
| struct stack | |
| { | |
| struct Queue queue[STACK_LEN]; | |
| int top; | |
| }; | |
| void push(char); | |
| /* | |
| push will checks | |
| for whether the stack is already | |
| full or not | |
| if it does it will prints error | |
| in console | |
| else it will calls enqueue method of queue | |
| for given stack pointer | |
| it will not increase stackpointer until | |
| particular queue is not filled up | |
| */ | |
| char pop(); | |
| /* | |
| pop will checks | |
| whether the stack is already | |
| empty or not | |
| if it does it will prints error | |
| in console | |
| else it will calls dequeue method of queue | |
| for given stack pointer | |
| it will not decrease stack pointer until | |
| particular queue is not empty | |
| */ | |
| char peek(); | |
| /* | |
| peek will call queue peek method | |
| */ | |
| bool isFull(); | |
| /* | |
| isFull will checks whether | |
| top is not equal to n-1 | |
| */ | |
| bool isEmpty(); | |
| /* | |
| isEmpty will checks whether | |
| top is not equal to -1 | |
| */ | |
| void traverse(); | |
| /* | |
| traverse will call each queues | |
| traverse method and also interate | |
| to all queues | |
| */ | |
| // stack initialization | |
| struct stack mStack = { | |
| .queue = {0}, | |
| .top = 0 | |
| }; | |
| // enqueue element from queue | |
| void queue_enqueue(struct Queue *self, char inp) | |
| { | |
| self->buffer[self->rear++] = inp; | |
| } | |
| // dequeue element from queue | |
| char queue_dequeue(struct Queue *self) | |
| { | |
| return self->buffer[self->front++]; | |
| } | |
| // Fullness status of queue | |
| bool queue_isFull(struct Queue *self) | |
| { | |
| if (self->rear == QUEUE_LEN-1) | |
| return true; | |
| return false; | |
| } | |
| // emptyness status queue | |
| bool queue_isEmpty(struct Queue *self) | |
| { | |
| if (self->front == -1 || self->front == self->rear) | |
| return true; | |
| return false; | |
| } | |
| // peek element in queue | |
| char queue_peek(struct Queue* self) | |
| { | |
| return self->buffer[self->front]; | |
| } | |
| // queue traversal | |
| void queue_traverse(struct Queue *self) | |
| { | |
| for (register int i = self->front; i <= self->rear; i++) | |
| { | |
| putchar(self->buffer[i]); | |
| } | |
| } | |
| // Pushes element | |
| void push(char data) | |
| { | |
| if (!isFull()) | |
| if (!queue_isFull(&mStack.queue[mStack.top])) | |
| queue_enqueue(&mStack.queue[mStack.top], data); | |
| else | |
| { | |
| mStack.top++; | |
| push(data); | |
| } | |
| else | |
| fprintf(stderr, "Stack is Full :(\n"); | |
| } | |
| // pops element | |
| char pop() | |
| { | |
| if (!isEmpty()) | |
| if (!queue_isEmpty(&mStack.queue[mStack.top])) | |
| return queue_dequeue(&mStack.queue[mStack.top]); | |
| else | |
| { | |
| mStack.top--; | |
| return pop(); | |
| } | |
| else | |
| fprintf(stderr, "Stack is Empty :(\n"); | |
| } | |
| // Checks stack full status | |
| bool isFull() | |
| { | |
| if (mStack.top == STACK_LEN-1) | |
| return true; | |
| return false; | |
| } | |
| // checks stack empty statux | |
| bool isEmpty() | |
| { | |
| if (mStack.top == -1) | |
| return true; | |
| return false; | |
| } | |
| // stack peek element | |
| char peek() | |
| { | |
| return queue_peek(&mStack.queue[mStack.top]); | |
| } | |
| // Stack traversal | |
| void traverse() | |
| { | |
| int temp = mStack.top; | |
| while (temp != -1) | |
| { | |
| queue_traverse(&mStack.queue[temp]); | |
| temp--; | |
| } | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| push('a'); | |
| push('a'); | |
| push('a'); | |
| push('a'); | |
| pop(); | |
| pop(); | |
| pop(); | |
| traverse(); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment