Queue using link list - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Queue using link list

Hello guys

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct queue *next;
};
struct queue
{
	struct node *front,*rear;	
};
struct queue *q;
void create_queue(struct queue *q)
{
	q->rear = NULL;
	q->front = NULL;
}
struct queue *insert(struct queue *q,int val)
{
	struct node *ptr;
	ptr = (struct node*)malloc(sizeof(struct node));
	ptr->data = val;
	if(q->front == NULL)
	{
		q->front = ptr;
		q->rear = ptr;
		q->front->next = q->rear->next = NULL;
	}
	else
	{
		q->rear->next = ptr;
		q->rear = ptr;
		q->rear->next = NULL;
	}
	return q;
}

struct queue *disp(struct queue *q)
{
	struct node *ptr;
	ptr = q->front;
	if(ptr == NULL)
		printf("\n Queue is empty");
	else
	{
		printf("\n");
		while(ptr!=q->rear)
		{
			printf("%d\t",ptr->data);
			ptr = ptr->next;
		}
		printf("%d \t ",ptr->data);
	}
	return q;
}

struct queue *dequeue(struct queue *q)
{
	struct node *ptr;
	ptr = q->front;
	if(q->front == NULL)
		printf("\n underflow");
	else
	{
		q->front = q->front->next;
		printf("\n The value being deleted is : %d",ptr->next );
		free(ptr);
	}
	return *q;
}
void main()
{
	int val,op;
	create_queue(q);
	do
	{
		printf("\n ***MAIN MENU***\n");
		printf("\n 1. Insert \n");
		printf("\n 2. Delete \n");
		printf("\n 3. Display \n");
		printf("\n 4. Exit \n");
		printf("\n Enter your choice : ");
		scanf("%d",&op);
		switch(op)
		{
			case 1 : printf("\n Enter the number :");
					scanf("%d",&val);
					q = enqueue(q,val);
					break;
			case 2 : q = dequeue(q);
					 break;
			case 3 : q = disp(q);
					 break;
			case 4 : printf("\n Thank you \n \n");
					 break;
			default : printf("\n Invalid Input ");
					 break;
		}
	}while(op!=4);
	
}
/*
Output : 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 56

 56 successfully enqueued!!!
 Queue elements are : 56 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 27

 27 successfully enqueued!!!
 Queue elements are : 56 27 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 44

 44 successfully enqueued!!!
 Queue elements are : 56 27 44 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 48

 48 successfully enqueued!!!
 Queue elements are : 56 27 44 48 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Enter the element : 29

 29 successfully enqueued!!!
 Queue elements are : 56 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 1

 Element cannot be inserted 
 Queue overflow
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 56 successfully dequeued
 Queue elements are : 27 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 27 successfully dequeued
 Queue elements are : 44 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 44 successfully dequeued
 Queue elements are : 48 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 48 successfully dequeued
 Queue elements are : 29 
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 2

 Queue underflow!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 3

 Queue is empty!!!
===MENU===
 1. ENQUEUE
 2. DEQUEUE
 3. DISPLAY
 4. EXIT
 Enter your choice : 4

 Thank you!!! 


*/

No comments:

PropellerAds