Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queue.c: Add queue in C #518

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing spaces around == [whitespace/operators] [3]

Origin: CPPLintBear, Section: all.cpplint.

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');
}