Weird Enqueue Function
Aug 1, 2012 at 11:43am UTC
Hi there ,
I have written a enqueue function for a priority queue . This doesn't have any compilation error but my program ends abruptly.
Here's the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
void Queue::enqueue(int item , int prior)
{
node *ptr = new node();
ptr->info = item ;
ptr->priority = prior ;
if (front == NULL) // queue is empty
{
ptr->next = NULL ;
front = ptr ;
}
else //queue not empty
{
node *loc , *preloc ;
while (loc->priority <= prior)
{
preloc = loc ;
loc = loc->next ;
}
preloc->next = ptr ;
ptr->next = loc ;
}
}
If you have any suggestions , let me know ,,thanks
Aug 1, 2012 at 12:24pm UTC
loc is used without being initialized.
Aug 1, 2012 at 6:40pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
void Queue::enqueue(int item , int prior)
{
node *ptr = new node();
ptr->info = item ;
ptr->priority = prior ;
if (front == NULL) // queue is empty
{
ptr->next = NULL ;
front = ptr ;
}
else //queue not empty
{
node *loc , *preloc ;
loc = front ;
while (loc->priority <= prior)
{
preloc = loc ;
loc = loc->next ;
}
preloc->next = ptr ;
ptr->next = loc ;
}
}
Still program stops abruptly ...
Aug 1, 2012 at 8:34pm UTC
When it comes to loop think of the 'extrems'.
What happens when
the first element is loc->priority > prior
i.e. no loop at all
no element is loc->priority > prior
i.e. how does the loop come to an end
Aug 25, 2012 at 3:18am UTC
I learned using debugger . and it gives a segmentation fault error.
Well , In my program the number with largest priority is kept in the front and lower priorities after that and so on..
I initially entered 2,9(item,prior)..it gets stored within the queue.
Then I entered 23,2(item,prior)..it gives a segmentation fault error on the line 21 in my previous post.
*preloc = cannot access memory at address at 0x2
why ?
Initially I thought there might be some null pointer problem but there's none.
Here's the full code of the problem :
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <iostream>
#include "Queue.h"
#include <cstdlib>
using namespace std;
int main()
{
Queue myqueue ;
int ans , item ,pr;
cout<<"\n\n Queue Operations\n" ;
while (true )
{
cout<<"\n1. Enqueue" ;
cout<<"\n2. Dequeue" ;
cout<<"\n3. Peep" ;
cout<<"\n4. IsEmpty" ;
cout<<"\n5. Traverse" ;
cout<<"\n6. Dispose" ;
cout<<"\n7. Exit" ;
myqueue.showVar();
cout <<endl;
myqueue.traverse();
cout <<endl;
cout<<"Enter any operation " ;
cin>>ans;
switch (ans)
{
case 1 :
cout<<"Enter an item and its priority " ;
cin>>item>>pr;
myqueue.enqueue(item,pr);
break ;
case 2 :
cout<<"\nThe element is " << myqueue.dequeue();
break ;
case 3 :
cout<<"\nThe topmost element is " <<myqueue.peep();
break ;
case 4 :
cout<<myqueue.isEmpty();
break ;
case 5 :
myqueue.traverse();
break ;
case 6 :
myqueue.dispose();
break ;
case 7 :
exit(0);
break ;
default :
cout<<"\nInvalid input " ;
exit(0);
}
}
return 0;
}
Queue.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#ifndef QUEUE_H
#define QUEUE_H
#define arrsize 10
typedef struct nodeType {
int info ;
int priority ;
struct nodeType *next ;
} node ;
class Queue
{
private :
node *front ;
public :
Queue();
void traverse();
bool isEmpty();
void enqueue(int ,int );
int dequeue();
int peep();
void showVar();
void dispose();
};
#endif // QUEUE_H
Queue.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include "queue.h"
#include <iostream>
#include <cstdlib>
int Queue::dequeue()
{
node *ptr = front ;
int item ;
if (isEmpty())
{
std::cout <<"\nQueue Empty!!" ;
exit(0);
}
else
{
item = ptr -> info ;
front = front -> next;
delete ptr ;
}
return item ;
}
int Queue::peep()
{
node *ptr = front;
if (isEmpty())
{
std::cout << "Queue empty" <<std::endl;
exit(0) ;
}
else
{
return ptr->info;
}
}
void Queue::enqueue(const int item ,const int prior)
{
node *ptr = new node();
ptr->info = item ;
ptr->priority = prior ;
if (front == NULL) // queue is empty
{
ptr->next = NULL ;
front = ptr ;
}
else //queue not empty
{
node *loc , *preloc;
loc = front ;
if (ptr->priority >= front->priority)
{
ptr->next = front ;
front = ptr;
}
else if (ptr->priority < front->priority)
{
while (ptr->priority < loc->priority && loc != NULL)
{
preloc = loc ;
loc = loc->next ;
}
preloc->next = ptr ;
ptr->next = loc ;
}
}
}
void Queue::showVar()
{
std::cout<<"\nFront: " <<front;
}
void Queue::traverse()
{
node *ptr = front ;
while (ptr != NULL)
{
std::cout << ptr -> info << ":" << ptr->priority <<" " ;
ptr = ptr -> next ;
}
}
bool Queue::isEmpty()
{
return (front == NULL);
}
Queue::Queue()
{
front = NULL ;
}
void Queue::dispose()
{
while ( front != NULL)
{
node *ptr = front ;
front = front ->next ;
delete ptr ;
}
front = NULL ;
}
Any kind of suggestions are welcome..thanks
Last edited on Aug 25, 2012 at 3:53pm UTC
Aug 25, 2012 at 10:07am UTC
You did not initialize preloc. If the new element has smaller priority than any other element the loop body will never run and preloc remains uninitialized.
Aug 25, 2012 at 4:08pm UTC
Error found :)
The line in enqueue
while (ptr->priority < loc->priority && loc != NULL)
should be written like
while (loc != NULL && ptr->priority < loc->priority)
The compiler executes lines from left to right .
Thus when it executes this line it first checks whether the loc is NULL or not and then checks it's priority. Thus it saves from SEGMENTATION FAULT error.
Is there any better way to write this part of code ?
Last edited on Aug 25, 2012 at 5:02pm UTC
Topic archived. No new replies allowed.