Home > Mobile >  Queue sort | Help | Link
Queue sort | Help | Link

Time:11-18

I need a help with code, when i compile code, evrething ok, when i choose remove, evrething ok, but when i write my val, i see that error "Exception thrown: Read access violation. front was nullptr."

In main can be something problems, its because i send not all code, just part when i use my function bool remove_Queue(Item& i, Queue* front, Queue* back, int& x)

code :

typedef int Item; 
const int MAX_QUEUE = 10; 
int cont = 0; 


typedef int Item;
struct Queue {
    Item value;
    
    Queue* next;
};

Queue* front;

Queue* back;
Queue* tmp;

bool insert_Queue( Item& i, Queue* front, Queue* back,Item x)
{ 
    tmp = new Queue;
    tmp->next = NULL;
    tmp->value = 1;
    front = back = tmp;


    return true;
}

bool remove_Queue(Item& i, Queue* front, Queue* back,  int& x)
{
    if (front == NULL) return false; 
    i = front->value;
    x --; 
        Queue* tmp = front; 
        front = front->next;
        delete tmp; 
    if (x == 0) back = NULL;
    return true;
}

int main()
    {
    while (1)
    {
        printf("\t\tChoose\n\n");
        printf("1.Stack\t\t2.Queue\t\t3.List\n\n");
        printf("Choose: ");
        scanf_s("%d", &choice1);
        while (1)
        {
            printf("\nOperations performed by Queue");
            printf("\n1.Insert\t\t2.Remove\n3.Print queue\t\t4.Pop from start\n5.Count\t\t6.Is empty?\n7.Delete Queue\t\t8.Exit");
            printf("\n\nEnter the choice: ");
            scanf_s("%d", &choice2);
            switch (choice2)
            {
            case 1:
            {
                int i;
                printf("input value:");
                scanf_s("%d", &i);
                insert_Queue(i, front, back, cont);
                x  = 1;
                break;
            }
            case 2:
            {
                Item val;
                printf("input value:");
                scanf_s("%d", &val);
                remove_Queue(val, front, back, x);
                break;
            }
            case 3:
            {
                print_Queue(front);
                break;
            }
            case 4:
            {

                break;
            }
            case 5:
            {
                count_Queue(x);
                break;
            }
            case 6:
            {
                isЕmpty_Queue(front);
                break;
            }
            case 7:
            {
                delete_Queue(front, back, x);
                break;
            }
            case 8:
            {
                exit(0);
            }

            default: printf("\n\n\nInvalid choice!!\n\n\n");
            }
        }
    }
    return 0;
}

I expecting when i call function remove_Queue, my program will remoove value which in start of Queue

CodePudding user response:

If I understood well. You want to pop the first element of the queue. In that case the only code that you need is this(Although I think that there is a better implemetation of a queue which will make your job easier):

bool remove_Queue(Queue* front,Queue* back)
{
    if(front == nullptr)
      return false;
    else
    {
       if(front == back)
       {
          front = back = nullptr;
       }
       else
       {
          Queue* temp = front;
          front = front->next;
          temp->next = nullptr;
          delete temp;
       }
    }
    return true;

}

CodePudding user response:

A few notes regarding the code:

  • No need to pass front, back, and cont around as they are defined as global variables.
  • For insertions at front:
    • New nodes can be created with auto tmp{ new Queue{i, front} };.
      I'd recommend using smart pointers instead, though.
    • back only needs to be updated if the queue was previously empty.
  • For removals at front:
    • x was being updated instead of cont.
    • No need to ask the user to input a value, unless we wanted to remove an item in a given position or with that given value.
    • I'd also recommend using nullptr instead of NULL in C code (since C 11).

I've done some changes to the posted code, removing the parts that were not implemented, and leaving only a possible implementation for a push_front, pop_front, and print_queue methods (I've assumed insert_Queue and remove_Queue were doing insertions and removals at front).

You can have a look here.

  • Related