Assume Front and Rear nodes exist with the NextRef attribute

Assume “Front” and “Rear” nodes exist with the “NextRef” attributes pointing to the first and last nodes of the queue or being null if the queue is empty. Create pseudo code for the following 3 queue methods, showing the logic tied to the inverted list. Include a summary of these actions. enqueue( item ) dequeue( ) display( )

Solution


// Precondition: newItem is the item to be inserted.
// Postcondition: If the insertion is successful, newItem
// is at the back of the queue.
void enqueue(int newItem){
// Inserts an item at the back of a queue.
// creating new node
Node newNode = new Node(item)
newNode->NextRef = NULL
IF Front == NULL then do
Front = newNode
Rear = Front
ELSE do
Rear->NextRef = newNode
Rear = newNode;
}

// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is deleted
// and returns true; if queue is empty, returns false
void dequeue(){
// Dequeues the front of a queue.
// queue is not empty then rempve front item
IF Front != NULL then do
IF Front == Rear then do // only one item in queue
Front = Rear = NULL
Else do
Front = Front->NextRef
}

void display(){

Node temp = Front
While temp != NULL do
print temp->Item
temp = temp->NextRef

}

Assume “Front” and “Rear” nodes exist with the “NextRef” attributes pointing to the first and last nodes of the queue or being null if the queue is empty. Creat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site