answersLogoWhite

0


Best Answer

Queues are a first in first out structure (FIFO). This means all extractions occur at the head of the list and all insertions occur at the tail. This requires that you maintain pointers to the head and tail node to allow constant time insertion and extraction of nodes. The nodes themselves are singly linked, each pointing to the next node. The tail node always points to NULL until a new node is insert, which the tail points to. The new node then becomes the tail. When extracting a head node, its next node (which may be NULL if it is also the last node) becomes the new head of the list. If the head is NULL, the tail is also set to NULL.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

A queue is easily implemented using a single linked list. The head of the queue equals the head of the list. The list nodes are linked so that the list can be traversed from head to tail.

To drop the head of the queue, make head.next the new head.

To add a new item to the queue, traverse the queue to its end, then add the new item.

You could also turn the list around and define the tail of the list as the head of the queue.

The decision where to anchor the queue is determined by the performance requirements. As traversal along the list is time-consuming, the least time-critical of the two operations (append new item, pop head of queue) should be the one that traverses the list.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You can implement a queue via linked list (practically with a bidirectionally linked list, as a queue has operations like 'add_first' and 'get/del_last')

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement queue using singly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


Queue ADT Using Array?

implement the queue ADT using an array


How do you implement queue using stack in c plus plus?

A queue in any language is a singly-linked list structure that permits new data to be inserted only at the end of a list while existing data can only be extracted from the beginning of the list. Queues are also known as a first in, first out (FIFO) structure. Unlike a standard singly-linked list, the list maintains a pointer to the last node as well as the first, in order to insert new data and extract existing data in constant time. Variations on the queue include the priority queue which permit data to be weighted, such that data with the greatest priority is promoted to the front of the queue, behind any existing data with the same or higher priority, using an insertion sort technique. Insertion is therefore achieved in linear time rather than constant time, however extraction is always in constant time.


Implement a queue using a singly linked list l the operations insert and delete should still take o1time?

Suppose we have a Queue as follows: E -> D -> C -> B We maintain a head pointer to E and a tail pointer to B. To add an element, we make a new queue item (say A), we set B->next = A, and set tail = A. To delete an element, we let a temp pointer to the head (say tempHead = head), set head = E->next and deallocate tempHead.


When can we say that a queue is full using double linked lists?

If you use a doubly-linked list to implement a queue then the queue is full whenever it is not empty. However, given that a list dynamically shrinks and expands to accommodate all the elements within it, we can never really say it is full per se because its true capacity is ultimately determined by the amount of available memory and that can vary from one moment to the next. Note that you do not need to use a doubly-linked list to implement a queue since there is no need to physically traverse a queue. We only need constant-time access to the head (for extractions) and the tail (for insertions). We still need to maintain links between the nodes, however just one link per node would suffice for this. As such, an adapted singly-linked list (also known as a forward list) would consume less memory than a doubly-linked list would. The only change we need to make is to add one pointer to keep track of the tail node. Note also that a singly-linked list generally does not maintain a count of its elements which helps improve efficiency even further. It's usually sufficient to know if the list is empty or not empty and we can easily determine that in constant-time simply by examining the head node state; if it points to null, then the list is empty otherwise it is not empty (or "full" if you prefer). The same applies to a queue; we're usually only interested in knowing if a queue actually exists before we extract the front element, and that can only be the case if there's at least one element in the queue (i.e., the queue is not empty). The actual number of elements is rarely of any importance, but we can easily adapt a singly-linked list to accommodate a count if we really needed one.

Related questions

Is it possible to implement stack and queues using linkes list?

Yes it is possible to implement stack and queue using linked list


Queue ADT Using Array?

implement the queue ADT using an array


How do you write a Java program to implement weighted queue using circular doubly linked list?

Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.


How do you implement queue using stack in c plus plus?

A queue in any language is a singly-linked list structure that permits new data to be inserted only at the end of a list while existing data can only be extracted from the beginning of the list. Queues are also known as a first in, first out (FIFO) structure. Unlike a standard singly-linked list, the list maintains a pointer to the last node as well as the first, in order to insert new data and extract existing data in constant time. Variations on the queue include the priority queue which permit data to be weighted, such that data with the greatest priority is promoted to the front of the queue, behind any existing data with the same or higher priority, using an insertion sort technique. Insertion is therefore achieved in linear time rather than constant time, however extraction is always in constant time.


Implement a queue using a singly linked list l the operations insert and delete should still take o1time?

Suppose we have a Queue as follows: E -> D -> C -> B We maintain a head pointer to E and a tail pointer to B. To add an element, we make a new queue item (say A), we set B->next = A, and set tail = A. To delete an element, we let a temp pointer to the head (say tempHead = head), set head = E->next and deallocate tempHead.


When can we say that a queue is full using double linked lists?

If you use a doubly-linked list to implement a queue then the queue is full whenever it is not empty. However, given that a list dynamically shrinks and expands to accommodate all the elements within it, we can never really say it is full per se because its true capacity is ultimately determined by the amount of available memory and that can vary from one moment to the next. Note that you do not need to use a doubly-linked list to implement a queue since there is no need to physically traverse a queue. We only need constant-time access to the head (for extractions) and the tail (for insertions). We still need to maintain links between the nodes, however just one link per node would suffice for this. As such, an adapted singly-linked list (also known as a forward list) would consume less memory than a doubly-linked list would. The only change we need to make is to add one pointer to keep track of the tail node. Note also that a singly-linked list generally does not maintain a count of its elements which helps improve efficiency even further. It's usually sufficient to know if the list is empty or not empty and we can easily determine that in constant-time simply by examining the head node state; if it points to null, then the list is empty otherwise it is not empty (or "full" if you prefer). The same applies to a queue; we're usually only interested in knowing if a queue actually exists before we extract the front element, and that can only be the case if there's at least one element in the queue (i.e., the queue is not empty). The actual number of elements is rarely of any importance, but we can easily adapt a singly-linked list to accommodate a count if we really needed one.


How do you Write a program in C programming language to implement a queue using singly linked list?

A queue is a first in first out (FIFO) data structure. We can use a linked list to implement a queue by pushing new elements onto the end of the list and extracting existing elements from the front of the list. Given the simplicity of the structure, we do not require the full functionality of a linked list (a doubly-linked list), which includes bi-directional traversal and insertion/extraction elsewhere in the list. We also do not need to maintain a count of the elements. We can prevent bi-directional traversal simply by using a forward list (a singly-linked list). This also gets rid of the need to maintain a count of the elements because a forward list does not (or at least should not) maintain a count. A forward list allows constant-time insertion and extraction at the head only, so we need to change the representation to include a pointer to the tail. All insertions will be made at the tail and all extractions at the head, so we need to modify the push and pop methods accordingly. All other insertion/extraction methods can be removed completely. We can also get rid of any iterators into the list since we do not need to traverse a queue. The only other accessor we need is the empty() accessor so we can tell if the queue is empty or not. With these modifications in place, we now have a well-defined queue.


Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


Evaluate the polynomial expression using linked list using c?

Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html


How do you solve josephus problem using circular linked list?

The Josephus problem is a problem to locate the place for the last survivour. It shows the power of the circular linked list over the singly linked lists.


What is a singly linked linear list?

A singly linked list is a linked list which only provides links in "one direction". Using a metaphor, a singly linked list is a one way street, while a doubly linked list is a two way street. Once you move forward in a singly linked list, there is no way to go backwards unless you kept your reference/pointer from before. A singly linked list would look like this: start ----> node1---->node2---->node3 ----> NULL You will see that node2 only has a link forward to node3 - it does not have a link backwards to node1, even though node1 has a link forwards to node2. To prevent us from permanently losing access to portions of the linked list, we generally keep a reference/pointer to "start". A doubly linked list would have twice the number of pointers/references as a singly linked list - making it very inefficient to store small datatypes. On the other hand, it would be possible to move both forwards and backwards with a doubly linked list because you have links pointing both forwards and backwards.


How do you implement a doubly linked list by using singly linked list?

Add another pointer to the nodes for the previous node: struct node { struct node *next; struct node *previous; void *data; }; typedef struct node node; Then change the logic for insertion and removal to make sure you set the previous pointer as well as the next one.