1、单向链队列中的优先队列/=/单向链队列中的优先队列/=#includeusing namespace std;/-templateclass ListQueue;/-templateclass ListQueueNodepublic:friend class ListQueue;friend ostreamListQueueNode(Tbool operator =(const ListQueueNode bool operator !=(const ListQueueNode bool operator (const ListQueueNode bool operator prior=p.p
2、rior;link=NULL;return 1;ListQueueNode();private:T element;int prior;ListQueueNode *link;/-templateListQueueNode:ListQueueNode(Tprior=pri;link=NULL;/-templateclass ListQueuepublic:ListQueue();ListQueue(T ListQueue();void InQueue(Tvoid DeQueue(T bool Empty() const;int Length() const;void Setpri(const
3、Tint Find(Tint Findall(Tvoid Display() const;void travel(void (*Fun)(T void Clear();friend ostream current!=NULL; current=current-link)oelementprior* head;/-templateListQueue:ListQueue()length=0;head=NULL;/-templateListQueue:ListQueue(Thead=new ListQueueNode(x,pri);/-templateListQueue:ListQueue()if(
4、head!=NULL)for(ListQueueNode *current=head; current=head; )head=head-link;delete current;head=NULL;length=0;/-templatevoid ListQueue:InQueue(Tlength+;elseListQueueNode *current=new ListQueueNode(x,pri);if(*currentlink=head;head=current;length+;elsefor(ListQueueNode *p=head; p-link!=NULL; p=p-link)if
5、(*currentlink)break;current-link=p-link;p-link=current;length+;/-templatevoid ListQueue:DeQueue(T delete head;head=NULL;length-;return ;ListQueueNode *p;for(ListQueueNode *current=head; current-link!=NULL; )p=current;current=current-link;x=current-element;delete current;p-link=NULL;length-;/-templat
6、ebool ListQueue:Empty() constreturn length=0;/-templateint ListQueue:Length() constreturn length;/-templatevoid ListQueue:Setpri(const Tif(head-element=element) p=head;head=p-link;elseListQueueNode *current;for(p=head; p-link!=NULL; )current=p; if(p-element=element)break;p=p-link;current-link=p-link
7、;p-prior=pri;length-;InQueue(p-element,p-prior);delete p;/-templateint ListQueue:Find(Tif(Empty()!=0)cout *current=head; current!=NULL; current=current-link)count+;if(*current=*p)return count;return -1;/-templateint ListQueue:Findall(Tif(Empty()!=0)cout *current=head; current!=NULL; current=current-
8、link)count+;if(*current=*p)parameteri=count;i+; return i;/-templatevoid ListQueue:Display() constif(Empty()!=0)cout *current=head; current!=NULL; current=current-link)coutelementpriorvoid ListQueue:travel(void (*Fun)(T current!=NULL; current=current-link)Fun(*current);/-templatevoid ListQueue:Clear(
9、)if(head!=NULL)for(ListQueueNode *current=head; current=head; )head=head-link;delete current;head=NULL;length=0;/-int main()/* ListQueue s;cout p(i,10);cout s;for(int i=0; i=5; i+)s.InQueue(i,i);couts.Length()endl;couts.Empty()endl;s.Display();s.DeQueue(i);couts.Length()endl;couts.Empty()endl;coutiendl;i=0;cout“-“endl;coutiendl;s.Setpri(i,0);cout“-6-“endl;couts.Find(i,1)endl;coutsendl;return 1;