473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3426
jj*****@gmail.c om 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.c om 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.c om wrote in news:1192741792 .546333.67490
@v23g2000prn.go oglegroups.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 >::insertAtFron t( 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 >::removeFromFr ont( 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 >::removeFromBa ck( 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
5806
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 queue. However, no matter what reasonably-sized type I use for the stamp, eventually the stamp will 'wrap around' and possibly cause incorrect ordering of elements. For my application, which queues elements very quickly and runs for an...
5
13251
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 enqueue, I do a binary search on where to put the object and then insert using that index and arraylist.insert(index, obj) The bottom of my arraylist is always my lowest, therefore, dequeueing is very fast and effecient.
3
1373
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 and saving it to disk. I receive alot of data from alot of clients so I do all this without processing the data. When a piece of data from a client has been completed I want to do
16
5418
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 all from you
4
10369
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
4365
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. When a node is removed from the graph, its neighbors' values must be updated to take into account the removed edges. I was told to look for a priority queue in Python. I had a look at the heapq module. It looks like it supports ordering on...
3
7436
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 ( maximum element ) while set and heap can erase any element ...
1
3962
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 specifications: 1. Three queues, highest priority to lowest: RR SJF FIFO 2. All jobs enter RR 3. Job at lower queue cannot execute until higher priority queue is empty
14
16975
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 priority queue, which is an array heap, and for example i/2 is the father of i and so on.. the code of the static priority queue is this: int i= ++CurrentSize; while(i != 1 && X > heap) { // cannot put x in heap heap = heap; //move...
0
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.