Colas 121111215201 Phpapp02
Colas 121111215201 Phpapp02
Colas 121111215201 Phpapp02
COLAS
Definicion. Es una lista lineal de elementos en la que
las operaciones de insertar y eliminar se realizan en
diferentes extremos de la cola.
Trabajan con filosofa FIFO ( First In - First out), el
primer elemento en entrar es el primer elemento en
salir.
Ejemplos:
Cola
P
(PRIMERO)
U
(LTIMO)
TIPOS DE COLAS:
Operaciones:
1.- Insertar A
Estado de la cola:
Inicio: Cola
Vaca
A
2.- Insertar B
3.- Insertar C
4.Elemento
Remover
5.- Insertar D
6.- Remover
Elemento
Frente
2
S
3
D
4
Z
Final
Final
Al remover un
elemento:
Frent
e
B
Frent
e
Insertar elemento
D:
Final
C
Final
C
Insertar elemento E:
Frent
e
B
Final
C
E
Final
Insertar elemento F:
Frent
e
B
F
Insertar elemento G:
Error: Cola llena!!!!
Representacin de colas:
Usando memoria esttica: arreglos con tamao
fijo.
Final
Frent
e
B
F
D
2
E
3
Final
Frente
Cola simple
Es una lista lineal de elementos en la que las
operaciones de insertar y eliminar se realizan en
diferentes extremos de la cola. Para lo cual hace uso de
dos indices frente y final.
Este tipo de cola presenta desaprovechamiento de
memoria al momento que se retiran elementos de la cola
ya que esa localidad queda libre; para evitar este
desaprovechamiento de memoria
tenemos dos
soluciones:
1.- Mover todos los elementos de la cola una posicin
cada vez que se quiere retirar un elemento de la cola
2.- Cuando final =max-1 y frente >0 mover todos los
elementos.
Insertar_Cola(1);
Insertar_Cola(2);
Insertar_Cola(3);
Insertar_Cola(4);
Insertar_Cola(5);
retirar_Cola();
retirar_Cola();
Insertar_Cola(7);
Frent
e
Final
1
5
Frent
e
3
Error no se puede
final esta en max-1
Final
4
Ver ejemplo
Cola_Sencilla1
Insertar_Cola(1);
Insertar_Cola(2);
Insertar_Cola(3);
Insertar_Cola(4);
retirar_Cola();
retirar_Cola();
Frent
e
Final
3
Final
Frent
e
Final
Frent
e
Colas Circulares
Es una variacin de las colas sencillas para
hacer un uso ms eficiente de la memoria
disponible, se trata a la estructura de cola
de forma circular. Es decir el elemento
anterior al primero es el ltimo
COLA CIRCULAR
COLA
2
3
4
P=0
U=0
COLAS
2) Se inserta A,B,C,D,E
P=1
U=5
COLA
2
3
4
3) Eliminar A
P=2
U=5
4) Insertar F
P=2
P=2
U=5
U=1
U
COLAS
2
1
P
B
INSERTAR F
C
E
5
4
U
SI U+1 = P
COLA CIRCULAR
LLENA
2
1
B
3
F
C
E
5
D
4
COLAS
OTRO EJEMPLO: N= 5
1) Se inserta A,B,C,D,E
P=1
U=5
COLA
2
3
4
2) Se inserta F
NO SE PUEDE INSERTAR F PORQUE LA
COLA CIRCULAR LLENA, U=N y P=1.
SRR
COLAS
Cola Circular
Es una representacin
lgica de la cola en
un arreglo.
El frente y final son
movibles.
Cuando el frente o final
llegan al extremo se
regresan a la primera
posicin del arreglo.
Frent
e
Cola
inicial
Final
Frent Final
e
Remover
C
Frent
e
Insertar E
Final
C
E
Final
Insertar F
F
E
Frent
e
C
ColaCirc_llena()
ColaCirc_vacia()
InsColaCirc(Object dato)
RetColaCirc()
BICOLA
Una bicola:
es un conjunto de elementos en el cual se
pueden aadir o quitar elementos de cualquier
extremo de la misma.
Los dos extremos de una bicola los llamaremos
izquierdo y derecho
Representacion esttica
Se mantienen los elementos de la bicola
en un arreglo circular.
Se utilizan dos variables indice Izquierdo y
Derecho.
Final: 4
luz
agua
ri
Final: 4
sql
luz
agua
ri
sql
luz
agua
Problema:
Colas en Java
Java contiene la definicin de interfaces y
clases para el manejo de colas.
Las colas son una coleccin de elementos
diseadas para almacenar elementos que
esperan ser procesados.
Java contiene una interface parametrizada
Queue<E> y varias clases que que la
implementan, entre ellas PriorityQueue<E>
Colas en Java
public interface Queue<E> extends
Collection<E> {
E element();
boolean offer(E o);
E peek();
E poll();
E remove();
}
Method Summary
E element()
Retrieves, but does not remove, the head of this queue, it throws an exception if this
queue is empty..
boo offer(Eo)
lean
Inserts the specified element into this queue, if possible.
E
peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is
empty.
E
poll()
Retrieves and removes the head of this queue, or null if this queue is empty.
E remove()
Retrieves and removes the head of this queue.
Type Parameters:
E - the type of elements held in this collection
Constructor Summary
PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to
their natural ordering (using Comparable).
PriorityQueue(Collection<? extends E>c)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to
their natural ordering (using Comparable).
PriorityQueue(intinitialCapacity, Comparator<? super E>comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the
specified comparator.
PriorityQueue(PriorityQueue<? extends E>c)
Method Summary
boolean
void
Comparat
or<?
super E>
Iterator<
E>
boolean
add(Eo)
Adds the specified element to this queue.
clear()
Removes all elements from the priority queue.
comparator()
Returns the comparator used to order this collection, or null if this collection is sorted
according to its elements natural ordering (using Comparable).
iterator()
Returns an iterator over the elements in this queue.
offer(Eo)
Inserts the specified element into this priority queue.
peek()
Retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
poll()
Retrieves and removes the head of this queue, or null if this queue is empty.
boolean
int
remove(Objecto)
Removes a single instance of the specified element from this queue, if it is present.
size()
Returns the number of elements in this collection.