Solving: Why Destroying a Queue Doesn't Work

  • Thread starter AcecA
  • Start date
  • Tags
    Queue
In summary: After you have deleted the first item in the queue, you can delete the queue itself.In summary, the algorithm given in the code does not properly destroy the queue due to a couple of issues. The elts member should be a pointer to _Queue instead of int. Additionally, the algorithm does not properly traverse the queue to delete all elements before deleting the queue itself. This results in q still having a value even after the queue has been "destroyed".
  • #1
AcecA
12
0

Homework Statement


All I need is an explanation as to why the algorithm I've been given doesn't destroy the queue properly. When I use the code below, and I check if q is NULL or not, it says it isn't, and I'd like to know why. Thanks :)

Homework Equations


None, this is computer science.
Langauge is C with some C++ conventions, by the way

The Attempt at a Solution



Code:
typedef int Element;

struct _Queue {
     Element *elts;
     int head;
     int capacity;
     int count;
};

typedef _Queue* Queue;

void DestroyQueue(Queue q)
{
     delete[] q->elts;
     q->elts = NULL;
     q->head = 0;
     q->count = 0;
     delete q;
     q = NULL;
}
 
Physics news on Phys.org
  • #2
AcecA said:

Homework Statement


All I need is an explanation as to why the algorithm I've been given doesn't destroy the queue properly. When I use the code below, and I check if q is NULL or not, it says it isn't, and I'd like to know why. Thanks :)


Homework Equations


None, this is computer science.
Langauge is C with some C++ conventions, by the way

The Attempt at a Solution



Code:
typedef int Element;

struct _Queue {
     Element *elts;
     int head;
     int capacity;
     int count;
};

typedef _Queue* Queue;

void DestroyQueue(Queue q)
{
     delete[] q->elts;
     q->elts = NULL;
     q->head = 0;
     q->count = 0;
     delete q;
     q = NULL;
}

I see a couple of problems. Your elts member is a pointer to Element, which means it is a pointer to an int. It probably should be a pointer to a _Queue, otherwise I can't understand what is is being used for.

Normally in a queue data type, you have one member that points to another queue element, that in turn points to another queue element, and so on until the last element in the queue. The pointer in the last queue item has the value NULL, marking the end of the queue.

You can't be sitting in one element and delete all the other elements in one fell swoop. You need to traverse the queue to the next-to-last element, delete the last element, back up to the previous element, delete the one that that element is pointing to, and so on, backing you way until you get to the first item in the queue. In case you didn't follow that, you need to back your way through the queue, deleting the current last item.
 

What is a queue and why is it important?

A queue is a data structure that follows the "first in, first out" (FIFO) principle. This means that the first item added to the queue will be the first one removed. It is important because it allows for efficient data management, especially in situations where data needs to be processed in a specific order.

Why is destroying a queue not a viable solution?

Destroying a queue means permanently deleting all of its elements and removing its structure. This is not a viable solution because it eliminates the benefits of using a queue, such as maintaining data order and efficient data management. It also means that any data that was in the queue will be lost and cannot be recovered.

What are the consequences of destroying a queue?

The consequences of destroying a queue include losing all data in the queue, disrupting any processes or systems that rely on the queue, and potentially causing errors or malfunctions in the code or program. It can also lead to inefficiency and disorganization in data management.

Are there alternatives to destroying a queue?

Yes, there are alternatives to destroying a queue. One option is to simply empty the queue without deleting its structure. This allows for the queue to still maintain its benefits, but with an empty state. Another option is to implement a queue with a limited capacity, so that when it reaches its maximum capacity, new data can be added while old data is removed, without completely destroying the queue.

What are some appropriate use cases for using a queue?

A queue is commonly used in situations where data needs to be processed in a specific order, such as in scheduling tasks, handling requests, or managing resources. It is also useful in situations where data needs to be stored temporarily and retrieved in the same order it was added, such as in a printer queue or a messaging system.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top