Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Implementing Queue in C. Using Array */
- #include <stdio.h>
- #define CAPACITY 5
- int front = 0, rear = -1, totalitem = 0;
- int ourq[CAPACITY];
- void enq(int item)
- {
- if (totalitem == CAPACITY)
- {
- printf("Opps!!The queue is Full\n");
- return;
- }
- else
- {
- rear = (rear + 1) % CAPACITY;
- ourq[rear] = item;
- totalitem++;
- }
- }
- int deq()
- {
- if (totalitem == 0)
- {
- printf("Opps!!The queue is empty\n");
- return -1;
- }
- else
- {
- int frontitem = ourq[front];
- ourq[front] = -1;
- front = (front + 1) % CAPACITY;
- totalitem--;
- return frontitem;
- }
- }
- void print()
- {
- int i;
- for (i = 0; i < CAPACITY; i++)
- {
- printf("%d ", ourq[i]);
- }
- printf("\n");
- }
- int main()
- {
- printf("Implementing Queue\n");
- enq(10);
- enq(20);
- enq(30);
- enq(40);
- print();
- enq(50);
- print();
- enq(60);
- printf("Deq: %d\n", deq());
- print();
- enq(70);
- print();
- return 0;
- }
- /*
- Output:
- Implementing Queue
- 10 20 30 40 0
- 10 20 30 40 50
- Opps!!The queue is Full
- Deq : 10
- - 1 20 30 40 50
- 70 20 30 40 50
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement