队列的一些操作
RT:
1 #include <iostream>
2
3 using namespace std;
4
5 class Queue
6 {
7 private:
8 typedef struct node
9 {
10 int data;
11 struct node *next;
12 }_Node;
13 _Node *headPtr,*tailPtr;
14 int Len;
15 public:
16 Queue();
17 ~Queue();
18 int size()const;
19 bool isempty()const;
20 void Qshow()const;
21 void enqueue(int x);
22 void dequeue();
23 int front()const;
24 };
25
26 Queue::Queue()
27 {
28 headPtr = NULL;
29 tailPtr = NULL;
30 Len = 0;
31 }
32
33 Queue::~Queue()
34 {
35 _Node *tmp;
36 while(headPtr != NULL)
37 {
38 tmp = headPtr;
39 headPtr = headPtr->next;
40 delete tmp;
41 }
42 }
43
44 int Queue::size()const
45 {
46 return(Len);
47 }
48
49 bool Queue::isempty()const
50 {
51 if(headPtr==NULL)
52 return true;
53 else
54 return false;
55 }
56
57 void Queue::Qshow()const
58 {
59 if(headPtr == NULL)
60 {
61 cout<<"Queue is empty!"<<endl;
62 }
63 cout<<"\nThere are %d record:\n";
64 _Node *tmp = headPtr;
65 while(tmp != NULL)
66 {
67 cout<<tmp->data<<" ";
68 tmp = tmp->next;
69 }
70 cout<<endl;
71 }
72
73 void Queue::enqueue(int x)
74 {
75 _Node *tmp;
76 tmp = new _Node;
77 tmp->data = x;
78 tmp->next = NULL;
79 if(headPtr == NULL)
80 headPtr = tmp;
81 else
82 tailPtr->next = tmp;
83 tailPtr =tmp;
84 Len++;
85 }
86
87 void Queue::dequeue()
88 {
89 _Node *tmp;
90 if(headPtr == NULL)
91 {
92 cout<<"yichu!!"<<endl;
93 return;
94 }else
95 {
96 tmp = headPtr;
97 headPtr = headPtr->next;
98 }
99 delete tmp;
100 Len--;
101 }
102
103 int Queue::front()const
104 {
105 if(headPtr == NULL)
106 {
107 cout<<"\noverflow!!\n";
108 return -1;
109 }else
110 {
111 return headPtr->data;
112 }
113 }
114
115 int main(void)
116 {
117 Queue iQ;
118 int x;
119 cout<<"create queue number:"<<endl;
120 while(cin>>x && x)
121 {
122 iQ.enqueue(x);
123 }
124 cout<<"Queue Size: "<<iQ.size()<<endl;
125 while(!(iQ.isempty()))
126 {
127 cout<<iQ.front()<<" ";
128 iQ.dequeue();
129 }
130 cout<<endl;
131 return 0;
132 }

浙公网安备 33010602011771号