CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Delete all elements that are less than the average value of queue

1. Junior Member
Join Date
Apr 2018
Posts
4

## Delete all elements that are less than the average value of queue

Hello!
I need some help.
I must do the following task: create a one-way queue with numbers in the range from -50 to +50. After creating the queue, perform the following: find the average value of all queue elements and delete all elements that are less than the average value. At the end of the job, all queues must be deleted.
I was able to write code to create a queue, delete it, and find the average value of all the elements.
However, I can not implement the function of removing elements in the queue that are less than average.
Please help me write this function to remove elements that are less then average value of all the elements of the queue, I will be very grateful for your help!
Code:
```#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

struct queue{
int info;
queue *next;
}*b, *e;

void AddQueue (queue **b, queue **e, int in)
{
queue *t=new queue;
t->info=in;
t->next=NULL;
if (*b==NULL)
*b=*e=t;
else {
(*e)->next=t;
*e=t;
}
return;
}

{
if (t==NULL)
{
cout<<"Queue is empty!\n";
return NULL;
}
while (t!=NULL)
{
in=t->info;
cout<<in<<" ";
t=t->next;
}
cout<<endl;
return t;
}

void Average(queue *k, queue *e)
{
queue *p=NULL;
int number=0, sum=0;
double aver;
p=b;
while(p!=NULL)
{
number++;
sum+=p->info;
p=p->next;
}
aver=(double)sum/number;
cout<<"Average value= "<<aver<<endl;
}

void DeleteQueue (queue **b, queue **e)
{
queue *t;
while(*b != NULL)
{
t=*b;
*b=(*b)->next;
delete t;
}
*e=NULL;
}

int _tmain(int argc, _TCHAR* argv[])
{
b=e=NULL;
queue *t=NULL;
int inf, n;
double sum;
cout<<"Input the number of elements"<<endl;
cin>>n;
for(int i=0; i<n; i++)
{
inf=rand()%100-50;
}
cout<<"Queue is: "<<endl;

Average(b, e);
DeleteQueue(&b, &e);
if(b==NULL)
cout<<"Queue was deleted"<<endl;
return 0;
}```

2. ## Re: Delete all elements that are less than the average value of queue

can not implement the function of removing elements in the queue that are less than average
Do you know how to remove an element in a one-way queue by adjusting pointers? As this is presumably a homework exercise, we're not going to write the code but will give advice. When removing an element, you need to consider whether the queue is empty, whether it is the first element and top (and bottom if only one element) needs adjusting or the last element and bottom needs adjusting. If neither, then the element is in the middle of the list so the next of the previous element (so much easier if doubly linked - keep a copy of the address of the previous element) needs to be set to the next of the element to be deleted.

Once you are able to correctly delete elements and you have the required average value, then iterate the queue and delete the required elements.

Note that you don't need to iterate the queue to find the average value. Keep a total and count when adding to the queue.

If you post your attempted code we'll provide further guidance.

Note that for pointers, nullptr should be used instead of NULL. Also rather than passing a pointer to a pointer for begin and end, you could just pass a reference to a pointer. This would make the code somewhat easier.

3. Junior Member
Join Date
Apr 2018
Posts
4

## Re: Delete all elements that are less than the average value of queue

Originally Posted by 2kaud
we're not going to write the code but will give advice.
Thank you for your advice, I'm trying to understand the process itself, so I always write the code myself, so thank you very much for the advice that helped me understand the essence!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•