Day 22 — Queue using List. 100 Days to Amazon
By: House Of Codes
Tags: Coding, Java, Programming, Software Development, Problem Solving

Day 22 — Queue using List

100 Days to Amazon

Image by Richard Mcall from Pixabay

100 Days to Amazon — Day 22 — Queue using List

Out of Free Stories? Here is my Friend Link.

Hey Guys, Today is day 22 of the challenge that I took. Wherein I will be solving every day for 100 days the programming questions that have been asked in previous interviews.

You have a bonus at the end if you keep reading. You can find out the companies that have asked these questions in real interviews.

All these problems are taken from the following e-book. 🎓

This is completely free 🆓 if you have an amazon kindle subscription.

This e-book contains 100 coding problems that have been asked in top tech interview questions. It also has a guide to solving all the problems in 200+ ways. These problems I assure you has been asked in previous interviews.

You have to decide whether you want to go unprepared for a tech interview or go ahead and quick search for this guide to solve the 100 problems.

Begin Your ascent to greatness🚀

Note : this e-book only contains the links to the solutions.

Day 22- Queue using Linked List

AIM🏹

To Implement a Q using LinkedList.

You need to implement an enqueue and dequeue function.

When getHeadOfLinkedList is called, return the head of the linked list.

When enqueue is called, insert the items in the list at the head.

When the Q is empty and dequeue is called return 0.

When the Q is empty, GetMax and GetMin should return 0.

GetMin should return the MIN element in the Q and it should NOT remove the element out.

Example

ENQUEUE 4 ENQUEUE 6 ENQUEUE 8 DEQUEUE

GETMAX should return: 6

Code👇

Author: Akshay Ravindran

Explanation🦅

This is a basic linked list concept that is used to create a queue data structure.

Queue by default works based on the first come first serve strategy(FIFO).

When a data element is inserted a node is created and it is inserted to the tail of the existing list if there is a list already.

If there is no existing list the node is created and made as to the head of the list.

Enqueue function adds a node to the list and the dequeue function removes the top element from the lists.

If there are no elements in the list then the dequeue function returns 0 as the result.

Algorithm👨‍🎓

Algorithm for enqueue:

• A new node is created using SintNode n=new SintNode(num) with the num initialized in the node.

• If the head of the list is null then the node is made the head and tail of the list.

• If there is an existing list the new node is added as the next node to the tail and the inserted node is made as to the tail node of the list.

Algorithm for dequeue:

• If the list is empty then the dequeue function returns zero as the result.

• Else the node element stored in the head is returned and the next node is made as to the head of the list.

Algorithm for getMin():

• A variable min is initialized with 0 as the value.

• The loop continues until the list is not null.

• If the curr. num value is less than the min value then min is assigned with the curr. num value.

The min variable value is returned as the output.

Conclusion⚔

Have you come this question in your interview before? Share it in the comment section below. 🤝

Don’t forget to hit the follow button✅to receive updates when we post new coding challenges. Tell us how you solved this problem in the comments section below. 🔥 We would be thrilled to read them. ❤

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved. (HackerRank) 🐱‍💻

Click HERE 🧨🎊🎃

This is completely free 🆓 if you have an amazon kindle subscription.