Untitled

 avatar
unknown
plain_text
2 years ago
1.6 kB
10
Indexable
#include<stdio.h>
//1 2 3 4 5
// insert at the rear
// delete from the front
int rear=-1;
int front=-1;

void push(int queue[],int element,int n);
void pop(int queue[]);
void display(int queue[]);

void display(int queue[]){
	if(front==-1 && rear == -1){
		printf("The queue is empty !!");
	}
	else{
		printf("Elements in the queue are as follows :-\n");
		for(int i=front;i<=rear;i++){
			printf("%d\n",queue[i]);
		}
	}
}
void push(int queue[],int element,int n){
	if(rear==(n-1)){
		printf("OVERLOAD");
	}
	else{
		if(rear==-1 && front==-1)
		{
			front++;
			rear++;
			queue[rear]=element;
			printf("Successfully pushed %d at position %d in the queue",element,rear);
		}
		else{
			rear++;
			queue[rear]=element;
			printf("Successfully pushed %d at position %d in the queue",element,rear);
		}
	}
}
void pop(int queue[]){
	if(front==-1){
		printf("UNDERFLOW");
	}
	else{
		//queue[rear]=0;
		front++;
		printf("Poped the element at position %d",front);
	}
}
int main(void){
	int n;
	printf("Enter the size of the queue : ");
	scanf("%d",&n);
	int choice,queue[n];
  do
  {
    printf("\nEnter what you wanna do in this queue ?\n1. Push\n2. Pop\n3. Display\n4. Exit\t : ");
    scanf("%d", &choice);
    if (choice == 1)
    {
      int temp;
      printf("Enter the element to be pushed : ");
      scanf("%d", &temp);
      push(queue, temp,n);
    }
    else if (choice == 2)
    {
      pop(queue);
    }
    else if (choice == 3)
    {
      display(queue);
    }

  } while (choice == 1 || choice == 2 || choice == 3);
}