数据结构之顺序栈和循环队列
1 //顺序栈 2 #include <iostream> 3 using namespace std; 4 const int StackSize = 5; 5 class SeqStack 6 { 7 public: 8 SeqStack(); 9 ~SeqStack(); 10 void Push(int x); 11 int Pop(); 12 int GetTop(); 13 int Empty(); 14 private: 15 int data[StackSize]; 16 int top; 17 }; 18 SeqStack::SeqStack() 19 { 20 top = -1; 21 } 22 SeqStack::~SeqStack() 23 { 24 25 } 26 int SeqStack::Empty() 27 { 28 if (top == -1) 29 { 30 return 1; 31 } 32 else 33 return 0; 34 } 35 int SeqStack::GetTop() 36 { 37 if (top==-1) 38 { 39 throw "下溢异常"; 40 } 41 else 42 { 43 return data[top]; 44 } 45 } 46 void SeqStack::Push(int x) 47 { 48 if (top==StackSize-1) 49 { 50 throw"上溢"; 51 52 } 53 else 54 { 55 data[++top] = x; 56 } 57 } 58 int SeqStack::Pop() 59 { 60 int x; 61 if (top==-1) 62 { 63 throw"下溢"; 64 } 65 x = data[top--]; 66 return x; 67 } 68 int main() 69 { 70 SeqStack S; 71 S.Push(1); 72 cout << "取栈顶元素:"<<S.GetTop() << endl; 73 try 74 { 75 int x = S.Pop(); 76 cout << "执行一次出栈操作,删除元素" << x << endl; 77 } 78 catch (char const* str) 79 { 80 cout << str << endl; 81 } 82 try 83 { 84 int x = S.Pop(); 85 cout << "执行两次出栈操作,删除元素" << x << endl; 86 } 87 catch (char const* str) 88 { 89 cout << str << endl; 90 } 91 try 92 { 93 int x; 94 cout << "请输入待入栈元素:"; 95 cin >> x; 96 S.Push(x); 97 } 98 catch (char const* str) 99 { 100 cout << str << endl; 101 } 102 if (S.Empty() == 1) 103 cout << "栈为空" << endl; 104 else 105 cout << "栈非空" << endl; 106 return 0; 107 } 108 109 110 //循环队列 111 #include <iostream> 112 using namespace std; 113 const int QueueSize = 5; 114 class CirQueue 115 { 116 public: 117 CirQueue(); 118 ~CirQueue(); 119 void EnQueue(int x);//入队操作 120 int DeQueue();//出队操作,将队头元素出队 121 int GetQueue(); 122 int Empty(); 123 private: 124 int data[QueueSize]; 125 int front, rear; 126 }; 127 CirQueue::CirQueue() 128 { 129 rear = front = QueueSize - 1; 130 } 131 CirQueue::~CirQueue() 132 { 133 134 } 135 void CirQueue::EnQueue(int x) 136 { 137 if ((rear + 1) % QueueSize == front) 138 { 139 throw"上溢"; 140 } 141 rear = (rear + 1) % QueueSize;//队尾指针在循环意义下加1 142 data[rear] = x;//在队尾处插入元素 143 } 144 int CirQueue::DeQueue() 145 { 146 if (rear == front) 147 { 148 throw"下溢"; 149 } 150 front = (front + 1) % QueueSize;//队头指针在循环意义下加1 151 return data[front]; 152 } 153 int CirQueue::GetQueue() 154 { 155 if (rear == front) 156 { 157 throw"下溢"; 158 } 159 return data[(front + 1) % QueueSize]; //注意不修改队头指针 160 } 161 int CirQueue::Empty() 162 { 163 if (front == rear) 164 { 165 return 1; 166 } 167 else 168 { 169 return 0; 170 } 171 } 172 int main() 173 { 174 CirQueue Q; 175 Q.EnQueue(1); 176 Q.EnQueue(2); 177 Q.EnQueue(3); 178 Q.EnQueue(4); 179 //Q.EnQueue(5); 180 cout << "当前队头元素为:" << Q.GetQueue() << endl; 181 try 182 { 183 int x; 184 cout << "请输入入队元素:"; 185 cin >> x; 186 Q.EnQueue(x); 187 } 188 catch (char const* str) { 189 cout << str << endl; 190 } 191 try 192 { 193 int x = Q.DeQueue(); 194 cout << "执行一次出队操作,出队元素是:" << x << endl; 195 } 196 catch (char const* str) { 197 cout << str << endl; 198 } 199 if (Q.Empty() == 1) cout << "队列为空" << endl; 200 else cout << "队列非空" << endl; 201 return 0; 202 }
道阻且长,行则将至

浙公网安备 33010602011771号