| | |
help with queue plz
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 1
Reputation:
Solved Threads: 0
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();
}
#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();
}
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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
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
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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
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
•
•
Join Date: May 2004
Posts: 80
Reputation:
Solved Threads: 5
•
•
•
•
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
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)
void queueL::display() { display_recursive(last); } void queueL::display_recursive(queue *n) { if(n == NULL) return; display_recursive(n->next); cout << endl << n->data; }
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
![]() |
Similar Threads
- queue and stack (C)
- plz help me plz plz plz plz plz plz (C)
- what doesnt work? plz help guys (C++)
- Can somebody help me plz? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help newb :)
- Next Thread: i want the user to enter an input without obligating him to press enter
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






