Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds


App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!



Implementing a queue using array - Practice Exercises Java


Lesson 11:

Dynamic memory management


Exercise 11.1:

Implementing a queue using array


Objetive:

Implementing a queue


Source Code:


import java.util.*;

public class Cola
{
	private int[] pila;
	private int posicion;
	private int tamanyo;

	public Cola(int tamanyo)
	{
		posicion = -1;
		this.tamanyo = tamanyo;

		// Iniciamos el array
		pila = new int[this.tamanyo];
	}

	public final void Encolar(int valor)
	{
		// Aumentamos posición
		posicion++;

		// Guardamos el valor
		pila[posicion] = valor;
	}

	public final int Desencolar()
	{
		int aux = pila[0];

		for (int i = 0; i < posicion; i++)
		{
			pila[i] = pila[i + 1];
		}

		pila[posicion] = aux;

		posicion--;

		// Retornamos el ultimo valor
		return pila[posicion + 1];
	}
}

public class TestCola
{
	public static void main(String[] args)
	{
		boolean depurando = false;

		Cola cola = new Cola(2);

		cola.Encolar(1);
		cola.Encolar(2);

		System.out.println(cola.Desencolar());
		System.out.println(cola.Desencolar());

		if (depurando)
		{
			new Scanner(System.in).nextLine();
		}
	}
}
Exercisey 11.1




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.