Deque in C - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Deque in C

Hello guys

#include<stdio.h>
#define MAX 5
int deque[MAX], left = -1, right = -1, val;

// display the deque
void display()
{
	int front = left ; 
	int rear = right;
	if( front == -1)
		printf("\n Queue is Empty");
	else
	{
		printf("\n The elements of the queue are : ");
		if(front<=rear)
		{
			while(front <= rear)
			{
				printf("%d  ",deque[front ]);
				front++;
			}
		}
		else
		{
			while(front <= MAX -1)
			{
				printf("%d  ",deque[front]);
				front++;
			}
			front = 0; 
			while(front <= rear)
			{
				printf("%d  ",deque[front]);
				front++;
				
			}
		}
		printf("\n");
	}
}

// Insert at left
void insert_left()
{
	printf("\n Enter the value to be inserted : ");
	scanf("%d",&val);
	if(left == 0 && right == MAX - 1 || left == right + 1)
		printf("\n Overflow");
	else if ( left == 0 && right!= MAX - 1 )
	{
		left = MAX - 1;
		deque[left] = val;
	}
	else if ( left == -1  )
	{
		left = 0;
		right = 0;
		deque[left] = val;
	}
	else
	{
		left--;
		deque[left] = val;
	}
}

// Insert at right
void insert_right()
{
	printf("\n Enter the value to be inserted : ");
	scanf("%d",&val);
	if(left == 0 && right == MAX - 1 || left == right + 1)
		printf("\n Overflow");
	else if ( left!=0 && right == MAX - 1 )
	{
		right = 0;
		deque[right] = val;
	}
	else if ( left == -1  )
	{
		left = 0;
		right = 0;
		deque[right] = val;
	}
	else
	{
		right++;
		deque[right] = val;
	}
}


// Delete at left
void delete_left()
{
	if(left == -1)
		printf("Underflow");
	else if (left == 0 && right == 0)
	{
		left = -1;
		right = -1;
	}
	else if (left == MAX -1)
		left = 0;
	else
		left++;
	
}

// Delete at right
void delete_right()
{
	if(left == -1)
		printf("Underflow");
	else if (left == 0 && right == 0)
	{
		left = -1;
		right = -1;
	}
	else if ( right == MAX -1)
		right = 0;
	else
		right--;
}

// Menu for input restricted
void input_restricted()
{
	int id;
	printf("\n === Input restricted Deque Menu === ");
	printf("\n 1. Insert at left");
	printf("\n 2. Delete at left");
	printf("\n 3. Delete at right");
	printf("\n 4. Display");
	printf("\n Enter choice : ");
	scanf("%d",&id);
	switch(id)
	{
		case 1 : insert_left();
				 break;
		case 2 : delete_left();
				 break;
		case 3 : delete_right();
				 break;
		case 4 : display();
				 break;
		default : printf("\n Invalid input ");
				 break;
	}
}

// Menu for output restricted
void output_restricted()
{
	int od;
	printf("\n === Output restricted Deque Menu === ");
	printf("\n 1. Delete at right");
	printf("\n 2. Insert at left");
	printf("\n 3. Insert at right");
	printf("\n 4. Display");
	printf("\n Enter choice : ");
	scanf("%d",&od);
	switch(od)
	{
		case 1 : delete_right();
				 break;
		case 2 : insert_left();
				 break;
		case 3 : insert_right();
				 break;
		case 4 : display();
				 break;
		default : printf("\n Invalid input ");
				 break;
	}
}


void main()
{
	int ch;
	do
	{
		
		printf("\n === Menu === ");
		printf("\n 1. Input restricted deque ");
		printf("\n 2. Output restricted deque ");
		printf("\n 3. Display");
		printf("\n 4. Exit ");
		printf("\n Enter your choice : ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1 : input_restricted();
					 break;
			case 2 : output_restricted();
					 break;
			case 3 : display();
					 break; 
			case 4 : printf("\n Thank you !!!\n\n");
					 break;
			default : printf("\n Invalid Input");
					 break;
		}
	}while(ch!=4);
}
/*
Output : 
	
 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 1

 Enter the value to be inserted : 56

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 3

 Enter the value to be inserted : 27

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 1

 Enter the value to be inserted : 44

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 44  56  27  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 2

 Enter the value to be inserted : 29

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 29  44  56  27  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 2

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 44  56  27  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 3

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 44  56  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 3

 Enter the value to be inserted : 28

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 2

 Enter the value to be inserted : 27

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 27  44  56  28  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 1

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 27  44  56  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 1

 Enter the value to be inserted : 48

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 1

 Enter the value to be inserted : 29

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 29  48  27  44  56  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 1

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 2

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 48  27  44  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 2

 === Output restricted Deque Menu === 
 1. Delete at right
 2. Insert at left
 3. Insert at right
 4. Display
 Enter choice : 3

 Enter the value to be inserted : 56

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 1

 === Input restricted Deque Menu === 
 1. Insert at left
 2. Delete at left
 3. Delete at right
 4. Display
 Enter choice : 3

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 3

 The elements of the queue are : 48  27  44  

 === Menu === 
 1. Input restricted deque 
 2. Output restricted deque 
 3. Display
 4. Exit 
 Enter your choice : 4

 Thank you !!!

lobo@lobo-Compaq-Presario-CQ60-Notebook-PC:~/Desktop$

*/

No comments:

PropellerAds