Program For Deque
Program For Deque
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct {
int items[MAX];
int front, rear;
} Deque;
void initializeDeque(Deque* dq) {
dq->front = -1;
dq->rear = -1;
}
int isFull(Deque* dq) {
return ((dq->front == 0 && dq->rear == MAX - 1) || (dq->front == dq->rear + 1));
}
int isEmpty(Deque* dq) {
return (dq->front == -1);
}
void insertFront(Deque* dq, int value) {
if (isFull(dq)) {
printf("Deque is full! Cannot insert at front.\n");
return;
}
if (dq->front == -1) {
dq->front = dq->rear = 0;
} else if (dq->front == 0) {
dq->front = MAX - 1;
} else {
dq->front--;
}
1
dq->items[dq->front] = value;
printf("Inserted %d at front.\n", value);
}
void insertRear(Deque* dq, int value) {
if (isFull(dq)) {
printf("Deque is full! Cannot insert at rear.\n");
return;
}
if (dq->front == -1) {
dq->front = dq->rear = 0;
} else if (dq->rear == MAX - 1) {
dq->rear = 0;
} else {
dq->rear++;
}
dq->items[dq->rear] = value;
printf("Inserted %d at rear.\n", value);
}
void deleteFront(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty! Cannot delete from front.\n");
return;
}
int value = dq->items[dq->front];
if (dq->front == dq->rear) {
dq->front = dq->rear = -1;
} else if (dq->front == MAX - 1) {
dq->front = 0;
} else {
dq->front++;
}
2
printf("Deleted %d from front.\n", value);
}
void deleteRear(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty! Cannot delete from rear.\n");
return;
}
int value = dq->items[dq->rear];
if (dq->front == dq->rear) {
dq->front = dq->rear = -1;
} else if (dq->rear == 0) {
dq->rear = MAX - 1;
} else {
dq->rear--;
}
printf("Deleted %d from rear.\n", value);
}
void displayDeque(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
int i = dq->front;
printf("Deque elements: ");
while (1) {
printf("%d ", dq->items[i]);
if (i == dq->rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}
3
int main() {
Deque dq;
initializeDeque(&dq);
int choice, value;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at front\n");
printf("2. Insert at rear\n");
printf("3. Delete from front\n");
printf("4. Delete from rear\n");
printf("5. Display deque\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(&dq, value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(&dq, value);
break;
case 3:
deleteFront(&dq);
break;
case 4:
deleteRear(&dq);
break;
4
case 5:
displayDeque(&dq);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
5
1. Preprocessor Directives and Constants
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#include <stdio.h> and #include <stdlib.h> are standard libraries for input-output
and standard functions (e.g., exit()).
#define MAX 10 defines the maximum size of the deque, limiting it to 10 elements.
2. Structure Definition
c
Copy code
typedef struct {
int items[MAX];
int front, rear;
} Deque;
Deque structure:
o int items[MAX] is an array to hold the deque elements.
o int front, rear are indices used to track the front and rear ends of the deque.
This function initializes front and rear to -1, indicating that the deque is empty.
6
Copy code
int isEmpty(Deque* dq) {
return (dq->front == -1);
}
5. Insertion Functions
Insert at Front
c
Copy code
void insertFront(Deque* dq, int value) {
if (isFull(dq)) {
printf("Deque is full! Cannot insert at front.\n");
return;
}
if (dq->front == -1) {
dq->front = dq->rear = 0;
} else if (dq->front == 0) {
dq->front = MAX - 1;
} else {
dq->front--;
}
dq->items[dq->front] = value;
printf("Inserted %d at front.\n", value);
}
Insert at Rear
c
Copy code
void insertRear(Deque* dq, int value) {
if (isFull(dq)) {
printf("Deque is full! Cannot insert at rear.\n");
return;
}
if (dq->front == -1) {
dq->front = dq->rear = 0;
} else if (dq->rear == MAX - 1) {
7
dq->rear = 0;
} else {
dq->rear++;
}
dq->items[dq->rear] = value;
printf("Inserted %d at rear.\n", value);
}
6. Deletion Functions
c
Copy code
void deleteFront(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty! Cannot delete from front.\n");
return;
}
8
Delete from Rear
c
Copy code
void deleteRear(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty! Cannot delete from rear.\n");
return;
}
7. Display Function
c
Copy code
void displayDeque(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
}
int i = dq->front;
printf("Deque elements: ");
while (1) {
printf("%d ", dq->items[i]);
if (i == dq->rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}
9
o If empty, it prints an appropriate message.
o It uses a loop to print elements from front to rear, wrapping around using (i +
1) % MAX.
8. Main Function
c
Copy code
int main() {
Deque dq;
initializeDeque(&dq);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(&dq, value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(&dq, value);
break;
case 3:
deleteFront(&dq);
break;
case 4:
deleteRear(&dq);
break;
case 5:
displayDeque(&dq);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
main function: Runs an infinite loop to present a menu and execute the user’s choice.
10
o Options include inserting or deleting from both ends, displaying the deque, and
exiting the program.
4o
11