1. #include <iostream>
  2. using namespace std;
  3. void insert_q(char *q, int *next, int &front, int &rear, char c){
  4. if(front==-1){
  5. front=0;
  6. }
  7. if(rear==-1){
  8. rear=0;
  9. } else {
  10. rear=next[rear];
  11. }
  12. q[rear]=c;
  13. }
  14. void delete_q(char *q, int *next, int &front, int &rear){
  15. front=next[front];
  16. }
  17. void print_q(char *q, int *next, int front, int rear){
  18. cout<<"현재 큐의 상태는 아래와 같다"<<endl;
  19. while(front!=rear){
  20. cout<<q[front]<<endl;
  21. front=next[front];
  22. }
  23. cout<<q[rear]<<endl; //마지막 rear까지 루프 한방에 출력하는 방법이 떠오르지 않아서...
  24. }
  25. int main(){
  26. char q[5];
  27. int next[5]={1,2,3,4,0};
  28. int front=-1, rear=-1;
  29. insert_q(q, next, front, rear, 'P');
  30. insert_q(q, next, front, rear, 'R');
  31. insert_q(q, next, front, rear, 'B');
  32. delete_q(q, next, front, rear);
  33. delete_q(q, next, front, rear);
  34. insert_q(q, next, front, rear, 'J');
  35. insert_q(q, next, front, rear, 'G');
  36. insert_q(q, next, front, rear, 'K');
  37. insert_q(q, next, front, rear, 'S');
  38. print_q(q, next, front, rear);
  39. return 0;
  40. }
Posted by 공돌이pooh
,