Last active
October 7, 2018 13:47
-
-
Save jamesgeorge007/52776498cb191a733bbc8c0695bb1f63 to your computer and use it in GitHub Desktop.
Insertion and deletion operations on a Deque.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define SIZE 15 | |
int Deque[SIZE], front = -1, rear = -1; | |
void display() | |
{ | |
int i; | |
if(front == -1) | |
{ | |
printf("\nEmpty!"); | |
exit(1); | |
} | |
else if(front <= rear) | |
{ | |
printf("\nState of the Deque is: \n"); | |
for(i = front; i <= rear; i++) | |
{ | |
printf("%d\t", Deque[i]); | |
} | |
} | |
else | |
{ | |
printf("\nState of the Deque is: \n"); | |
for(i = front; i <= SIZE-1; i++) | |
{ | |
printf("%d\t", Deque[i]); | |
} | |
for(i = 0; i <= rear; i++) | |
{ | |
printf("%d\t", Deque[i]); | |
} | |
} | |
printf("\n"); | |
} | |
void insertAtRear(int el) | |
{ | |
if(rear + 1 == front || (front == 0) && (rear == SIZE-1)) | |
{ | |
printf("\nOverflow!\n"); | |
exit(1); | |
} | |
else if(rear == SIZE - 1) | |
{ | |
rear = 0; | |
} | |
else | |
{ | |
if(front == -1 && rear == -1) | |
{ | |
front = 0; | |
} | |
rear+=1; | |
} | |
Deque[rear] = el; | |
printf("\nFront = %d, Rear = %d", front, rear); | |
display(); | |
} | |
void deleteAtFront() | |
{ | |
int item = Deque[front]; | |
if(front == -1) | |
{ | |
printf("\nUnderflow!\n"); | |
exit(1); | |
} | |
else | |
{ | |
if(front == rear) | |
{ | |
front = rear = -1; | |
} | |
else if(front == SIZE - 1) | |
{ | |
front = 0; | |
} | |
else | |
{ | |
front += 1; | |
} | |
} | |
printf("\nThe item removed was %d\nFront = %d, Rear = %d", item, front, rear); | |
display(); | |
} | |
void insertAtFront(int el) | |
{ | |
if((front == 0 && rear == SIZE-1) || front == rear + 1) | |
{ | |
printf("\nOverflow!\n"); | |
exit(1); | |
} | |
else if(front == -1 && rear == -1) | |
{ | |
rear = 0; | |
front = 0; | |
} | |
else if(front == 0) | |
{ | |
front = SIZE - 1; | |
} | |
else | |
{ | |
front--; | |
} | |
Deque[front] = el; | |
printf("\nFront = %d, Rear = %d", front, rear); | |
display(); | |
} | |
void deleteAtRear() | |
{ | |
int item = Deque[rear]; | |
if(front == -1) | |
{ | |
printf("\nUnderflow!\n"); | |
exit(1); | |
} | |
else if(front == rear) | |
{ | |
front = rear = -1; | |
} | |
else if(rear == 0) | |
{ | |
rear = SIZE - 1; | |
} | |
else | |
{ | |
rear--; | |
} | |
printf("\nThe item removed was %d, Front = %d, Rear = %d\n", item, front, rear); | |
display(); | |
} | |
void inputRestricted() | |
{ | |
char choice, cont; | |
int el; | |
do{ | |
printf("\n1.Insert at rear\n2.Delete at front\n3.Delete at rear\n4.Display\nEnter your choice: "); | |
choice = getche(); | |
switch(choice) | |
{ | |
case '1': | |
printf("\nEnter the element to be inserted: "); | |
scanf("%d", &el); | |
insertAtRear(el); | |
break; | |
case '2': | |
deleteAtFront(); | |
break; | |
case '3': | |
deleteAtRear(); | |
break; | |
case '4': | |
display(); | |
break; | |
default: | |
printf("\nInvalid option!"); | |
break; | |
} | |
//printf("\nWant to continue? "); | |
//choice = getche(); | |
}while(1); | |
} | |
void outputRestricted() | |
{ | |
char choice, cont; | |
int el; | |
do{ | |
printf("\n1.Delete at front\n2.Insert at front\n3.Insert at rear\n4.Display\nEnter your choice: "); | |
choice = getche(); | |
switch(choice) | |
{ | |
case '1': | |
deleteAtFront(); | |
break; | |
case '2': | |
printf("\nEnter the element to be inserted: "); | |
scanf("%d", &el); | |
insertAtFront(el); | |
break; | |
case '3': | |
printf("\nEnter the element to be inserted: "); | |
scanf("%d", &el); | |
insertAtRear(el); | |
break; | |
case '4': | |
display(); | |
break; | |
default: | |
printf("\nInvalid option!"); | |
break; | |
} | |
printf("\nWant to continue? "); | |
cont = getche(); | |
}while(cont == 'y' || cont == 'Y'); | |
} | |
void main() | |
{ | |
char choice; | |
printf("\n1.Input Restricted Queue\n2.output Restricted Queue? "); | |
choice = getche(); | |
switch(choice) | |
{ | |
case '1': | |
inputRestricted(); | |
break; | |
case '2': | |
outputRestricted(); | |
break; | |
default: | |
printf("\nInvalid option!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment