#include <stdio.h>
#include <conio.h>
#include <malloc.h>

struct node
{
int data;
struct node *next;
};

struct node *q;

void insert(int);
void delete_element();
void display();
void peek();

int main()
{
	int val, option;
	
	q = NULL;
	
	do
	{
		printf("\n *****MAIN MENU*****");
		printf("\n 1. INSERT");
		printf("\n 2. DELETE");
		printf("\n 3. PEEK");
		printf("\n 4. DISPLAY");
		printf("\n 5. EXIT");
		printf("\n Enter your option : ");
		scanf("%d", &option);
		switch(option)
		{
			case 1:
				printf("\n Enter the number to insert in the queue:");
				scanf("%d", &val);
				insert(val);
				break;
			case 2:
				delete_element();
				break;
			case 3:
				peek();
				break;
			case 4:
				display();
				break;
		}
	}while(option != 5);
	getch();
	return 0;
}

void insert(int val)
{
	struct node *ptr, *p;
	ptr = (struct node*)malloc(sizeof(struct node));
	ptr -> data = val;
	ptr->next = NULL;
	
	if(q == NULL)
	{
		q = ptr;
	}
	else
	{
		p = q;
		while (p->next!=NULL) {
			printf(">>> %d \n", p->data);
			p = p->next;
		}
		p->next = ptr;
	}
	return;
}

void display()
{
	struct node *ptr;
	ptr = q;
	while (ptr != NULL) {
			printf("\n Data : %d", ptr->data);
			ptr = ptr->next;
	}
	return;
}

void delete_element()
{
	struct node *ptr;
	if(q == NULL)
		printf("\n UNDERFLOW");
	else
	{
		ptr = q;
		q = q->next;
		printf("\n The value being deleted is : %d", ptr -> data);
		free(ptr);
	}
	return;
}

void peek()
{
	if(q==NULL)
	{
		printf("\n QUEUE IS EMPTY");
	}
	else
		printf("\n The value at the head is : %d", q -> data);
	return;
}