943,719 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4246
  • C++ RSS
Apr 21st, 2004
1

help with queue plz

Expand Post »
ok im trying to change this from link list to a queue.bt whenever i run it it sill shows from last to first which is the opposite from what i want.here it is, i hope somebody can try to see whats wrong with it.

#include<iostream.h>
#include<conio.h>
struct queue
{
int data;
queue* next;
};
class queueL
{
protected:
queue* last;
queue* first;
public:
queueL()
{last = NULL;}
void additem(int d);
void display();
};
void queueL::additem(int d)
{
queue* newqueue = new queue;
newqueue->data=d;
newqueue->next=last;
last = newqueue;
}
void queueL::display()
{
queue* current = last;
while(current!=NULL)
{
cout<<endl<<current->data;
current = current->next;
}
}
void main()
{
queueL ql;
clrscr();
ql.additem(25);
ql.additem(36);
ql.additem(49);
ql.additem(64);
ql.display();
}
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
eager04 is offline Offline
1 posts
since Apr 2004
Apr 21st, 2004
0

Re: help with queue plz

Hi,

I am not a well - versed programmer, but yes, you do have a linked list going there in one direction. How about trying a dual linked list (a list that has pointers going in both directions so that if you had

A B C

and the "now" is on B, that you have the pointer information for both A and C, so that you can move forwards or backwards.

Then, for queue mode, go to the end of the list, and work it backwards using the reverse pointers.

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Apr 22nd, 2004
1

Re: help with queue plz

your add_item method should traverse to the END of the list, and add the element there. the remove_item method should remove the first element in the list.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Apr 28th, 2004
0

Re: help with queue plz

Hi,

I think infamous is right! Might have mis-read what you are trying to do.

I do know that in my programming days, I did make a lot of linked lists both ways, just for flexibility. I also kept another copy of the head pointer around in another variable in the event I "lost" it during coding.

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Jun 7th, 2004
0

Re: help with queue plz

Hello
Why you are using a c-style struct why you are not using a c++ class
here you are a good example
Attached Files
File Type: h Queue.h (2.6 KB, 38 views)
Reputation Points: 12
Solved Threads: 2
Newbie Poster
abu_sager is offline Offline
10 posts
since Jun 2004
Jun 7th, 2004
0

Re: help with queue plz

Quote originally posted by abu_sager ...
Hello
Why you are using a c-style struct why you are not using a c++ class
here you are a good example
There is nothing wrong with using a struct for the queue nodes here. If all you're storing are a few pieces of information, there's no need for an overgrown class to handle the same data.

The queue template class posted, while it is an example (I'm assuming it works as advertised), I wouldn't call it a good example--it's a bloated gob of code handed down from on high. It looks like Abu here is more interested in saying "ooh, look at all the features i'm using! classes! enumerations! pointers! const methods! templates!" ...all of which are useful things, but not really necessary for the problem at hand.

If you haven't run across templates yet, have a look at http://babbage.cs.qc.edu/STL_Docs/templates.htm for a start.

Now, on to your question--the solution provided by 'infamous' will work, as will doubly-linking the list and running through the display backward. Another approach is to alter the display method:
C++ Syntax (Toggle Plain Text)
  1. void queueL::display()
  2. {
  3. display_recursive(last);
  4. }
  5.  
  6. void queueL::display_recursive(queue *n)
  7. {
  8. if(n == NULL) return;
  9. display_recursive(n->next);
  10. cout << endl << n->data;
  11. }
Recursion can be a handy tool when dealing with linked lists. If it's not clear, display_recursive will continue to call itself, passing each successive queue node until it hits the end of the list, i.e. it hits a null next pointer. Then it will display them in reverse order as each recursive call terminates.

This is a little more complicated than the other two solutions; I'm just offering it as an alternative. Now you have at least three ways to get your display method to show the nodes in the desired order.

--sg
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help newb :)
Next Thread in C++ Forum Timeline: i want the user to enter an input without obligating him to press enter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC