資料結構實驗4:C++實現迴圈佇列
阿新 • • 發佈:2018-11-10
實驗4
4.1 實驗目的
熟練掌握佇列的順序儲存結構和鏈式儲存結構。
熟練掌握佇列的有關演算法設計,並在迴圈順序佇列和鏈佇列上實現。
根據具體給定的需求,合理設計並實現相關結構和演算法。
4.2 實驗要求
4.2.1 迴圈順序佇列的實驗要求
迴圈順序佇列結構和運算定義,演算法的實現以庫檔案方式實現,不得在測試主程式中直接實現;
實驗程式有較好可讀性,各運算和變數的命名直觀易懂,符合軟體工程要求;
程式有適當的註釋。
4.3 實驗任務
4.3.1 迴圈順序佇列實驗任務
編寫演算法實現下列問題的求解。
<1>初始化一個佇列。
<2>判斷是否隊空。
<3>判斷是否隊滿。
設佇列最大長度:MaxLen=100
第一組資料:入隊n個元素,判斷隊滿
第二組資料:用迴圈方式將1到99,99個元素入隊,判隊滿
<4>入隊
第一組資料:4,7,8,12,20,50
第二組資料:a,b,c,d,f,g
<5>出隊
<6>取隊頭元素
<7>求當前佇列中元素個數
<8>編寫演算法實現
①初始化空迴圈佇列;
②當鍵盤輸入奇數時,此奇數入隊;
③當鍵盤輸入偶數時,隊頭出隊;
④當鍵盤輸入0時,演算法退出;
⑤每當鍵盤輸入後,輸出當前佇列中的所有元素。
4.5 執行結果截圖及說明
圖1 測試(1)、(2)、(3)、(5)、(6)、(7)
圖2 測試(4)
圖3 測試(4)
圖4 測試(8)
4.6 附原始碼
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, but 3 // are changed infrequently 4 // 5 6 #if !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_) 7#define AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 #include <stdc++.h> 14 15 using namespace std; 16 17 typedef int elementType; 18 typedef char elementType1; 19 const int maxn = 100; 20 21 // TODO: reference additional headers your program requires here 22 23 //{{AFX_INSERT_LOCATION}} 24 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 25 26 #endif // !defined(AFX_STDAFX_H__8FA49CDF_FC99_4984_AB37_46921F7ED357__INCLUDED_)
1 // _SeqCircleQueue.h: interface for the _SeqCircleQueue class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_) 6 #define AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 class _SeqCircleQueue 13 { 14 public: 15 _SeqCircleQueue(); 16 virtual ~_SeqCircleQueue(); 17 bool emptySeqCircleQueue(); 18 bool fullSeqCircleQueue(); 19 bool enQueue( elementType value ); 20 bool deQueue( elementType &value ); 21 bool getFront( elementType &value ); 22 int length(); 23 void oddOrEven( elementType value ); 24 friend ostream &operator<<( ostream &os, _SeqCircleQueue &scq ) 25 { 26 if( ( scq._front - 1 ) % maxn == scq._rear ) 27 return os; 28 int column = 0; 29 for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn ) 30 { 31 os << setw(3) << setiosflags(ios::left) << scq.data[i] << " "; 32 column ++; 33 if( column % 10 == 0 ) 34 os << endl; 35 } 36 os << endl; 37 } 38 private: 39 elementType data[maxn]; 40 int _front; 41 int _rear; 42 43 }; 44 45 #endif // !defined(AFX__SEQCIRCLEQUEUE_H__FCBC0603_27E1_4352_833C_6BED9B418B96__INCLUDED_)
1 // _SeqCircleQueue.cpp: implementation of the _SeqCircleQueue class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "_SeqCircleQueue.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 _SeqCircleQueue::_SeqCircleQueue() 13 { 14 _front = _rear = 0; 15 } 16 17 _SeqCircleQueue::~_SeqCircleQueue() 18 { 19 ios::sync_with_stdio(false); 20 cout << "The _SeqCircleQueue destruction has been called!" << endl; 21 } 22 23 bool _SeqCircleQueue::emptySeqCircleQueue() 24 { 25 return _front == _rear; 26 } 27 28 bool _SeqCircleQueue::fullSeqCircleQueue() 29 { 30 return ( _rear + 1 ) % maxn == _front; 31 } 32 33 bool _SeqCircleQueue::enQueue( elementType value ) 34 { 35 if( fullSeqCircleQueue() ) 36 { 37 cerr << "Seq-Circle-Queue is full!Error in _SeqCircleQueue::enQueue()!" << endl; 38 return false; 39 } 40 data[_rear] = value; 41 _rear = ( _rear + 1 ) % maxn; 42 return true; 43 } 44 45 bool _SeqCircleQueue::deQueue( elementType &value ) 46 { 47 if( emptySeqCircleQueue() ) 48 { 49 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::popFront()!" << endl; 50 return false; 51 } 52 value = data[_front]; 53 _front = ( _front + 1 ) % maxn; 54 return true; 55 } 56 57 bool _SeqCircleQueue::getFront( elementType &value ) 58 { 59 if( emptySeqCircleQueue() ) 60 { 61 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::getFront()!" << endl; 62 return false; 63 } 64 value = data[_front]; 65 return true; 66 } 67 68 int _SeqCircleQueue::length() 69 { 70 if( emptySeqCircleQueue() ) 71 { 72 cerr << "Seq-Circle-Queue is empty!Error in _SeqCircleQueue::length()!" << endl; 73 return -1; 74 } 75 return ( _rear - _front + maxn ) % maxn; 76 } 77 78 void _SeqCircleQueue::oddOrEven( elementType value ) 79 { 80 if( value & 1 ) 81 { 82 enQueue(value); 83 cout << value << " will be added to the queue!" << endl; 84 cout << (*this); 85 } 86 else if( !( value & 1) && value != 0 ) 87 { 88 elementType x; 89 deQueue(x); 90 cout << x << " has been deleted from the queue!" << endl; 91 cout << (*this); 92 } 93 else //if( value == 0 ) 94 { 95 cout << "The _SeqCircleQueue::oddOrEven() has been stoped!" << endl; 96 return; 97 } 98 }
1 // charSeqCircleQueue.h: interface for the charSeqCircleQueue class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #if !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_) 6 #define AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 12 class charSeqCircleQueue 13 { 14 public: 15 charSeqCircleQueue(); 16 virtual ~charSeqCircleQueue(); 17 bool emptyCharSeqCircleQueue(); 18 bool fullCharSeqCircleQueue(); 19 bool enQueue( elementType1 value ); 20 bool deQueue( elementType1 &value ); 21 bool getFront( elementType1 &value ); 22 int length(); 23 friend ostream &operator<<( ostream &os, charSeqCircleQueue &cscq ) 24 { 25 if( ( cscq._front - 1 ) % maxn == cscq._rear ) 26 return os; 27 int column = 0; 28 for( int i = cscq._front; i % maxn != cscq._rear; i = ( i + 1 ) % maxn ) 29 { 30 os << setw(3) << setiosflags(ios::left) << cscq.data[i] << " "; 31 column ++; 32 if( column % 10 == 0 ) 33 os << endl; 34 } 35 os << endl; 36 } 37 private: 38 elementType1 data[maxn]; 39 int _front; 40 int _rear; 41 42 }; 43 44 #endif // !defined(AFX_CHARSEQCIRCLEQUEUE_H__FBB4F8DD_2EF9_43A6_8E23_FD7E4C56908E__INCLUDED_)
1 // charSeqCircleQueue.cpp: implementation of the charSeqCircleQueue class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "charSeqCircleQueue.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 charSeqCircleQueue::charSeqCircleQueue() 13 { 14 _front = _rear = 0; 15 } 16 17 charSeqCircleQueue::~charSeqCircleQueue() 18 { 19 ios::sync_with_stdio(false); 20 cout << "The charSeqCircleQueue destruction has been called!" << endl; 21 } 22 23 bool charSeqCircleQueue::emptyCharSeqCircleQueue() 24 { 25 return _front == _rear; 26 } 27 28 bool charSeqCircleQueue::fullCharSeqCircleQueue() 29 { 30 return ( _rear + 1 ) % maxn == _front; 31 } 32 33 bool charSeqCircleQueue::enQueue( elementType1 value ) 34 { 35 if( fullCharSeqCircleQueue() ) 36 { 37 cerr << "Seq-Circle-Queue is full!Error in charSeqCircleQueue::::enQueue()!" << endl; 38 return false; 39 } 40 data[_rear] = value; 41 _rear = ( _rear + 1 ) % maxn; 42 return true; 43 } 44 45 bool charSeqCircleQueue::deQueue( elementType1 &value ) 46 { 47 if( emptyCharSeqCircleQueue() ) 48 { 49 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::popFront()!" << endl; 50 return false; 51 } 52 value = data[_front]; 53 _front = ( _front + 1 ) % maxn; 54 return true; 55 } 56 57 bool charSeqCircleQueue::getFront( elementType1 &value ) 58 { 59 if( emptyCharSeqCircleQueue() ) 60 { 61 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::getFront()!" << endl; 62 return false; 63 } 64 value = data[_front]; 65 return true; 66 } 67 68 int charSeqCircleQueue::length() 69 { 70 if( emptyCharSeqCircleQueue() ) 71 { 72 cerr << "Seq-Circle-Queue is empty!Error in charSeqCircleQueue::::length()!" << endl; 73 return -1; 74 } 75 return ( _rear - _front + maxn ) % maxn; 76 }
4.7 除錯過程中出現的bug總結
注意細節!