0% found this document useful (0 votes)
17 views11 pages

Program For Deque

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Program For Deque

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program to implement double ended queue(Dequeue)

#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.

3. Initialize the Deque


c
Copy code
void initializeDeque(Deque* dq) {
dq->front = -1;
dq->rear = -1;
}

 This function initializes front and rear to -1, indicating that the deque is empty.

4. Checking if the Deque is Full or Empty


c
Copy code
int isFull(Deque* dq) {
return ((dq->front == 0 && dq->rear == MAX - 1) || (dq->front == dq->rear
+ 1));
}

 isFull function: Checks if the deque is full.


o front == 0 && rear == MAX - 1 means the deque has filled the array end-to-
end.
o front == rear + 1 means the array is wrapped around and full.

6
Copy code
int isEmpty(Deque* dq) {
return (dq->front == -1);
}

 isEmpty function: Checks if the deque is empty by verifying if front is -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);
}

 insertFront function: Adds an element at the front.


o If the deque is full, it prints an error message.
o Conditions:
 If the deque is initially empty (front == -1), it sets front and rear to 0.
 If front is at the start (0), it wraps around to MAX - 1.
 Otherwise, it simply decrements front.
o Finally, it inserts value at the updated front position.

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);
}

 insertRear function: Adds an element at the rear.


o If the deque is full, it prints an error message.
o Conditions:
 If the deque is empty (front == -1), it initializes both front and rear to
0.
 If rear is at the last position (MAX - 1), it wraps around to 0.
 Otherwise, it increments rear.
o It then inserts value at the updated rear position.

6. Deletion Functions

Delete from Front

c
Copy code
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++;
}

printf("Deleted %d from front.\n", value);


}

 deleteFront function: Removes an element from the front.


o If the deque is empty, it prints an error message.
o Conditions:
 If there’s only one element (front == rear), it resets front and rear to
-1 (making the deque empty).
 If front is at the last position (MAX - 1), it wraps around to 0.
 Otherwise, it increments front.
o It prints the deleted value.

8
Delete from Rear

c
Copy code
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);


}

 deleteRear function: Removes an element from the rear.


o If the deque is empty, it prints an error message.
o Conditions:
 If only one element is left (front == rear), it resets both front and
rear.
 If rear is at 0, it wraps around to MAX - 1.
 Otherwise, it decrements rear.
o It prints the deleted value.

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");
}

 displayDeque function: Shows all elements in the deque.

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);

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;
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

ChatGPT can make mistakes. Check importan

11

You might also like