UNIVERSITY INSTITUTE OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE AND
ENGG.
Bachelor of Engineering (Computer
Science & Engineering)
DATA STRUCTURES 22CSH-211/22ITH-211
Operations on Queues DISCOVER . LEARN .
EMPOWER
Basic Operations in Queue
Enqueue Operation
Below are the steps to enqueue (insert) data into a queue
•Check whether the queue is full or not.
•If the queue is full – print the overflow error and exit the program.
•If the queue is not full – increment the rear pointer to point to the next empty space.
•Add the element in the position pointed to by the Rear.
•Return success.
Algorithm for Enqueue Operation
procedure enqueuer (data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
Below are the steps to perform dequeue operation
•Check whether the queue is full or not.
•If the queue is empty – print the underflow error and exit the program.
•If the queue is not empty – access the data where the front is pointing.
•Increment the front pointer to point to the next available data element.
•Return success.
Algorithm for Dequeue Operation
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
peek()
This function helps to see the data at the front of the queue. The algorithm of peek()
function is as follows −
Algorithm
begin procedure peek
return queue[front]
end procedure
Example
int peek()
{
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear
pointer to reach at MAXSIZE to determine that the queue is full. In case we maintain the
queue in a circular linked-list, the algorithm will differ. Algorithm of isfull() function −
Algorithm
begin procedure isfull
if rear equals to MAXSIZE
return true
else
return false
End if
end procedure
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence
empty.
Here's the C programming code −
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
REFERENCES
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• [Link]
• [Link]
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India
12
THANK
YOU