#include<iostream> #include<list> #include<string> using namespace std; class clientQueue; //前向申明 class client{ public: client(string xname,size_t xpos =0):name(xname),pos(xpos){} void addIntoQueue(clientQueue &queue); void leaveFromQueue(clientQueue &queue); string name; size_t pos; }; class clientQueue{ public: size_t queueSize() { return clientList.size();} void incClient(client * cli); //必须用指针,否则存放的是对象的拷贝 void decClient(client * cli) ; private: list<client *> clientList; }; void client::addIntoQueue(clientQueue &queue) { queue.incClient(this); } void client::leaveFromQueue(clientQueue &queue) { queue.decClient(this); } void clientQueue::incClient(client * cli) { clientList.push_back(cli); //将新客户加入队列末尾 cli->pos = clientList.size(); // 更新新客户位置 } void clientQueue::decClient(client * cli) { list<client *>::iterator it = clientList.begin(); advance(it,cli->pos); //找到要删除的位置的后面一个client,从其开始的所有client的位置 for(;it !=clientList.end(); ++it ) ( (*it)->pos )--; it = clientList.begin(); advance(it,cli->pos-1); //删除该client clientList.erase(it); cli->pos =0; //将被删除的client的pos置为0 } int main() { client a("A"),b("B"),c("C"),d("D"),e("E"); //新建五个client A B C D E clientQueue queue; a.addIntoQueue(queue); b.addIntoQueue(queue); c.addIntoQueue(queue); cout << a.pos <<" " << b.pos << " " <<c.pos << " " << d.pos << " " << e.pos << endl; b.leaveFromQueue(queue); cout << a.pos <<" " << b.pos << " " <<c.pos << " " << d.pos << " " << e.pos << endl; d.addIntoQueue(queue); e.addIntoQueue(queue); cout << a.pos <<" " << b.pos << " " <<c.pos << " " << d.pos << " " << e.pos << endl; a.leaveFromQueue(queue); cout << a.pos <<" " << b.pos << " " <<c.pos << " " << d.pos << " " << e.pos << endl; }
// 客户类 class Custom { public: Custom(int s) { index = 0; serial = s; next = NULL; prev = NULL; } // 刷新排名信息 void Refresh(){ printf("排在号码为 %d 客户的前面还有 %d 人\n",serial,index); } // 位置向前移动 void GoAhead(){ --index; Refresh(); if (NULL!=next) { next->GoAhead(); } } // 位置向后移动 void GoBack() { ++index; Refresh(); if (NULL!=next) { next->GoBack(); } } // 获取当前位置 int GetIndex() { return index; } int index; int serial; Custom * next; Custom * prev; }; // 排队系统 class QueuingSystem { public: QueuingSystem(){ m_head = NULL; m_tail = NULL; num = 0; } // 添加排队客户,排到队尾 void AddToBack(int s) { Custom * p = new Custom(s); if ( NULL==m_head ) { m_head = p; m_tail = m_head; } else { p->index = m_tail->index+1; m_tail->next = p; p->prev = m_tail; m_tail = p; } p->Refresh(); ++num; } // 将 c 插队到 t 的前面 bool InsertTo(Custom * c,Custom * t) { if ( NULL==c || NULL==t ) { return NULL; } c->next = t; if ( NULL!=t->prev ) { t->prev->next = c; c->prev = t->prev; c->index = t->prev->index+1; } else { m_head = c; c->index = 0; } t->prev = c; c->next->GoBack(); ++num; return true; } // 添加排队客户,插队到流水号为 to 的前面 bool InsertTo(int s,int to) { if ( NULL==m_head ) { return false; } Custom * p = m_head; while( NULL!=p ) { if (to==p->serial) { return InsertTo(new Custom(s),p); } p = p->next; } return false; } // 退出排队 bool Quit(Custom * c) { if ( NULL==c ) { return false; } if ( NULL!=c->next ) { c->next->GoAhead(); } if ( m_head==c ) { m_head = c->next; } if ( m_tail==c ) { m_tail = c->prev; } if ( NULL!=c->prev ) { c->prev->next = c->next; } if ( NULL!=c->next ) { c->next->prev = c->prev; } delete c; --num; return true; } // 退出排队 bool Quit(int serial) { if ( NULL==m_head ) { return false; } Custom * p = m_head; while(NULL!=p) { if (serial==p->serial) { return Quit(p); } p = p->next; } return false; } // 叫号,排在队首的出列办理业务 bool Pop(int & serial) { if ( NULL==m_head ) { return false; } else { serial = m_head->serial; return Quit(m_head); } } Custom * m_head; Custom * m_tail; int num; };
#include <iostream> #include <list> #include <string> using namespace std; class User { private: int id; //唯一标识一个用户 string name; int seq; public: User(int id, string name, int seq = 0) { this->id = id; this->name = name; this->seq = 0; } string getName() { return name; } void setName(string name) { this->name = name; } int getSeq() { return seq; } void setSeq(int seq) { this->seq = seq; } int getId() { return id; } bool operator==(const User &u1)const { return (id == u1.id); } }; class MyQueue { private: list<User> userList; public: //进入队列尾部 void enQueue(User u) { u.setSeq(userList.size() + 1); userList.push_back(u); } //队头出队列 void deQueue() { userList.pop_front(); updateSeq(); } //队列中的人随机离开 void deQueue(User& u) { userList.remove(u); updateSeq(); } //出队列后更新队列中每个人的序列 void updateSeq() { int i = 1; list<User>::iterator iter; for (iter = userList.begin(); iter != userList.end(); ++iter) { iter->setSeq(i); i++; } } //打印队列的信息 void printList() { list<User>::iterator iter; for (iter = userList.begin(); iter != userList.end(); ++iter) cout << "id:" << iter->getId() << " name:" << iter->getName() << " seq:" << iter->getSeq() << endl; } }; int main() { User u1(1, "user1"); User u2(2, "user2"); User u3(3, "user3"); User u4(4, "user4"); MyQueue queue; queue.enQueue(u1); queue.enQueue(u2); queue.enQueue(u3); queue.enQueue(u4); queue.deQueue(); //对首元素u1出队列 queue.deQueue(u3); //队列中间的元素u3出队列 queue.printList(); return 0; }
#include <iostream> using namespace std; struct User{ int userID; int pos; User* nextUser; User(int _userId, int _pos): userID(_userId), pos(_pos), nextUser(nullptr) {} }; class myQueue{ public: myQueue(){ dummyHead=new User(-1,0); lastNode=dummyHead; length=0; } ~myQueue(){ delete dummyHead; dummyHead=nullptr; lastNode=nullptr; length=0; } void addUser(int id){ cout<<"addUser: \t"; User* newUser=new User(id, ++length); lastNode->nextUser=newUser; lastNode=newUser; cout<<"The user "<<id<<" is added, its position is "<<newUser->pos<<"."<<endl; } void checkUser(int id){ cout<<"checkUser: \t"; if(length==0){ cout<<"No user in queue."<<endl; return; } User* node=dummyHead->nextUser; while(node){ if(node->userID==id){ cout<<"User found in pos "<<node->pos<<"."<<endl; return; } node=node->nextUser; } cout<<"User not found."<<endl; } void deleteUser(int id){ cout<<"deleteUser: \t"; if(length==0){ cout<<"Cannot delete, no user in queue."<<endl; return; } User* node=dummyHead->nextUser; User* preNode=dummyHead; while(node){ if(node->userID==id){ if(node==lastNode){ delete node; preNode->nextUser=nullptr; lastNode=preNode; cout<<"The last user leaves, no one's position is changed."<<endl; } else{ preNode->nextUser=node->nextUser; delete node; cout<<"The user "<<id<<" leaves."<<endl; node=preNode->nextUser; cout<<"Pos Update: "; while(node){ node->pos--; cout<<"\tThe user "<<node->userID<<"'s new position is "<<node->pos<<"."<<endl; node=node->nextUser; } } return; } node=node->nextUser; preNode=preNode->nextUser; } cout<<"The user id is not found."<<endl; } private: User* dummyHead; User* lastNode; int length; }; int main(){ myQueue queue; queue.addUser(1); queue.addUser(2); queue.addUser(4); queue.checkUser(1); queue.checkUser(3); queue.deleteUser(3); queue.deleteUser(1); queue.deleteUser(4); queue.deleteUser(2); return 0; }
大体思路可以如下,具体函数就不一一实现了 //排队系统 class System{ private: list<User> container; //实际内部所用容器 public: addUser( User型变量 ); //仅在尾部添加用户 deleteUser( User型变量 ); //可在任意位置移除用户,每delete一个User,就要updateUser一下以确保各个User的位置的及时更新 printAllUser(); //遍历容器打印出所有用户的信息 private: updateAllUser(); //在里面遍历容器来重新设定序号 }; //用户 class User{ private: string name; //姓名 int rank; //当前位置 public: User(); //ctor public: getName(); getRank(); public: setRank(); };
/* 1)建立队列=》入队,出队,出队的位置; 2)建立用户=》出队,入队,位置; */ #include<iostream> #include<vector> #include<list> using namespace std; class QueueUp { private: vector<int>m_vector; public: QueueUp(bool m_exit,vector<int>& m_vector) { this->m_exit=false; this->m_dequeuePos=0; this->m_vector=m_vector; } QueueUp(vector<int>& m_vector) { this->m_exit=false; this->m_dequeuePos=0; this->m_vector=m_vector; } int EnQueue(int UserID) { m_vector.push_back(UserID); int len=m_vector.size(); return len; } void DeQUeue(int postion) { m_vector.erase(m_vector.begin()+postion-1); this->m_exit=true; m_dequeuePos=postion; } bool m_exit; int m_dequeuePos; virtual ~QueueUp() { //cout<<"队列销毁!"<<endl; } }; class User { private: int m_Position; int m_UserID; QueueUp*m_Queue; public: User(QueueUp*m_Queue,int m_UserID) { this->m_Queue=m_Queue; this->m_UserID=m_UserID; } User(){} void EnQuene() { this->m_Position=this->m_Queue->EnQueue(this->m_UserID); } void DeQueue() { this->m_Queue->DeQUeue(this->m_Position); } int GetUserID() { return m_UserID; } int GetCurrentPos() { return this->m_Position; } void SetCurrentPos(int m_pos) { this->m_Position=m_pos; } virtual ~User() { // cout<<"用户销毁!"<<endl; } }; bool IsExit(QueueUp &m_Queue,list<User>& pUser) { bool flag=false; if(m_Queue.m_exit) { cout<<m_Queue.m_dequeuePos<<"位置EXIT"<<endl; list<User>::iterator mIt; mIt=pUser.begin(); int j=m_Queue.m_dequeuePos-1; while(j>0){mIt++;j--;} mIt=pUser.erase(mIt); while(mIt!=pUser.end()) { User & m_user2=*mIt; m_user2.SetCurrentPos(m_user2.GetCurrentPos()-1); mIt++; } flag=true; } return flag; } void Test() { vector<int>m_vector; QueueUp m_Queue(m_vector); list<User>pUser; list<User>::iterator mIt; int i=0; for(;i<10;i++) { User mUser(&m_Queue,100+i); mUser.EnQuene(); pUser.push_back(mUser); } mIt=pUser.begin(); while(mIt!=pUser.end()) { cout<<(*mIt).GetUserID()<<"位置:"<<(*mIt).GetCurrentPos()<<endl; mIt++; } //出队 char ch='n'; while(ch!='y') { cout<<"出队的位置:"; int m; cin>>m; cout<<endl; m_Queue.DeQUeue(m); if(IsExit(m_Queue,pUser)) { mIt=pUser.begin(); while(mIt!=pUser.end()) { cout<<(*mIt).GetUserID()<<"位置:"<<(*mIt).GetCurrentPos()<<endl; mIt++; } } cout<<"是否结束(y=yes,n=no):"; cin>>ch; cout<<endl; } } int main() { Test(); return 0; }