473,378 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Priority queue help

This is a programming assignment. You are asked to work with pointers.
Be aware that error messages are often not very helpful when your
pointers
point to bad locations. Therefore, reserve additional time for
debugging.
Implement a data structure Extended Queue using pointers. In addition
to the usual queue operations Enqueue(x), Dequeue and the trivial
operations
Make-empty-queue and the Is-empty test, you are asked to have
two nonstandard operations.
· High-priority-enqueue(x) enqueues x at the front of the queue.
· Medium-priority-enqueue(x) enqueues x in the middle. If the sequence
has odd length, then x is enqueued just before the middle.
The elements x of the Extended Queue are integers.
Pointers are often used for fast algorithms. We use pointers here,
because
we require that each of the Extended Queue operations is done in
(worst
case) time O(1).
(a) Describe how you achieve time O(1) for the operation Medium-
priorityenqueue(
x).
(b) Write a program implementing Extended Queue as follows. It starts
building an empty Extended Queue. Then it reads standard input
without any prompts until it reaches end-of-file. The input is a
sequence
of commands (one command per line) of the form:
· e x (for Enqueue(x))
· h x (for High-priority-enqueue(x))
· m x (for Medium-priority-enqueue(x))
· d (for Dequeue)
After reading any command, that command is executed in time O(1).
On Dequeue, the dequeued element is output. When eof is reached in
the input, then start a new line and do Dequeue until the Extended
Queue is empty.
(c) Run your program on the Command File below

e 1
h 2
m 3
m 4
m 5
d
h 6
d
d
d
m 7
h 8
h 9
m 10
d
d
m 11
e 12
e 13
m 14

can anyone help me out?

Oct 18 '07 #1
4 3395
jj*****@gmail.com wrote:
[homework problem redacted]
You're in luck!!! That very question was asked an answered here:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

In other words... DO YOUR OWN HOMEWORK!!!
Oct 18 '07 #2
On 2007-10-18 23:09, jj*****@gmail.com wrote:
This is a programming assignment.
can anyone help me out?
Sure, where is your code and what is your problem with it? You might
also be interested in FAQ items 5.2 and 5.8:
http://www.coders2020.com/cplusplus-...t.html#faq-5.2
http://www.coders2020.com/cplusplus-...t.html#faq-5.8

--
Erik Wikström
Oct 18 '07 #3
jj*****@gmail.com wrote in news:1192741792.546333.67490
@v23g2000prn.googlegroups.com:
This is a programming assignment. You are asked to work with pointers.
At least you're honest up front.

[snip homework assignment]

We're not going to do it for you. You have to try it yourself. When you
have specific C++ questions (and "How do I do this assignment in C++" isn't
specific).
Oct 18 '07 #4
I so far have implemented everything using a linked list, Now I just
need help making the medium priority queue.

#ifndef LISTNODE_H
#define LISTNODE_H

// forward declaration of class List required to announce that class
// List exists so it can be used in the friend declaration at line 13
template< typename NODETYPE class List;

template< typename NODETYPE >
class ListNode
{
friend class List< NODETYPE >; // make List a friend

public:
ListNode( const NODETYPE & ); // constructor
NODETYPE getData() const; // return data in node
private:
NODETYPE data; // data
ListNode< NODETYPE *nextPtr; // next node in list
}; // end class ListNode

// constructor
template< typename NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 )
{
// empty body
} // end ListNode constructor

// return copy of data in node
template< typename NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const
{
return data;
} // end function getData

#endif

---------------------------------------------------------------------

#ifndef LIST_H
#define LIST_H

#include <iostream>
using std::cout;

#include <new>
#include "Listnode.h" // ListNode class definition

template< typename NODETYPE >
class List
{
public:
List(); // constructor
~List(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE *firstPtr; // pointer to first node
ListNode< NODETYPE *lastPtr; // pointer to last node

// utility function to allocate new node
ListNode< NODETYPE *getNewNode( const NODETYPE & );
}; // end class List

// default constructor
template< typename NODETYPE >
List< NODETYPE >::List()
: firstPtr( 0 ), lastPtr( 0 )
{
// empty body
} // end List constructor

// destructor
template< typename NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) // List is not empty
{
cout << "Destroying nodes ...\n";

ListNode< NODETYPE *currentPtr = firstPtr;
ListNode< NODETYPE *tempPtr;

while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
cout << tempPtr->data << '\n';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
} // end while
} // end if

cout << "All nodes destroyed\n\n";
} // end List destructor

// insert node at front of list
template< typename NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE *newPtr = getNewNode( value ); // new node

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr; // new list has only one node
else // List is not empty
{
newPtr->nextPtr = firstPtr; // point new node to previous 1st
node
firstPtr = newPtr; // aim firstPtr at new node
} // end else
} // end function insertAtFront

// insert node at back of list
template< typename NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE *newPtr = getNewNode( value ); // new node

if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr; // new list has only one node
else // List is not empty
{
lastPtr->nextPtr = newPtr; // update previous last node
lastPtr = newPtr; // new last node
} // end else
} // end function insertAtBack

// delete node from front of list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
ListNode< NODETYPE *tempPtr = firstPtr; // hold tempPtr to
delete

if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0; // no nodes remain after removal
else
firstPtr = firstPtr->nextPtr; // point to previous 2nd node

value = tempPtr->data; // return data being removed
delete tempPtr; // reclaim previous front node
return true; // delete successful
} // end else
} // end function removeFromFront

// delete node from back of list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
ListNode< NODETYPE *tempPtr = lastPtr; // hold tempPtr to
delete

if ( firstPtr == lastPtr ) // List has one element
firstPtr = lastPtr = 0; // no nodes remain after removal
else
{
ListNode< NODETYPE *currentPtr = firstPtr;

// locate second-to-last element
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr; // move to next node

lastPtr = currentPtr; // remove last node
currentPtr->nextPtr = 0; // this is now the last node
} // end else

value = tempPtr->data; // return value from old last node
delete tempPtr; // reclaim former last node
return true; // delete successful
} // end else
} // end function removeFromBack

// is List empty?
template< typename NODETYPE >
bool List< NODETYPE >::isEmpty() const
{
return firstPtr == 0;
} // end function isEmpty

// return pointer to newly allocated node
template< typename NODETYPE >
ListNode< NODETYPE *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
return new ListNode< NODETYPE >( value );
} // end function getNewNode

// display contents of List
template< typename NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) // List is empty
{
cout << "The list is empty\n\n";
return;
} // end if

ListNode< NODETYPE *currentPtr = firstPtr;

cout << "The list is: ";

while ( currentPtr != 0 ) // get element data
{
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
} // end while

cout << "\n\n";
} // end function print

#endif
Oct 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
3
by: Erik | last post by:
Hi Everyone, I was thinking of how to do this for a while now and I cant get my head round it so I though I'd ask the experts! :-) What I am doing is reading in large amounts of data over TCP...
16
by: Crirus | last post by:
hello I read somewhere about priority queue...what is that and Vb net have such class? -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it...
4
by: vfunc | last post by:
Is the STL priority queue a proper implementation of a heap with siftup algorithm etc ? How do you implement an STL priority queue (link to an example) ? Is there a resort method, why ? Thanks
3
by: Chris Lasher | last post by:
Hello, I am working with large graphs (~150,000 to 500,000 nodes) which I need decompose node-by-node, in order of a node's value. A node's value is determined by the sum of its edge weights....
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
1
by: operatingsystem | last post by:
I need to generate such scheduler in operating system subject which satisfy below conditions...may i request u if anybody knows abt this plz help me out in this..topics.. Scheduler...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.