Rubikted
Rubikted

Reputation: 41

Deleting Values in Queue algorithm C#

I want to delete items out of a queue, I am able to delete values and then add values back, but when I then try to delete the values, it replaces the old values with the new values. for example if i add the values 1,2,3,4,5,6,7,8,9,10 and then delete values 1 and 2, and add values 11 and 12. When I then start deleting the rest of the values it skips value 9 and 10 as 11 and 12 has replaced them e.g. delete values 3,4,5,6,7,8, 11, 12 but I don't that to happen, it want it to show deleted values, 3,4, 5, 6, 7, 8, 9, 10, 11, 12. How can I do this?

namespace queue
{
class Program
{
    static void Main(string[] args)
    {

        int max = 10;
        int[] a;
        a = new int[max];
        int input; 
        int front = 0;
        int rear = 0;
        int j = 0;

        do
        {
            Console.WriteLine
                 ("What do you want to do?");

            Console.WriteLine();

            Console.WriteLine("Add = 1");
            Console.WriteLine("Dequeue = 2");
            Console.WriteLine("Exit = 3");

            Console.WriteLine();

            input = int.Parse(Console.ReadLine());

            Console.WriteLine();

            if (input == 1)
            {
                Console.WriteLine("Add");

                if (rear != max)
                {
                    Console.WriteLine("What do you want to add?");
                    int v; //value

                    v = int.Parse(Console.ReadLine());
                    a[j] = v;
                    j++;
                    rear++;

                }

                else
                    Console.WriteLine("Queue is full");
            }

            else if (input == 2)

            {
                Console.WriteLine();
                Console.WriteLine("Dequeue");

                if (rear != 0)
                {

                    Console.WriteLine("The dequeue value is:" + a[front++]);
                    rear--;
                    j--;
                }

                else

                    Console.WriteLine(" Queue is empty");
            }

            else if (input == 3)
            {
                Console.WriteLine("Exit program");
                Console.WriteLine();
         
            }

            else

            {
                Console.WriteLine("Enter option");
                Console.WriteLine();
            }

        } while (input != 'X');

    }
}
}

Upvotes: 0

Views: 794

Answers (2)

Marco Salerno
Marco Salerno

Reputation: 5201

This is how to do it using a Queue<T>, some optimizations, better readability and so on..

class Program
{
    static void Main(string[] args)
    {
        int maxListLength = 10;
        Queue<int> numbers = new Queue<int>(maxListLength);
        int inputInteger = 0;
        string input = string.Empty;

        while (true)
        {
            Console.WriteLine("What do you want to do?\n\nAdd = 1\nDequeue = 2\nExit = 3\n");
            input = Console.ReadLine();

            if (int.TryParse(input, out int convertedInput))
            {
                inputInteger = convertedInput;
            }
            else
            {
                Console.WriteLine("Input must be an integer");
                continue;
            }

            switch (inputInteger)
            {
                case 1:
                    Console.WriteLine("You choose to Add");
                    if (numbers.Count != maxListLength)
                    {
                        Console.WriteLine("What value do you want to add?");
                        input = Console.ReadLine();

                        if (int.TryParse(input, out convertedInput))
                        {
                            inputInteger = convertedInput;
                            numbers.Enqueue(inputInteger);
                        }
                        else
                        {
                            Console.WriteLine("Input must be an integer");
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine("The Queue is full");
                    }
                    break;
                case 2:
                    Console.WriteLine("You chose to Dequeue");
                    if (numbers.Count > 0)
                    {
                        Console.WriteLine("The dequeue value is: " + numbers.Dequeue());
                    }
                    else
                    {
                        Console.WriteLine("The Queue is empty");
                    }
                    break;
                case 3:
                    Console.WriteLine("Exit program\nPress a button to exit");
                    Console.ReadLine();
                    return;
                    break;
                default:
                    Console.WriteLine("Input must be 1, 2 or 3");
                    continue;
                    break;
            }
        }
    }
}

Upvotes: 2

ilyes
ilyes

Reputation: 372

If you choose to use an array you must move all the elements after the first element when you dequeue, which is not a good idea. So if you choose to use a list it would be easier and hear your program modified with using a list instead.

using System;
using System.Collections.Generic;

namespace queue
{
    class Program
    {
        static void Main(string[] args)
        {

            int max = 10;
            var items = new List<int>();
            int b; //user input
            int nbElements = 0;

            do
            {
                Console.WriteLine
                     ("What do you want to do?");

                Console.WriteLine();

                Console.WriteLine("Add = 1");
                Console.WriteLine("Dequeue = 2");
                Console.WriteLine("Exit = 3");

                Console.WriteLine();

                b = int.Parse(Console.ReadLine());

                Console.WriteLine();

                if (b == 1)
                {
                    Console.WriteLine("You choose to Add");

                    if (nbElements != max)
                    {
                        Console.WriteLine("What value do you want to add?");
                        int v; //value

                        v = int.Parse(Console.ReadLine());
                        items.Add(v);
                        nbElements++;

                    }

                    else
                    {
                        Console.WriteLine("The Queue is full");
                        foreach (var item in items)
                        {
                            Console.WriteLine(item);
                        }
                    }

                }

                else if (b == 2)

                {
                    Console.WriteLine();
                    Console.WriteLine("You chose to Dequeue");

                    if (nbElements != 0)
                    {

                        Console.WriteLine("The dequeue value is:" + items[0]);
                        items.RemoveAt(0);
                        nbElements--;
                    }

                    else

                        Console.WriteLine(" Queue is empty");
                }

                else if (b == 3)
                {
                    Console.WriteLine("Exit program");
                    Console.WriteLine();
                    Console.WriteLine("Press Enter to exit");
                    Console.ReadLine();
                    Environment.Exit(0);

                }

                else

                {
                    Console.WriteLine("Enter a valid option");
                    Console.WriteLine();
                }

            } while (b != 'X');

        }
    }
}

Upvotes: 0

Related Questions