Queue (computing)
A queue (also called row) is an abstract data type, characterized by being a sequence of elements in which the insertion operation push is performed on one end and the pull operation on the other. It is also called a FIFO (First In First Out) structure, because the first element in will also be the first out.
Queues are used in computer systems, transportation, and research operations (among others), where objects, people, or events are taken as data that is stored and held by queues for further processing. This type of abstract data structure is implemented in object-oriented languages by classes, in the form of linked lists.
Specific uses of the tail
The particularity of a queue is the fact that we can only access the first and the last element of the structure. Likewise, elements can only be removed at the beginning and can only be added at the end of the queue.
Examples of queues in real life would be: people shopping at a supermarket, waiting to go in to see a baseball game, waiting at the cinema to see a movie, a small hairdresser, etc. The essential idea is that they are all waiting lines.
Additional information
If it is empty, deleting an element would be impossible until a new element is added. When adding an element we could give more importance to some elements than to others (a VIP charge) and for this a special type of queue is created which is the priority queue. (See priority queue).
Basic Operations
- Create: creates the empty tail.
- Encolar: one element is added to the tail. It is added at the end of this.
- Uncolar: (sacar, exit, remove): the front element of the tail is removed, that is, the first element that entered.
- Front: (check, front): the front element of the tail is returned, that is, the first element that entered.
Implementations
Queues can be implemented using Linked Lists or using a variation of the array commonly known as a circular queue. The advantage of the circular queue is that contiguous memory in RAM can be used which allows for better cache performance than a linked list plus less RAM usage. These may or may not grow dynamically.
C tails
# Include≤2.h# Include≥stdlib.h#define RED ##x1B[31m]♪ Define GRN ♪ ♪Define YEL "x1B[33m]#define BLU "x1B[34m]#define MAG "x1B[35m]#define CYN ##x1B[36m]Define WHT "x1B[37m]♪ Define RESET ♪typedef struct Node{struct Node ♪next;struct Node ♪previous;Char ♪data;!node_t;typedef struct Queue{node_t ♪top;node_t ♪bottom;int size;!queue_t;node_t ♪createNode(Char♪);Char♪ Don't remove(node_t♪);queue_t ♪createQueue();int Remove(queue_t ♪);Char ♪peek(queue_t ♪);int isEmpty(queue_t ♪);/* If we handle a static Queue...int isFull(queue_t *);*/int when(Char ♪, queue_t ♪);Char ♪after(queue_t ♪);int printQueue(queue_t ♪);int printNode(node_t ♪);int getQueueSize(queue_t ♪);node_t ♪createNode(Char ♪data♪node_t ♪node = (node_t ♪)Shut up.(1, sizeof(node_t));node- 2005data = data;return node;!Char♪ Don't remove(node_t ♪node♪ Char♪ data = NULL; if (node) { if (node- 2005previous) node- 2005previous- 2005next = node- 2005next; if (node- 2005next) node- 2005next- 2005previous = node- 2005previous; data = node- 2005data; free(node); ! return data;!queue_t ♪createQueue(){queue_t ♪What? = (queue_t ♪)Shut up.(1, sizeof(queue_t));return What?;!int Remove(queue_t ♪What?♪ if (What?) { node_t ♪ptr = What?- 2005top; node_t ♪aux; while(ptr = NULL♪ aux = ptr; ptr = ptr- 2005next; free(Don't remove(aux)); ! free(What?); ! return (What? ♪ NULL);!Char ♪peek(queue_t ♪What?♪ if (What? " fake " What?- 2005top = NULL♪ return What?- 2005top- 2005data; ! return NULL;!int isEmpty(queue_t ♪What?♪return (What?- 2005top ♪ NULL);!int when(Char ♪data, queue_t ♪What?♪if(What? = NULL♪node_t ♪new = createNode(data);if(What?- 2005bottom ♪ NULL♪What?- 2005bottom = new;What?- 2005top = new;//queue-tenth-tenth-purvious = queue-ghorbottom;//No need to set previous for bottom since calloc makes it null.!else{What?- 2005bottom- 2005next = new;new- 2005previous = What?- 2005bottom;new- 2005next = NULL;What?- 2005bottom = new;!What?- 2005size+;return EXIT_SUCCESS;!return EXIT_FAILURE;!Char ♪after(queue_t ♪What?♪if(What? = NULL " fake " What?- 2005top = NULL♪Char ♪data;node_t ♪aux = What?- 2005top;What?- 2005top = aux- 2005next;What?- 2005top- 2005previous = NULL;data = Don't remove(aux);What?- 2005size--;return data;!return NULL;!int printQueue(queue_t ♪What?♪node_t ♪ptr = What?- 2005top;int i, size = getQueueSize(What?);for(i = 0; i.size; i+♪#ifdef __unix__ printf(GRN"[%i]Address: %p, Data: %, Next: %p, Previous: %pn"RESET, i, ptr, ptr- 2005data, ptr- 2005next, ptr- 2005previous);#elif __WIN32printf("[%i]Address: %p, Data: %, Next: %p, Previous: %pn", i, ptr, ptr- 2005data, ptr- 2005next, ptr- 2005previous);#endifptr = ptr- 2005next;!printf("n");return EXIT_SUCCESS;!int printNode(node_t ♪ptr♪Char ♪i = "printNode function";#ifdef __unix__ printf(GRN"[%s]Address: %p, Data: %, Next: %p, Previous: %pn"RESET, i, ptr, ptr- 2005data, ptr- 2005next, ptr- 2005previous);#elif __WIN32 printf("[%s]Address: %p, Data: %, Next: %p, Previous: %pn", i, ptr, ptr- 2005data, ptr- 2005next, ptr- 2005previous);#endif printf("n"); return EXIT_SUCCESS;!int getQueueSize(queue_t ♪What?♪return What?- 2005size;!
Queues in Pascal
Class PscColas, Matriz[]:Chain, Position, Value:Entero Private: Proc Start ReDim Matrix,1 Position = 0 Value = 0 FinProcProc Finish Delete Matrix FinProcProc Length:EnteroReturn Limit(Matriz) FinProcProc Dimension theCola ReDim Preservation Matrix, LongMat(Matriz) + 1 FinProc Public: Proc Encolar(Content:Cadena) Yeah. Position = LongMat(Matriz) Then Dimension theCola Matrix[position] = Contents Position = Position + 1 FinProcProc Uncool Yes(Value ) Limit(Matriz) Then Value = Value + 1 FinProcProc FrontCola:Cadena Return Matrix[Value] FinProcProc FundCola:ChainReturn Matriz[Limit(Matriz) FinProcProp ColaLongitude:EnteroLec:Longitude FinProp Private: Builder: Start Destroyer: Finish FinClase
Queues at Maude
The NVQueue is the non-empty queue, which we differentiate from the normal queue when taking errors into account. In turn, the X element represents the type of value that the tail can contain: integer, character, record...
fmod COLA {X:: TRIV} is draws ColaNV{X} Cola{X}. subsort ColaNV{X}. Cola{X}. *** generators o create: - rigid Cola{X} [ctor]. o encolar: X$Elt Cola{X} - 2005 ColaNV {X} [ctor]. *** builders o desencolar: Cola{X} - tax Cola{X}. *** selectors op front: ColaNV{X} - 2005, X$Elt. *** variables var C: ColaNV{X}. vars E E2: X$Elt. *** equations eq decolar(create) = create. eq desencolar(encolar(E, create)) = create. eq decolar(encolar(E, C)) = encolar(E, decolar(C)). eq front(encolar(E, create)) = E. eq front(encolar(E, C)) = front(C). endfm Specification of a queue of integers in Maude: view VInt from TRIV to INT is Elt to Int. endv view VColaInt from TRIV to COLA{VInt} is draw Elt to Cola{VInt}. endv fmod COLA-COLAS-INT is protecting INT. protecting COLA{VColaInt}. *** operations of the queue of integers o encolarInt: Int ColaNV{VColaInt} - tax ColaNV{VColaInt}. o desencolarInt: Cola{VColaInt} - censo Cola{VColaInt}. op frontInt: ColaNV{VColaInt} - plan [Int]. *** variables var CCNV: ColaNV{VColaInt}. var CC: Cola{VColaInt}. var CE: Cola{VInt}. var E: Int. *** equations eq encolarInt(E, encolar(CE, CC)) = encolar(E, CE), CC. eq desencolarInt (encolar(CE, create)) = encolar(disencolar(CE), create. eq desencolarInt (encolar(CE, CCNV)) = encolar(CE, desencolarInt(CCNV)). eq frontInt(CCNV) = front(CCNV)). endfm
Queues in C++
#ifndef COLA# Define COLA // Define the tailusing namespace std;template template template template .class T▪class Cola{ struct No.{ T element; struct No.♪ Next; // place the node in second position }; No.♪ first; No.♪ Last; unsigned int elements;public: Cola(): first(0), Last(0), elements(0) {cHFFFF} ~Cola(){ while (elements = 0) pop(); ! void push(const T" Elem♪ No.♪ aux = new No.; aux- 2005element = Elem; if (elements ♪ 0) first = aux; else Last- 2005Next = aux; Last = aux; +elements; ! void pop(){ No.♪ aux = first; first = first- 2005Next; if (Last ♪ aux♪ Last = first; ! Delete aux; --elements; ! T consult() const{ return first- 2005element; ! Bool Empty() const{ return elements ♪ 0; !
Queues in JAVA
Like stacks, this type of data structure can be implemented statically or dynamically, that is, with either an array or a linked list. When talking about a static queue, it is considered that it will have a defined size and cannot exceed said capacity for storing more information, only the indicated one. And with respect to a dynamic queue, it corresponds to one that will not have a capacity limit, that is, we can do n number of insertions.
The following is the Static Queue, which is implemented based on an array:
public class Cola Estatica{ private int tail[]; private int top;//indicates the position of the last element inserted private int capacity; public Cola Estatica(int cap♪ capacity=cap; tail=new int[chuckles]capacity]; top=-1; ! public Boolean estaVacia(){ return(top==1); ! public Boolean This one.(){ return(top+1)♪capacity); ! public void encolar(int element♪ if(This one.()♪false) tail[+++top]=element; else System.out.println("Upper overflow, you can't shrink"); ! public int Uncool(){ if(estaVacia()♪false♪ int data=tail[chuckles]0]; top--; for(int i=0;i♫top;i+♪ tail[chuckles]i]=tail[chuckles]i+1]; ! return data; ! else{ System.out.println("Lower overflow, you can't unplug"); ! return -1; ! public static void main (String args[]♪ Cola Estatica cabbage=new Cola Estatica(5); cabbage.encolar(1); cabbage.encolar(12); cabbage.encolar(3); int r=cabbage.Uncool(); System.out.println("The deleted data is "+r); Boolean b=cabbage.estaVacia(); Boolean c=cabbage.This one.(); System.out.println("Is the plate empty? "+b); System.out.println("Is the plate full? "+c); ! !
The following is the implementation of the Queue dynamically, implemented based on a simply linked list:
class No.{ int Information; No. Next; public No.(it info♪ Information=info; Next=null; !!class Cola{ No. nodeCabeza, Final; public Cola() { // Start the Cola, in this case your first state is empty nodeCabeza = null; Final = null; ! public void insert(int x) { No. new = new No.(x); if (nodeCabeza ♪ null " fake " Final ♪ null) { NodeCabeza = new; ! else { Final.Next = new; ! Final = New; ! public int eliminate(){ if (nodeCabeza ♪ null " fake " Final ♪ null) { System.out.println("empty color, you can't remove"); ! else { No. # Eliminate # = nodeCabeza; nodeCabeza = nodeCabeza.Next; # Eliminate #.Next = null; return # Eliminate #.Information; ! ! public void PrintCola(){ if nodeCabeza ♪ null " fake " Final ♪ null) { System.out.println("empty color"); ! else{ for(No. Assistant = nodeCabeza; Assistant = null; Assistant=Assistant.Next♪ System.out.print(Assistant.Information+"); ! System.out.println(); ! ! public static void main(String[] ar) { Cola tail=new Cola(); tail.insert(8); tail.insert(25); tail.insert(2); tail.PrintCola(); int v = tail.eliminate(); System.out.println("Element eliminated"+v); tail.PrintCola();!
Queues in C#
public partial class frmncipal { // Global variables public static string[] Cola; public static int Front; public static int Final; public static int N; [STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmncipal()); ! public frmncipal() // Builder { InitializeComponent(); Cola = new string[chuckles]5]; // Linear arrangement 5 N = 4; Front = -1; Final = -1; ! void CmdInsertionClick(object sender, System.EventArgs e) { frmInsertion Insertion = new frmInsertion(); Insertion.Show(); ! void CmdClick(object sender, System.EventArgs e) { frm Tour Tour = new frm Tour(); Tour.Show(); ! void CmdBusquedaClick(object sender, EventArgs e) { frmBusqueda Search = new frmBusqueda(); Search.Show(); ! void CmdEliminacionClick(object sender, EventArgs e) { frmElimination Delete = new frmElimination(); Delete.Show(); ! !
Algorithm Insert(Tail, N, Front, End, Element)
void CmdInsertClick(object sender, System.EventArgs e) { element = txtInsertion.Text; // It is verified that there is room in the Cola if (frmncipal.Front ♪ 0 " fake " frmncipal.Final ♪ frmncipal.N) { MessageBox.Show("La Cola is full"); return; ! if (frmncipal.Front ♪ frmncipal.Final + 1) { MessageBox.Show("La Cola is full"); return; ! // If the tail is empty, pointers are initialized if (frmncipal.Front ♪ -1) { frmncipal.Front = 0; frmncipal.Final = 0; ! else if (frmncipal.Final ♪ frmncipal.N) { frmncipal.Final = 0; ! else { frmncipal.Final = frmncipal.Final + 1; ! // Item added to the Cola frmncipal.Cola[chuckles]frmncipal.Final] = element; txtInsertion.Text = "; !
Elimination Algorithm (Tail, Front, End, N)
void CmdEliminarClick(object sender, EventArgs e) { if (frmncipal.Front ♪ -1) { MessageBox.Show("Clean Cola"); return; ! string element = frmncipal.Cola[chuckles]frmncipal.Front]; // if the tail has only one element if (frmncipal.Front ♪ frmncipal.Final) { frmncipal.Front = -1; frmncipal.Final = -1; ! else if (frmncipal.Front ♪ frmncipal.N) { frmncipal.Front = 0; ! else { frmncipal.Front = frmncipal.Front + 1; ! lsElimina.Items.Add(element); !
Another way to schedule a queue in Java By Jorge Herrera C.
import java.util. ♪;public class Cola .Type▪{ private Ready..Type▪ tail; public Cola(){ tail=new ArrayList.Type▪(); ! public Boolean queue(){ return tail.isEmpty(); ! public void add(Type element♪ tail.add(element); ! public Type Get out.(){ if(queue())return null; Type element=tail.get(0); tail.remove(0); return element; ! !// End of class Cola
The following is an example of a queue class handler class
public class Handle { public static void main(String[] args) { Cola tail=new .Integer▪Cola(); System.out.println(tail.Get out.()); tail.add(23); tail.add(24); tail.add(25); while(!tail.queue(){ System.out.println(tail.Get out.()); ! Cola names=new .String▪Cola(); names.add("Jorge"); names.add("Raquel"); names.add("Mayra Alejandra"); while(!names.queue(){ System.out.println(names.Get out.()); ! !!// End of class
Types of queues
- Circular waves (rings): in which the last element and the first are united.
- Priority ballsIn them, the elements are dealt with in the order indicated by a priority associated with each. If several elements have the same priority, they will be treated in a conventional manner according to their position. There are two forms of implementation:
- Add a field to each node with its priority. It is convenient to keep the line ordered by order of priority.
- Create as many queues as there are priorities, and store each element in your queue.
- Bicolas (or double-finished waves): they are tails where nodes can be added and removed on both ends; they are called DEQUE (Double Ended QUEue). To represent the bicolas we can do it with a circular array with Home and End that points to each of the ends. There are variants:
- Restricted entry balls: These are those where the insertion is only done by the end, although we can eliminate the Start or Final.
- Restricted output bottles: They are those where it is only eliminated by the end, although it can be inserted to the Start and Final.
Contenido relacionado
Stack (computing)
Sleeping barber problem
Lexicon