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.
Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.
implement the queue ADT using an array
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.
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.
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.
Yes it is possible to implement stack and queue using linked list
Add weights to the elements of the queue and use an algorithm to sort the queue every time an element is added.
implement the queue ADT using an array
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.
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.
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.
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.
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 arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.
To implement a queue using stacks efficiently, you can use two stacks. One stack is used for enqueueing elements, and the other stack is used for dequeueing elements. When dequeueing, if the dequeue stack is empty, you can transfer elements from the enqueue stack to the dequeue stack to maintain the order of elements. This approach allows for efficient implementation of a queue using stacks.
Evaluating a Polynomial expression using a singly linked list visit : http://myfundatimemachine.blogspot.in/2012/06/polynomial-evaluation-in-c.html
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.