In: Computer Science
Give the algorithms in pseudo code of the enqueue and dequeue procedures. Constraint: the structure implementing the queue must only be pointed to by a single field (example: head, tail or number of elements, etc.)
Queue:
A queue is a linear data structure in which addition is done at the one end and deletion is one at another end.
Insertion can be performed at the rear end.
Deletion can be performed at the front end.
Pseudocode:
The informal language to develop an algorithm used by the programmer is known as pseudocode. This describes the text-based step by step solution to the given problem that is intended for human reading.
The pseudo code of the enqueue is given below:
Step 1: Start
Step 2: Declare variable x
Step 3: Input variable x
Step 4: struct node *ptr = head
Step 5: struct node *link = (struct node*) malloc(sizeof(struct node))
Step 6: link->data = data
Step 7: link->next = NULL;
Step 8: while ptr != NULL
Step 8.1: ptr = ptr->next
Step 9: ptr->next = link
Step 10: Stop
The pseudo code of the dequeue is given below:
Step 1: Start
Step 2: struct node *ptr = head
Step 3: if ptr == NULL
Step 3.1: return
Step 4: struct node* temp = head
Step 5: head = temp->next
Step 6: free(temp)
Step 7: Stop