zoukankan      html  css  js  c++  java
  • 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!
    Node-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!
    Node-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 }
  • 相关阅读:
    tnagios
    python-gearman使用
    yaml
    中国大陆互联网国际出口情况(2015年)
    vsftpd配置
    spoj-ASSIGN-bitDP
    spoj-ANARC05H -dp
    Light oj 1379 -- 最短路
    SPOJ-394-ACODE
    2018年东北农业大学春季校赛
  • 原文地址:https://www.cnblogs.com/25th-engineer/p/9955136.html
Copyright © 2011-2022 走看看