实验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. #include <stdc++.h>
  14. //#include <iostream>
  15.  
  16. using namespace std;
  17.  
  18. typedef int elementType;
  19. typedef char elementType1;
  20.  
  21. typedef struct node
  22. {
  23. elementType data;
  24. struct node *link;
  25. }LNode, *PNode;
  26.  
  27. typedef struct charNode
  28. {
  29. elementType1 data;
  30. struct charNode *link;
  31. }CLNode, *CPNode;
  32.  
  33. //typedef struct linkedQueue
  34. //{
  35. // LNode *_front, *_rear;
  36. //}LQueue, *PQueue;
  37.  
  38. // TODO: reference additional headers your program requires here
  39.  
  40. //{{AFX_INSERT_LOCATION}}
  41. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  42.  
  43. #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. class linkedQueue
  15. {
  16. public:
  17. linkedQueue();
  18. virtual ~linkedQueue();
  19. bool emptyLinkedQueue();
  20. //bool fullSeqCircleQueue();
  21. bool enQueue( elementType value );
  22. bool deQueue( elementType &value );
  23. bool getFront( elementType &value );
  24. int length();
  25. void oddOrEven( elementType value );
  26. friend ostream &operator<<( ostream &os, linkedQueue &lq )
  27. {
  28. /*
  29. if( ( scq._front - 1 ) % maxn == scq._rear )
  30. return os;
  31. int column = 0;
  32. for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )
  33. {
  34. os << setw(3) << setiosflags(ios::left) << scq.data[i] << " ";
  35. column ++;
  36. if( column % 10 == 0 )
  37. os << endl;
  38. }
  39. os << endl;
  40. */
  41. if( lq._front == NULL )
  42. return os;
  43. LNode *tmp = lq._front;
  44. int column = ;
  45. while( tmp != lq._rear->link )
  46. {
  47. os << setw() << setiosflags(ios::left) << tmp->data << " ";
  48. column ++;
  49. tmp = tmp->link;
  50. if( column % == )
  51. os << endl;
  52. }
  53. os << endl;
  54. }
  55. private:
  56. LNode *_front;
  57. LNode *_rear;
  58. };
  59.  
  60. #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 -;
  89. }
  90. LNode *tmp = _front;
  91. int _size = ;
  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 & )
  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 & ) && value != )
  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 = ;
  42. while( tmp != clq._rear->link )
  43. {
  44. os << setw() << setiosflags(ios::left) << tmp->data << " ";
  45. column ++;
  46. tmp = tmp->link;
  47. if( column % == )
  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 -;
  89. }
  90. CLNode *tmp = _front;
  91. int _size = ;
  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 = ; i <= ; 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. for( int j = ; j <= ; j ++ )
  89. {
  90. elementType value = rand() % + ;
  91. cout << "The current value = " << value << endl;
  92. LQ1.oddOrEven(value);
  93. }
  94. LQ1.oddOrEven();
  95. /**/
  96. /*
  97. if( CSCQ1.emptyCharSeqCircleQueue() )
  98. cout << "Empty!" << endl;
  99.  
  100. elementType x;
  101. if( SCQ1.deQueue(x) )
  102. {
  103. cout << x << endl;
  104. }
  105. cout << SCQ1;
  106. if( SCQ1.getFront(x) )
  107. cout << x << endl;
  108. cout << SCQ1.length() << endl;
  109.  
  110. if( SCQ1.fullSeqCircleQueue() )
  111. cout << "Full!" << endl;
  112. */
  113. cin.get();
  114. //Sleep( 1000 * 120 );
  115. return ;
  116. }

C++实现链队类——合肥工业大学数据结构实验5:链式队列的更多相关文章

  1. SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...

  2. 数据结构实验之栈与队列十一:refresh的停车场

    数据结构实验之栈与队列十一:refresh的停车场 Description refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多.当停车场满时 ...

  3. SDUT-2449_数据结构实验之栈与队列十:走迷宫

    数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...

  4. SDUT-1479_数据结构实验之栈与队列九:行编辑器

    数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...

  5. SDUT-3335_数据结构实验之栈与队列八:栈的基本操作

    数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...

  6. SDUT-3334_数据结构实验之栈与队列七:出栈序列判定

    数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...

  7. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

  8. SDUT-2134_数据结构实验之栈与队列四:括号匹配

    数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...

  9. SDUT-2133_数据结构实验之栈与队列三:后缀式求值

    数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...

随机推荐

  1. Selenium | 网上教程

    java selenium (一) selenium 介绍 java selenium (二) 环境搭建方法一 java selenium (三) 环境搭建 基于Maven java selenium ...

  2. windows 异步通知I/O模型与重叠I/O模型

    一.异步IO模型(asynchronous IO) (1)什么是异步I/O 异步I/O(asynchronous I/O)由POSIX规范定义.演变成当前POSIX规范的各种早起标准所定义的实时函数中 ...

  3. js实时获取并显示当前时间的方法

  4. redis查数据

    1 连接服务 [root@redis1-20 ~]# telnet 127.0.0.1 6380 Trying 127.0.0.1... Connected to 127.0.0.1. Escape ...

  5. docker监控系统

    第一:docker监控系统之命令行式监控 第二:docker监控系统之cadvisor 第三:docker监控系统之 第四:docker监控系统之

  6. Iahub and Xors Codeforces - 341D

    二维线段树被卡M+T...于是去学二维树状数组区间更新区间查询 树状数组维护数列区间xor的修改.删除(就是把原问题改成一维): 以下p*i实际都指i个p相xor,即(i&1)*pa表示原数列 ...

  7. 【转】grep 用法详解

    有时会使用到,但老忘,转到博客以便学习收藏 转自http://blog.csdn.net/tenfyguo/article/details/6387786 首先要记住的是: 正则表达式与通配符不一样, ...

  8. JS执行保存在数据库中的JS代码

    function createScript(script) { var myScript = document.createElement("script"); myScript. ...

  9. C#实现为类和函数代码自动添加版权注释信息的方法

    这篇文章主要介绍了C#实现为类和函数代码自动添加版权注释信息的方法,主要涉及安装文件的修改及函数注释模板的修改,需要的朋友可以参考下   本文实例讲述了C#实现为类和函数代码自动添加版权注释信息的方法 ...

  10. 【转】PowerManager 与 WakeLock

    PowerManager 与 WakeLock PowerManager 用来控制设备的电源状态. 而PowerManager.WakeLock 也称作唤醒锁, 是一种保持 CPU 运转防止设备休眠的 ...