Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE 4
- int queue[SIZE]; // |0|1|2|3|
- int head = -1;
- int tail = -1;
- void initialise() {
- head = -1;
- tail = -1;
- for (int i = 0; i < SIZE; i++) {
- queue[i] = 0;
- }
- }
- int isFull() {
- if ( ( head == tail + 1 ) )
- {
- return 1;
- }
- else if ((head == 0 && tail == SIZE - 1)) {
- return 1;
- }
- else {
- return 0;
- }
- }
- int isEmpty() {
- if (head == -1) {
- return 1;
- }
- else {
- return 0;
- }
- }
- void push(int number) {
- if (head == -1) {
- head = 0;
- tail = (tail + 1) % SIZE;
- queue[tail] = number;
- //printf("ATM->Head is at position:%d\n", head);
- //printf("ATM->Tail is at position:%d\n", tail);
- }
- else if (head != -1) {
- if (isFull()) {
- head = (head + 1) % SIZE;
- tail = (tail + 1) % SIZE;
- queue[tail] = number;
- //printf("ATM->Head is at position:%d\n", head);
- //printf("ATM->Tail is at position:%d\n", tail);
- }
- else {
- tail = (tail + 1) % SIZE;
- queue[tail] = number;
- //printf("ATM->Head is at position:%d\n", head);
- //printf("ATM->Tail is at position:%d\n", tail);
- }
- /*
- else if(head != -1){
- if(isFull() && tail>head){
- printf("Tail reached head!\n");
- head = (head + 1)% SIZE;
- tail = (tail + 1) % SIZE;
- queue[tail] = number;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- else if(tail<head){
- head = (head + 1) % SIZE;
- queue[tail-1] = number;
- }
- else {
- tail = (tail + 1) % SIZE;
- queue[tail] = number;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- }
- */
- }
- }
- void pop() {
- if (head == -1)
- {
- printf("Queue Underflow\n");
- return;
- }
- printf("Element deleted from queue is : %d\n", queue[head]);
- queue[head] = 0;
- if (head == tail) /* queue has only one element */
- {
- head = -1;
- tail = -1;
- }
- else
- {
- if (head == SIZE - 1)
- head = 0;
- else
- head = head + 1;
- }
- /*
- int delete;
- if (head == -1) {
- printf("Queue is empty! Don't have anything to pop!\n");
- }
- else if(head==tail) {
- queue[head] = 0 ;
- head = -1;
- tail = -1;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- else if (head != tail) {
- if (tail > head) {
- if (tail == head + 1) {
- queue[head] = 0;
- head = tail;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- else {
- queue[head] = 0;
- head = (head + 1) % SIZE;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- }
- else{
- if (head == tail + 1) {
- queue[head] = 0;
- if (head == tail) {
- head = (head + 1) % SIZE;
- }
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- else {
- queue[head] = 0;
- head = (head + 1) % SIZE;
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- }
- }
- */
- }
- void display() {
- for (int i = 0; i < SIZE; i++) {
- printf("queue[%d] = %d \n", i, queue[i]);
- }
- printf("ATM->Head is at position:%d\n", head);
- printf("ATM->Tail is at position:%d\n", tail);
- }
- int main() {
- //case 1
- int numberPush;
- int choice;
- do
- {
- printf("\n\t---MENU---\n");
- printf("\n\t---1.PUSH---\n");
- printf("\n\t---2.POP---\n");
- printf("\n\t---3.EMPTY---\n");
- printf("\n\t---4.FULL---\n");
- printf("\n\t---5.INITILIASE---\n");
- printf("\n\t---6.DISPLAY---\n");
- printf("\n\t---7.EXIT---\n");
- printf("Choose an option:");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1: {
- printf("Push:");
- scanf("%d",&numberPush);
- push(numberPush);
- break;
- }
- case 2: {
- pop();
- break;
- }
- case 3: {
- if (isEmpty() == 1) {
- printf("Empty!All the positions are empty!\n");
- }
- else {
- printf("Not empty! We have elements!\n");
- }
- break;
- }
- case 4: {
- if (isFull() == 1) {
- printf("Full! Each position has an element stored!\n");
- }
- else {
- printf("Not each position has an element stored!\n");
- }
- break;
- }
- case 5: {
- initialise();
- printf("The list has been initialized!\n");
- break;
- }
- case 6: {
- display();
- break;
- }
- default:
- printf("Program quited!\n");
- break;
- }
- } while (choice>0 && choice<7);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement