• 数据结构实验4:C++实现循环队列


    实验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总结

    注意细节!

  • 相关阅读:
    python 全栈开发,Day127(app端内容播放,web端的玩具,app通过websocket远程遥控玩具播放内容,玩具管理页面)
    队列Queue FIFO先进先出 栈Stack FILO先进后出
    sql server 2008 学习笔记
    c#剪切板
    c#复制图片到粘贴板
    C# GUID的使用
    winform自定义控件
    进程和线程
    vs常用快捷键
    c# foreach循环二维数组
  • 原文地址:https://www.cnblogs.com/25th-engineer/p/9940885.html
Copyright © 2020-2023  润新知