1. 程式人生 > >C++實現鏈隊類——合肥工業大學資料結構實驗5:鏈式佇列

C++實現鏈隊類——合肥工業大學資料結構實驗5:鏈式佇列

實驗5

5.1 實驗目的

熟練掌握佇列的順序鏈式儲存結構。

熟練掌握佇列的有關演算法設計,並在鏈佇列上實現。

根據具體給定的需求,合理設計並實現相關結構和演算法。

5.2 實驗要求

5.2.1鏈佇列實驗要求

本次實驗中的鏈佇列結構指不帶頭結點的單鏈表;

鏈佇列結構和運算定義,演算法的實現以庫檔案方式實現,不得在測試主程式中直接實現;

實驗程式有較好可讀性,各運算和變數的命名直觀易懂,符合軟體工程要求;

程式有適當的註釋。

5.3實驗任務

5.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時,演算法退出;

⑤每當鍵盤輸入後,輸出當前佇列中的所有元素。

5.5 執行結果截圖及說明

圖1 測試(1)、(2)、(3)

 

圖2 測試(5)、(6)、(7)

 

圖3 測試(4)

 

圖4 測試(4)

 

圖5 測試(8)

 

5.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__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_) 7 #define AFX_STDAFX_H__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 14 #include <stdc++.h> 15 //#include <iostream> 16 17 using namespace std; 18 19 typedef int elementType; 20 typedef char elementType1; 21 22 typedef struct node 23 { 24 elementType data; 25 struct node *link; 26 }LNode, *PNode; 27 28 typedef struct charNode 29 { 30 elementType1 data; 31 struct charNode *link; 32 }CLNode, *CPNode; 33 34 //typedef struct linkedQueue 35 //{ 36 // LNode *_front, *_rear; 37 //}LQueue, *PQueue; 38 39 // TODO: reference additional headers your program requires here 40 41 //{{AFX_INSERT_LOCATION}} 42 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 43 44 #endif // !defined(AFX_STDAFX_H__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_)

 

 1 // linkedQueue.h: interface for the linkedQueue class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_)
 6 #define AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include "stdafx.h"
13 
14 
15 class linkedQueue  
16 {
17 public:
18     linkedQueue();
19     virtual ~linkedQueue();
20     bool emptyLinkedQueue();
21     //bool fullSeqCircleQueue();
22     bool enQueue( elementType value );
23     bool deQueue( elementType &value );
24     bool getFront( elementType &value );
25     int length();
26     void oddOrEven( elementType value );
27     friend ostream &operator<<( ostream &os, linkedQueue &lq )
28     {
29         /*
30         if( ( scq._front - 1 ) % maxn == scq._rear )
31             return os;
32         int column  = 0;
33         for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )
34         {
35             os << setw(3) << setiosflags(ios::left) << scq.data[i] << " ";
36             column ++;
37             if( column % 10 == 0 )
38                 os << endl;
39         }
40         os << endl;
41         */
42         if( lq._front == NULL )
43             return os;
44         LNode *tmp = lq._front;
45         int column = 0;
46         while( tmp != lq._rear->link )
47         {
48             os << setw(4) << setiosflags(ios::left) << tmp->data << " ";
49             column ++;
50             tmp = tmp->link;
51             if( column % 10 == 0 )
52                 os << endl;
53         }
54         os << endl;
55     }
56 private:
57     LNode *_front;
58     LNode *_rear;
59 };
60 
61 #endif // !defined(AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_)

 

  1 // linkedQueue.cpp: implementation of the linkedQueue class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "linkedQueue.h"
  7 
  8 //////////////////////////////////////////////////////////////////////
  9 // Construction/Destruction
 10 //////////////////////////////////////////////////////////////////////
 11 
 12 linkedQueue::linkedQueue()
 13 {
 14     _front = _rear = NULL;
 15 }
 16 
 17 linkedQueue::~linkedQueue()
 18 {
 19     LNode *tmp = NULL;
 20     while( _front != _rear )
 21     {
 22         tmp = _front;
 23         _front = _front->link;
 24         delete tmp;
 25     }
 26     cout << "The linkedQueue destruction has been called!" << endl;
 27 }
 28 
 29 bool linkedQueue::emptyLinkedQueue()
 30 {
 31     return _front == NULL;
 32 }
 33 
 34 bool linkedQueue::enQueue( elementType value )
 35 {
 36     LNode *newNode = new LNode;
 37     if( !newNode )
 38     {
 39         cerr << "Space allocating falied!Error in linkedQueue::enQueue()!" << endl;
 40         return false;
 41     }
 42     newNode->data = value;
 43     newNode->link = NULL;
 44     if( emptyLinkedQueue() )
 45     {
 46         _front = _rear = newNode;
 47     }
 48     else
 49     {
 50         _rear->link = newNode;
 51         _rear = newNode;
 52     }
 53     return true;
 54 }
 55 
 56 bool linkedQueue::deQueue( elementType &value )
 57 {
 58     if( emptyLinkedQueue() )
 59     {
 60         cerr << "Node deleting falied!Error in linkedQueue::deQueue()!" << endl;
 61         return false;
 62     }
 63     LNode *tmp = _front;
 64     value = _front->data;
 65     _front = _front->link;
 66     delete tmp;
 67     if( _front == NULL )
 68         _rear = NULL;
 69     return true;
 70 }
 71 
 72 bool linkedQueue::getFront( elementType &value )
 73 {
 74     if( emptyLinkedQueue() )
 75     {
 76         cerr << "Queue is empty!\nNode-data acquiring falied!Error in linkedQueue::deQueue()!" << endl;
 77         return false;
 78     }
 79     value = _front->data;
 80     return true;
 81 }
 82 
 83 int linkedQueue::length()
 84 {
 85     if( emptyLinkedQueue() )
 86     {
 87         cerr << "Queue is empty!" << endl;
 88         return -1;
 89     }
 90     LNode *tmp = _front;
 91     int _size = 0;
 92     while( tmp != NULL )
 93     {
 94         tmp = tmp->link;
 95         _size ++;
 96     }
 97     return _size;
 98 }
 99 
100 void linkedQueue::oddOrEven( elementType value )
101 {
102     if( value & 1 )
103     {
104         enQueue(value);
105         cout << value << " will be added to the queue!" << endl;
106         //cout << (*this);
107         cout << "The current queue is as follow:" << endl << (*this);
108         cout << "The current length of the queue is " << (*this).length() << endl;
109 
110     }
111     else if( !( value & 1) && value != 0 )
112     {
113         elementType x;
114         deQueue(x);
115         cout << x << " has been deleted from the queue!" << endl;
116         cout << (*this);
117         cout << "The current queue is as follow:" << endl << (*this);
118         cout << "The current length of the queue is " << (*this).length() << endl;
119     }
120     else //if( value == 0 )
121     {
122         cout << "The value = " << value << ", the _SeqCircleQueue::oddOrEven() has been stoped!" << endl;
123         return;
124     }
125 }

 

 1 // charLinkedQueue.h: interface for the charLinkedQueue class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_)
 6 #define AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 class charLinkedQueue  
13 {
14 public:
15     charLinkedQueue();
16     virtual ~charLinkedQueue();
17     bool emptyCharLinkedQueue();
18     //bool fullSeqCircleQueue();
19     bool enQueue( elementType1 value );
20     bool deQueue( elementType1 &value );
21     bool getFront( elementType1 &value );
22     int length();
23     friend ostream &operator<<( ostream &os, charLinkedQueue &clq )
24     {
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         if( clq._front == NULL )
39             return os;
40         CLNode *tmp = clq._front;
41         int column = 0;
42         while( tmp != clq._rear->link )
43         {
44             os << setw(4) << setiosflags(ios::left) << tmp->data << " ";
45             column ++;
46             tmp = tmp->link;
47             if( column % 10 == 0 )
48                 os << endl;
49         }
50         os << endl;
51     }
52 private:
53     CLNode *_front;
54     CLNode *_rear;
55 
56 };
57 
58 #endif // !defined(AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_)

 

 1 // charLinkedQueue.cpp: implementation of the charLinkedQueue class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #include "stdafx.h"
 6 #include "charLinkedQueue.h"
 7 
 8 //////////////////////////////////////////////////////////////////////
 9 // Construction/Destruction
10 //////////////////////////////////////////////////////////////////////
11 
12 charLinkedQueue::charLinkedQueue()
13 {
14     _front = _rear = NULL;
15 }
16 
17 charLinkedQueue::~charLinkedQueue()
18 {
19     CLNode *tmp = NULL;
20     while( _front != _rear )
21     {
22         tmp = _front;
23         _front = _front->link;
24         delete tmp;
25     }
26     cout << "The charLinkedQueue destruction has been called!" << endl;
27 }
28 
29 bool charLinkedQueue::emptyCharLinkedQueue()
30 {
31     return _front == NULL;
32 }
33 
34 bool charLinkedQueue::enQueue( elementType1 value )
35 {
36     CLNode *newNode = new CLNode;
37     if( !newNode )
38     {
39         cerr << "Space allocating falied!Error in charLinkedQueue::enQueue()!" << endl;
40         return false;
41     }
42     newNode->data = value;
43     newNode->link = NULL;
44     if( emptyCharLinkedQueue() )
45     {
46         _front = _rear = newNode;
47     }
48     else
49     {
50         _rear->link = newNode;
51         _rear = newNode;
52     }
53     return true;
54 }
55 
56 bool charLinkedQueue::deQueue( elementType1 &value )
57 {
58     if( emptyCharLinkedQueue() )
59     {
60         cerr << "Node deleting falied!Error in charLinkedQueue::deQueue()!" << endl;
61         return false;
62     }
63     CLNode *tmp = _front;
64     value = _front->data;
65     _front = _front->link;
66     delete tmp;
67     if( _front == NULL )
68         _rear = NULL;
69     return true;
70 }
71 
72 bool charLinkedQueue::getFront( elementType1 &value )
73 {
74     if( emptyCharLinkedQueue() )
75     {
76         cerr << "Queue is empty!\nNode-data acquiring falied!Error in charLinkedQueue::deQueue()!" << endl;
77         return false;
78     }
79     value = _front->data;
80     return true;
81 }
82 
83 int charLinkedQueue::length()
84 {
85     if( emptyCharLinkedQueue() )
86     {
87         cerr << "Queue is empty!" << endl;
88         return -1;
89     }
90     CLNode *tmp = _front;
91     int _size = 0;
92     while( tmp != NULL )
93     {
94         tmp = tmp->link;
95         _size ++;
96     }
97     return _size;
98 }

 

  1 // _linked_queue.cpp : Defines the entry point for the console application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "charLinkedQueue.h"
  6 #include "linkedQueue.h"
  7 
  8 int main(int argc, char* argv[])
  9 {
 10     ios::sync_with_stdio(false);
 11     //srand( time(NULL) );
 12     linkedQueue LQ1;
 13     /*
 14     charLinkedQueue CLQ1;
 15     
 16     //if( CLQ1.emptyCharLinkedQueue() )
 17         //cout << "The queue is empty!" << endl;
 18     elementType1 value;
 19     while( cin >> value )
 20     //while( ~scanf( "%c", &value ) && value != '#' )
 21     //cin >> value;
 22     //scanf( "%c", &value );
 23     {
 24         if( isdigit(value) || isalpha(value) )
 25         {
 26             cout << value << " will be added to the end of the queue" << endl;
 27             CLQ1.enQueue(value);
 28             cout << "The current queue is as follow:" << endl << CLQ1;
 29             cout << "The current length of the queue is " << CLQ1.length() << endl;
 30         }
 31             
 32         if( value == '#' )
 33             break;
 34     }
 35     
 36     if( LQ1.emptyLinkedQueue() )
 37         cout << "The queue is empty!" << endl;
 38     elementType value;
 39     while( cin >> value )
 40     {
 41         if( (char)value != '#' )
 42         {
 43             cout << value << " will be added to the end of the queue" << endl;
 44             LQ1.enQueue(value);
 45             cout << "The current queue is as follow:" << endl << LQ1;
 46             cout << "The current length of the queue is " << LQ1.length() << endl;
 47         }
 48         else
 49             break;
 50     }
 51     
 52     cout << "The current length of the queue is " << LQ1.length() << endl;
 53     */
 54     for( int i = 1; i <= 10; i ++ )
 55         LQ1.enQueue(i);
 56     cout << "The origin queue is as follow:" << endl << LQ1;
 57     cout << "The current length of the queue is " << LQ1.length() << endl;
 58     /*
 59     for( int j = 0; j < 10; j ++ )
 60     {
 61         int value;
 62         int key = rand() % 3 + 1;
 63         //cout << key << " ";
 64         if( key == 1 )//get the queue-front data
 65         {
 66             LQ1.getFront(value);
 67             cout << "The data of queue-front = " << value << endl;
 68         }
 69         else if( key == 2 )//delete the queue-front data
 70         {
 71             LQ1.deQueue(value);
 72             cout << value << " has been deleted from the queue!" << endl;
 73             cout << "The current queue is as follow:" << endl << LQ1;
 74             cout << "The current length of the queue is " << LQ1.length() << endl;
 75         }
 76         else//add data to the end of the queue
 77         {
 78             value = rand() % 100 + 2;
 79             cout << value << " will be added to the end of the queue" << endl;
 80             LQ1.enQueue(value);
 81             cout << "The current queue is as follow:" << endl << LQ1;
 82             cout << "The current length of the queue is " << LQ1.length() << endl;
 83         }
 84     
 85     }
 86     */
 87 
 88     
 89     for( int j = 1; j <= 10; j ++ )
 90     {
 91         elementType value = rand() % 100 + 2;
 92         cout << "The current value = " << value << endl;
 93         LQ1.oddOrEven(value);
 94     }
 95     LQ1.oddOrEven(0);
 96     /**/
 97     /*
 98     if( CSCQ1.emptyCharSeqCircleQueue() )
 99         cout << "Empty!" << endl;
100 
101     elementType x;
102     if( SCQ1.deQueue(x) )
103     {
104         cout << x << endl;
105     }
106     cout << SCQ1;
107     if( SCQ1.getFront(x) )
108         cout << x << endl;
109     cout << SCQ1.length() << endl;
110     
111     if( SCQ1.fullSeqCircleQueue() )
112         cout << "Full!" << endl;
113     */
114     cin.get();
115     //Sleep( 1000 * 120 );
116     return 0;
117 }