欢迎来到天天文库
浏览记录
ID:8821584
大小:84.00 KB
页数:5页
时间:2018-04-08
《数据结构优先队列链表实现》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、链表实现代码:#includeusingnamespacestd;constintSIZE=50;classNode{public:Node(){pre=NULL;next=NULL;}intvalue;Node*pre;Node*next;};classPQueue{public:PQueue();boolempty()const;boolfull()const;voidpop();voidpush(intp);intsize()const;Node*top();voiddisplay();private:No
2、de*front,*back;intcount;};PQueue::PQueue(){front=NULL;back=NULL;count=0;}boolPQueue::empty()const{if(front==NULL)returntrue;elsereturnfalse;}voidPQueue::pop(){if(empty())cout<<"队列为空!"<3、t->next;t->pre=NULL;front=t;}else{p->pre->next=p->next;p->next->pre=p->pre;}count--;}}voidPQueue::push(intp){if(empty()){Node*newNode1=newNode();newNode1->value=p;front=newNode1;Node*newNode2=newNode();newNode2->pre=front;back=newNode2;front->next=newNode2;count++;}els4、e{Node*newNode=newNode();back->value=p;newNode->pre=back;back->next=newNode;back=newNode;count++;}}intPQueue::size()const{returncount;}Node*PQueue::top(){Node*max=front;Node*temp=front;while(temp->next!=NULL){if(temp->value>max->value)max=temp;temp=temp->next;}returnma5、x;}voidPQueue::display(){if(empty())cout<<"队列为空!"<next!=NULL){if(i++%5==0)cout<value<<"";temp=temp->next;}cout<6、>t;if(t==1){inttemp;printf("请输入数据:");cin>>temp;q.push(temp);}elseif(t==2){q.pop();}elseif(t==3){q.display();}elsecout<<"请重新输入:"<
3、t->next;t->pre=NULL;front=t;}else{p->pre->next=p->next;p->next->pre=p->pre;}count--;}}voidPQueue::push(intp){if(empty()){Node*newNode1=newNode();newNode1->value=p;front=newNode1;Node*newNode2=newNode();newNode2->pre=front;back=newNode2;front->next=newNode2;count++;}els
4、e{Node*newNode=newNode();back->value=p;newNode->pre=back;back->next=newNode;back=newNode;count++;}}intPQueue::size()const{returncount;}Node*PQueue::top(){Node*max=front;Node*temp=front;while(temp->next!=NULL){if(temp->value>max->value)max=temp;temp=temp->next;}returnma
5、x;}voidPQueue::display(){if(empty())cout<<"队列为空!"<next!=NULL){if(i++%5==0)cout<value<<"";temp=temp->next;}cout<6、>t;if(t==1){inttemp;printf("请输入数据:");cin>>temp;q.push(temp);}elseif(t==2){q.pop();}elseif(t==3){q.display();}elsecout<<"请重新输入:"<
6、>t;if(t==1){inttemp;printf("请输入数据:");cin>>temp;q.push(temp);}elseif(t==2){q.pop();}elseif(t==3){q.display();}elsecout<<"请重新输入:"<
此文档下载收益归作者所有