0% found this document useful (0 votes)
149 views9 pages

Data Structure - Q1) Write A Program To Implement The Double Ended - Queue Aeraxia - in

The document describes a program to implement a double-ended queue (deque) data structure in C. It includes functions to insert and delete elements from either end of the deque, as well as display its contents. The program uses an array and two indices to represent the deque, handles overflow and underflow conditions, and provides a menu-driven interface.

Uploaded by

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

Data Structure - Q1) Write A Program To Implement The Double Ended - Queue Aeraxia - in

The document describes a program to implement a double-ended queue (deque) data structure in C. It includes functions to insert and delete elements from either end of the deque, as well as display its contents. The program uses an array and two indices to represent the deque, handles overflow and underflow conditions, and provides a menu-driven interface.

Uploaded by

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

Q1) Write a program to implement the double ended

queue
Explanation:-
Header Files:
• stdio.h: This standard library header provides input/output
functions like printf and scanf for user interaction.
• conio.h: This non-standard header (not recommended for
modern C development) offers console input functions like
getch. It's generally better practice to use getchar() from
stdio.h for portability.
Deque Representation:
• An integer array deque of size MAX (defined as 10) stores the
deque elements.
• Two integer variables left and right act as indices to track the
front and rear of the deque, respectively.
• Initially, both left and right are set to -1, indicating an empty
deque.
Deque Operations:
• The program implements five functions for deque operations:
o insert_right(void): Inserts an element at the right end
(rear) of the deque.
o insert_left(void): Inserts an element at the left end (front)
of the deque.
o delete_right(void): Deletes and returns the element from
the right end.
o delete_left(void): Deletes and returns the element from
the left end.
o display(void): Prints the elements present in the deque.
Menu-Driven Interface:
• The main function is the program's entry point.
• It presents a menu to the user with options for insertion,
deletion, and displaying the deque elements.
• Based on the user's choice, it calls the corresponding deque
operation function.
Error Handling:
• The code checks for overflow and underflow conditions during
insertion and deletion operations, respectively.
o Overflow occurs when the deque is full and cannot
accommodate more elements.
o Underflow occurs when the deque is empty and there are
no elements to delete.
• If an error condition is detected, an appropriate message is
printed.
Explanation of Functions:
1. insert_right()
o Prompts the user to enter a value to be added.
o Checks for overflow condition:
▪ If left is 0 and right is MAX-1 (end of the array), or
▪ If left is just one position ahead of right (circular
overflow).
o If the deque is empty (left == -1), initializes both left and
right to 0.
o Otherwise, increments right considering circular behavior
(if it reaches the end of the array, it wraps around to the
beginning).
o Inserts the new element at deque[right].
2. insert_left()
o Similar to insert_right(), it prompts for a value and checks
for overflow.
o If empty (left == -1), initializes left and right to 0.
o Otherwise, decrements left considering circular behavior
(if it reaches the beginning of the array, it wraps around to
the end).
o Inserts the new element at deque[left].
3. delete_right()
o Checks for underflow condition (empty deque).
o Prints the element to be deleted from deque[right].
o If the deque becomes empty after deletion (left == right),
resets both left and right to -1.
o Otherwise, updates right considering circular behavior (if
it's 0, it becomes MAX-1).
4. delete_left()
o Similar to delete_right(), it checks for underflow and
prints the element to be deleted from deque[left].
o If the deque becomes empty, resets left and right to -1.
o Otherwise, updates left considering circular behavior (if
it's MAX-1, it becomes 0).
5. display()
o Handles the empty deque case by printing a message.
o Defines front and rear variables to represent the starting
and ending indices for iteration.
o If front is less than or equal to rear (typical case), iterates
through the elements from front to rear and prints them.
o If front is greater than rear (circular deque), it handles
printing in two segments:
▪ Prints elements from front to the end of the array
(MAX-1).
▪ Resets front to 0 and continues printing elements
from

You might also like