ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

˳Ðò¶ÓÁРQueue(¶þ)
2013-11-20 14:24:18 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:374´Î
Tags£ºË³Ðò ¶ÓÁÐ   Queue

 

¡¡¡¡Error_code serve_and_retrieve(Queue_entry &item);

¡¡¡¡};

¡¡¡¡#endif

¡¡¡¡#ifndef QUEUE_H

¡¡¡¡#define QUEUE_H

¡¡¡¡const int maxqueue=10;

¡¡¡¡enum Error_code {success,overflow,underflow};

¡¡¡¡typedef char Queue_entry ;

¡¡¡¡class Queue{

¡¡¡¡public:

¡¡¡¡Queue();

¡¡¡¡bool empty() const;

¡¡¡¡Error_code append(const Queue_entry &item);

¡¡¡¡Error_code serve();

¡¡¡¡Error_code retrieve(Queue_entry &item)const;

¡¡¡¡protected:

¡¡¡¡int count;

¡¡¡¡int front,rear;

¡¡¡¡Queue_entry entry[maxqueue];

¡¡¡¡};

¡¡¡¡class Extended_queue:public Queue{

¡¡¡¡public:

¡¡¡¡bool full()const;

¡¡¡¡int size()const;

¡¡¡¡void clear();

¡¡¡¡Error_code serve_and_retrieve(Queue_entry &item);

¡¡¡¡};

¡¡¡¡#endif

¡¡¡¡queue.cpp

¡¡¡¡[cpp] view plaincopyprint

¡¡¡¡#include"queue.h"

¡¡¡¡Queue::Queue()

¡¡¡¡{

¡¡¡¡count=0;

¡¡¡¡rear=maxqueue-1;

¡¡¡¡front=0;

¡¡¡¡}

¡¡¡¡bool Queue::empty() const

¡¡¡¡{

¡¡¡¡return count==0;

¡¡¡¡}

¡¡¡¡Error_code Queue::append(const Queue_entry &item)

¡¡¡¡{

¡¡¡¡if(count>=maxqueue)return overflow;

¡¡¡¡count++;

¡¡¡¡rear=((rear+1)==maxqueue) 0:(rear+1);

¡¡¡¡entry[rear]=item;

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡Error_code Queue::serve()

¡¡¡¡{

¡¡¡¡if(count<=0)return underflow;

¡¡¡¡count--;

¡¡¡¡front=((front+1)==maxqueue) 0:(front+1);

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡Error_code Queue::retrieve(Queue_entry &item) const

¡¡¡¡{

¡¡¡¡if(count<=0)return underflow;

¡¡¡¡item=entry[front];

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡bool Extended_queue::full() const{

¡¡¡¡return count==maxqueue;

¡¡¡¡}

¡¡¡¡int Extended_queue::size()const{

¡¡¡¡return count;

¡¡¡¡}

¡¡¡¡void Extended_queue::clear(){

¡¡¡¡count=0;

¡¡¡¡rear=front;

¡¡¡¡}

¡¡¡¡Error_code Extended_queue::serve_and_retrieve(Queue_entry &item){

¡¡¡¡if(count==0)return underflow;

¡¡¡¡count--;

¡¡¡¡item=entry[front];

¡¡¡¡front=((front+1)==maxqueue) 0:(front+1);

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡#include"queue.h"

¡¡¡¡Queue::Queue()

¡¡¡¡{

¡¡¡¡count=0;

¡¡¡¡rear=maxqueue-1;

¡¡¡¡front=0;

¡¡¡¡}

¡¡¡¡bool Queue::empty() const

¡¡¡¡{

¡¡¡¡return count==0;

¡¡¡¡}

¡¡¡¡Error_code Queue::append(const Queue_entry &item)

¡¡¡¡{

¡¡¡¡if(count>=maxqueue)return overflow;

¡¡¡¡count++;

¡¡¡¡rear=((rear+1)==maxqueue) 0:(rear+1);

¡¡¡¡entry[rear]=item;

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡Error_code Queue::serve()

¡¡¡¡{

¡¡¡¡if(count<=0)return underflow;

¡¡¡¡count--;

¡¡¡¡front=((front+1)==maxqueue) 0:(front+1);

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡Error_code Queue::retrieve(Queue_entry &item) const

¡¡¡¡{

¡¡¡¡if(count<=0)return underflow;

¡¡¡¡item=entry[front];

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡bool Extended_queue::full() const{

¡¡¡¡return count==maxqueue;

¡¡¡¡}

¡¡¡¡int Extended_queue::size()const{

¡¡¡¡return count;

¡¡¡¡}

¡¡¡¡void Extended_queue::clear(){

¡¡¡¡count=0;

¡¡¡¡rear=front;

¡¡¡¡}

¡¡¡¡Error_code Extended_queue::serve_and_retrieve(Queue_entry &item){

¡¡¡¡if(count==0)return underflow;

¡¡¡¡count--;

¡¡¡¡item=entry[front];

¡¡¡¡front=((front+1)==maxqueue) 0:(front+1);

¡¡¡¡return success;

¡¡¡¡}

¡¡¡¡comparequeue.cpp

¡¡¡¡[cpp] view plaincopyprint

¡¡¡¡#include "queue.h"

¡¡¡¡#include

¡¡¡¡#include

¡¡¡¡using namespace std;

¡¡¡¡void Introduction();

¡¡¡¡void Compare();

¡¡¡¡int main(){

¡¡¡¡Introduction();

¡¡¡¡Compare();

¡¡¡¡return 0;

¡¡¡¡}

¡¡¡¡//show the introduction of the program

¡¡¡¡void Introduction(){

¡¡¡¡cout<<"Hi! This program is compare two parts of characters."<

¡¡¡¡<<"First you should type in a line of characters which is supposed to consist of two parts "

¡¡¡¡<<"separated by a colon ':' ."<

¡¡¡¡<<"And then enter to quit put in"<

¡¡¡¡<<"Then I will show you a single character as follows :"<

¡¡¡¡<<"[N] No colon on the line"<

¡¡¡¡<<"[L] The left part (before the colon) is longer than the right"<

¡¡¡¡<<"[R] The right part (after the colon) is longer than the left"<

¡¡¡¡<<"[D] The left and the right parts have the same length but different"<

¡¡¡¡<<"[S] The left and the right parts are exactly the same"<

¡¡¡¡<<"And enter [Ctrl+'Z'] to stop input"<

¡¡¡¡<<"So , just try it!"<

¡¡¡¡return;

¡¡¡¡}

¡¡¡¡void Compare(){

¡¡¡¡Extended_queue left_queue;

¡¡¡¡char left_char;

¡¡¡¡bool waiting=true;

¡¡¡¡while(cin>>left_char && waiting){

¡¡¡¡if(left_char==':'){

¡¡¡¡waiting=false; //if ':' is entered, quit the left input

¡¡¡¡break;

¡¡¡¡}

¡¡¡¡else if(left_queue.full())

¡¡¡¡cout<<"Queue is full!"<

¡¡¡¡else

¡¡¡¡left_queue.append(left_char);

¡¡¡¡//if the input is not ':' and there is space in the queue

¡¡¡¡//append the charcter to the queue

¡¡¡¡}

¡¡¡¡char right_char;

¡¡¡¡bool same=true;

¡¡¡¡while(!waiting && cin>>right_char){ //if ':' is input before

¡¡¡¡//now input the righter charachters

¡¡¡¡if(left_queue.empty()){

¡¡¡¡cout<<"R"<

¡¡¡¡//and the user is still inputing, right must longer than left

¡¡¡¡//print [R]

¡¡¡¡return;

¡¡¡¡}

¡¡¡¡else{

¡¡¡¡left_queue.serve_and_retrieve(left_char);

¡¡¡¡if(left_char!=right_char)

¡¡¡¡same=false; //if the character input now is different from

¡¡¡¡//the one put into left before, set the bool same to 'false'

¡¡¡¡}

¡¡¡¡}

¡¡¡¡if(waiting){

¡¡¡¡cout<<"N"<

¡¡¡¡//print [N]

¡¡¡¡return ;

¡¡¡¡}

¡¡¡¡if(!left_queue.empty()){

¡¡¡¡cout<<"L"<

¡¡¡¡//left must longer than right

¡¡¡¡//print [L]

¡¡¡¡return ;

¡¡¡¡}

¡¡¡¡if(left_queue.empty() && same){

¡¡¡¡cout<<"S"<

¡¡¡¡//left and right have the exactly same characters

¡¡¡¡//print [S]

¡¡¡¡return;

¡¡¡¡}

¡¡¡¡return;

¡¡¡¡}

¡¡¡¡#include "queue.h"

¡¡¡¡#include

¡¡¡¡#include

¡¡¡¡using namespace std;

¡¡¡¡void Introduction();

¡¡¡¡void Compare();

¡¡¡¡int main(){

¡¡¡¡Introduction();

¡¡¡¡Compare();

¡¡¡¡return 0;

¡¡¡¡}

¡¡¡¡//show the introduction of the program

¡¡¡¡void Introduction(){

¡¡¡¡cout<<"Hi! This program is compare two parts of characters."<

¡¡¡¡<<"First you should type in a line of characters which is supposed to consist of two parts "

¡¡¡¡<<"separated by a colon ':' ."<

¡¡¡¡<<"And then enter to quit put in"<

¡¡¡¡<<"Then I will show you a single character as follows :"<

¡¡¡¡<<"[N] No colon on the line"<

¡¡¡¡<<"[L] The left part (before the colon) is longer than the right"<

¡¡¡¡<<"[R] The right part (after the colon) is longer than the left"<

¡¡¡¡<<"[D] The left and the right parts have the same length but different"<

¡¡¡¡<<"[S] The left and the right parts are exactly the same"<

¡¡¡¡<<"And enter [Ctrl+'Z'] to stop input"<

¡¡¡¡<<"So , just try it!"<

¡¡¡¡return;

¡¡¡¡}

        

Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 ÏÂÒ»Ò³ βҳ 2/3/3
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
·ÖÏíµ½: 
ÉÏһƪ£ºË³ÐòÕ» Stack ÏÂһƪ£ºÍ¨¹ý¶þ²æÊ÷ºÍÒ»¸öÊýÕÒµ½Â·¾¶

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Linuxϵͳ¼ò½é (2025-12-25 21:55:25)
¡¤Linux°²×°MySQL¹ý³Ì (2025-12-25 21:55:22)
¡¤Linuxϵͳ°²×°½Ì³Ì£¨ (2025-12-25 21:55:20)
¡¤HTTP Åc HTTPS µÄ²î„ (2025-12-25 21:19:45)
¡¤ÍøÕ¾°²È«±ØÐ޿ΣºÍ¼ (2025-12-25 21:19:42)