Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Implementing a queue using array - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

Implementing a queue using array - Practice Exercises C# Sharp


Lesson 11:

Dynamic memory management


Exercise 11.1:

Implementing a queue using array


Objetive:

Implementing a queue


Source Code:


using System;
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 void Encolar(int valor)
    {
        // Aumentamos posición
        posicion++;

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

    public 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];
    }
}

class TestCola
{
    static void Main()
    {
        bool depurando = false;

        Cola cola = new Cola(2);

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

        Console.WriteLine(cola.Desencolar());
        Console.WriteLine(cola.Desencolar());

        if (depurando)
            Console.ReadLine();
    }
}
Exercisey 11.1

Share Now:







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.