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 附源代码
- // stdafx.h : include file for standard system include files,
- // or project specific include files that are used frequently, but
- // are changed infrequently
- //
- #if !defined(AFX_STDAFX_H__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_)
- #define AFX_STDAFX_H__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include <stdc++.h>
- //#include <iostream>
- using namespace std;
- typedef int elementType;
- typedef char elementType1;
- typedef struct node
- {
- elementType data;
- struct node *link;
- }LNode, *PNode;
- typedef struct charNode
- {
- elementType1 data;
- struct charNode *link;
- }CLNode, *CPNode;
- //typedef struct linkedQueue
- //{
- // LNode *_front, *_rear;
- //}LQueue, *PQueue;
- // TODO: reference additional headers your program requires here
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
- #endif // !defined(AFX_STDAFX_H__009A1125_2F9A_4B93_9830_51B9398EBC52__INCLUDED_)
- // linkedQueue.h: interface for the linkedQueue class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_)
- #define AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include "stdafx.h"
- class linkedQueue
- {
- public:
- linkedQueue();
- virtual ~linkedQueue();
- bool emptyLinkedQueue();
- //bool fullSeqCircleQueue();
- bool enQueue( elementType value );
- bool deQueue( elementType &value );
- bool getFront( elementType &value );
- int length();
- void oddOrEven( elementType value );
- friend ostream &operator<<( ostream &os, linkedQueue &lq )
- {
- /*
- if( ( scq._front - 1 ) % maxn == scq._rear )
- return os;
- int column = 0;
- for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )
- {
- os << setw(3) << setiosflags(ios::left) << scq.data[i] << " ";
- column ++;
- if( column % 10 == 0 )
- os << endl;
- }
- os << endl;
- */
- if( lq._front == NULL )
- return os;
- LNode *tmp = lq._front;
- int column = ;
- while( tmp != lq._rear->link )
- {
- os << setw() << setiosflags(ios::left) << tmp->data << " ";
- column ++;
- tmp = tmp->link;
- if( column % == )
- os << endl;
- }
- os << endl;
- }
- private:
- LNode *_front;
- LNode *_rear;
- };
- #endif // !defined(AFX_LINKEDQUEUE_H__6DF75075_BE53_4235_872D_3381A1A450D0__INCLUDED_)
- // linkedQueue.cpp: implementation of the linkedQueue class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "linkedQueue.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- linkedQueue::linkedQueue()
- {
- _front = _rear = NULL;
- }
- linkedQueue::~linkedQueue()
- {
- LNode *tmp = NULL;
- while( _front != _rear )
- {
- tmp = _front;
- _front = _front->link;
- delete tmp;
- }
- cout << "The linkedQueue destruction has been called!" << endl;
- }
- bool linkedQueue::emptyLinkedQueue()
- {
- return _front == NULL;
- }
- bool linkedQueue::enQueue( elementType value )
- {
- LNode *newNode = new LNode;
- if( !newNode )
- {
- cerr << "Space allocating falied!Error in linkedQueue::enQueue()!" << endl;
- return false;
- }
- newNode->data = value;
- newNode->link = NULL;
- if( emptyLinkedQueue() )
- {
- _front = _rear = newNode;
- }
- else
- {
- _rear->link = newNode;
- _rear = newNode;
- }
- return true;
- }
- bool linkedQueue::deQueue( elementType &value )
- {
- if( emptyLinkedQueue() )
- {
- cerr << "Node deleting falied!Error in linkedQueue::deQueue()!" << endl;
- return false;
- }
- LNode *tmp = _front;
- value = _front->data;
- _front = _front->link;
- delete tmp;
- if( _front == NULL )
- _rear = NULL;
- return true;
- }
- bool linkedQueue::getFront( elementType &value )
- {
- if( emptyLinkedQueue() )
- {
- cerr << "Queue is empty!\nNode-data acquiring falied!Error in linkedQueue::deQueue()!" << endl;
- return false;
- }
- value = _front->data;
- return true;
- }
- int linkedQueue::length()
- {
- if( emptyLinkedQueue() )
- {
- cerr << "Queue is empty!" << endl;
- return -;
- }
- LNode *tmp = _front;
- int _size = ;
- while( tmp != NULL )
- {
- tmp = tmp->link;
- _size ++;
- }
- return _size;
- }
- void linkedQueue::oddOrEven( elementType value )
- {
- if( value & )
- {
- enQueue(value);
- cout << value << " will be added to the queue!" << endl;
- //cout << (*this);
- cout << "The current queue is as follow:" << endl << (*this);
- cout << "The current length of the queue is " << (*this).length() << endl;
- }
- else if( !( value & ) && value != )
- {
- elementType x;
- deQueue(x);
- cout << x << " has been deleted from the queue!" << endl;
- cout << (*this);
- cout << "The current queue is as follow:" << endl << (*this);
- cout << "The current length of the queue is " << (*this).length() << endl;
- }
- else //if( value == 0 )
- {
- cout << "The value = " << value << ", the _SeqCircleQueue::oddOrEven() has been stoped!" << endl;
- return;
- }
- }
- // charLinkedQueue.h: interface for the charLinkedQueue class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_)
- #define AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- class charLinkedQueue
- {
- public:
- charLinkedQueue();
- virtual ~charLinkedQueue();
- bool emptyCharLinkedQueue();
- //bool fullSeqCircleQueue();
- bool enQueue( elementType1 value );
- bool deQueue( elementType1 &value );
- bool getFront( elementType1 &value );
- int length();
- friend ostream &operator<<( ostream &os, charLinkedQueue &clq )
- {
- /*
- if( ( scq._front - 1 ) % maxn == scq._rear )
- return os;
- int column = 0;
- for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )
- {
- os << setw(3) << setiosflags(ios::left) << scq.data[i] << " ";
- column ++;
- if( column % 10 == 0 )
- os << endl;
- }
- os << endl;
- */
- if( clq._front == NULL )
- return os;
- CLNode *tmp = clq._front;
- int column = ;
- while( tmp != clq._rear->link )
- {
- os << setw() << setiosflags(ios::left) << tmp->data << " ";
- column ++;
- tmp = tmp->link;
- if( column % == )
- os << endl;
- }
- os << endl;
- }
- private:
- CLNode *_front;
- CLNode *_rear;
- };
- #endif // !defined(AFX_CHARLINKEDQUEUE_H__91C9120D_49FD_417A_8336_57503196B63F__INCLUDED_)
- // charLinkedQueue.cpp: implementation of the charLinkedQueue class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "charLinkedQueue.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- charLinkedQueue::charLinkedQueue()
- {
- _front = _rear = NULL;
- }
- charLinkedQueue::~charLinkedQueue()
- {
- CLNode *tmp = NULL;
- while( _front != _rear )
- {
- tmp = _front;
- _front = _front->link;
- delete tmp;
- }
- cout << "The charLinkedQueue destruction has been called!" << endl;
- }
- bool charLinkedQueue::emptyCharLinkedQueue()
- {
- return _front == NULL;
- }
- bool charLinkedQueue::enQueue( elementType1 value )
- {
- CLNode *newNode = new CLNode;
- if( !newNode )
- {
- cerr << "Space allocating falied!Error in charLinkedQueue::enQueue()!" << endl;
- return false;
- }
- newNode->data = value;
- newNode->link = NULL;
- if( emptyCharLinkedQueue() )
- {
- _front = _rear = newNode;
- }
- else
- {
- _rear->link = newNode;
- _rear = newNode;
- }
- return true;
- }
- bool charLinkedQueue::deQueue( elementType1 &value )
- {
- if( emptyCharLinkedQueue() )
- {
- cerr << "Node deleting falied!Error in charLinkedQueue::deQueue()!" << endl;
- return false;
- }
- CLNode *tmp = _front;
- value = _front->data;
- _front = _front->link;
- delete tmp;
- if( _front == NULL )
- _rear = NULL;
- return true;
- }
- bool charLinkedQueue::getFront( elementType1 &value )
- {
- if( emptyCharLinkedQueue() )
- {
- cerr << "Queue is empty!\nNode-data acquiring falied!Error in charLinkedQueue::deQueue()!" << endl;
- return false;
- }
- value = _front->data;
- return true;
- }
- int charLinkedQueue::length()
- {
- if( emptyCharLinkedQueue() )
- {
- cerr << "Queue is empty!" << endl;
- return -;
- }
- CLNode *tmp = _front;
- int _size = ;
- while( tmp != NULL )
- {
- tmp = tmp->link;
- _size ++;
- }
- return _size;
- }
- // _linked_queue.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "charLinkedQueue.h"
- #include "linkedQueue.h"
- int main(int argc, char* argv[])
- {
- ios::sync_with_stdio(false);
- //srand( time(NULL) );
- linkedQueue LQ1;
- /*
- charLinkedQueue CLQ1;
- //if( CLQ1.emptyCharLinkedQueue() )
- //cout << "The queue is empty!" << endl;
- elementType1 value;
- while( cin >> value )
- //while( ~scanf( "%c", &value ) && value != '#' )
- //cin >> value;
- //scanf( "%c", &value );
- {
- if( isdigit(value) || isalpha(value) )
- {
- cout << value << " will be added to the end of the queue" << endl;
- CLQ1.enQueue(value);
- cout << "The current queue is as follow:" << endl << CLQ1;
- cout << "The current length of the queue is " << CLQ1.length() << endl;
- }
- if( value == '#' )
- break;
- }
- if( LQ1.emptyLinkedQueue() )
- cout << "The queue is empty!" << endl;
- elementType value;
- while( cin >> value )
- {
- if( (char)value != '#' )
- {
- cout << value << " will be added to the end of the queue" << endl;
- LQ1.enQueue(value);
- cout << "The current queue is as follow:" << endl << LQ1;
- cout << "The current length of the queue is " << LQ1.length() << endl;
- }
- else
- break;
- }
- cout << "The current length of the queue is " << LQ1.length() << endl;
- */
- for( int i = ; i <= ; i ++ )
- LQ1.enQueue(i);
- cout << "The origin queue is as follow:" << endl << LQ1;
- cout << "The current length of the queue is " << LQ1.length() << endl;
- /*
- for( int j = 0; j < 10; j ++ )
- {
- int value;
- int key = rand() % 3 + 1;
- //cout << key << " ";
- if( key == 1 )//get the queue-front data
- {
- LQ1.getFront(value);
- cout << "The data of queue-front = " << value << endl;
- }
- else if( key == 2 )//delete the queue-front data
- {
- LQ1.deQueue(value);
- cout << value << " has been deleted from the queue!" << endl;
- cout << "The current queue is as follow:" << endl << LQ1;
- cout << "The current length of the queue is " << LQ1.length() << endl;
- }
- else//add data to the end of the queue
- {
- value = rand() % 100 + 2;
- cout << value << " will be added to the end of the queue" << endl;
- LQ1.enQueue(value);
- cout << "The current queue is as follow:" << endl << LQ1;
- cout << "The current length of the queue is " << LQ1.length() << endl;
- }
- }
- */
- for( int j = ; j <= ; j ++ )
- {
- elementType value = rand() % + ;
- cout << "The current value = " << value << endl;
- LQ1.oddOrEven(value);
- }
- LQ1.oddOrEven();
- /**/
- /*
- if( CSCQ1.emptyCharSeqCircleQueue() )
- cout << "Empty!" << endl;
- elementType x;
- if( SCQ1.deQueue(x) )
- {
- cout << x << endl;
- }
- cout << SCQ1;
- if( SCQ1.getFront(x) )
- cout << x << endl;
- cout << SCQ1.length() << endl;
- if( SCQ1.fullSeqCircleQueue() )
- cout << "Full!" << endl;
- */
- cin.get();
- //Sleep( 1000 * 120 );
- return ;
- }
C++实现链队类——合肥工业大学数据结构实验5:链式队列的更多相关文章
- SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场
数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...
- 数据结构实验之栈与队列十一:refresh的停车场
数据结构实验之栈与队列十一:refresh的停车场 Description refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多.当停车场满时 ...
- SDUT-2449_数据结构实验之栈与队列十:走迷宫
数据结构实验之栈与队列十:走迷宫 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个由n * m 个格子组成的迷宫,起 ...
- SDUT-1479_数据结构实验之栈与队列九:行编辑器
数据结构实验之栈与队列九:行编辑器 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一个简单的行编辑程序的功能是:接受用 ...
- SDUT-3335_数据结构实验之栈与队列八:栈的基本操作
数据结构实验之栈与队列八:栈的基本操作 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 堆栈是一种基本的数据结构.堆栈具 ...
- SDUT-3334_数据结构实验之栈与队列七:出栈序列判定
数据结构实验之栈与队列七:出栈序列判定 Time Limit: 30 ms Memory Limit: 1000 KiB Problem Description 给一个初始的入栈序列,其次序即为元素的 ...
- SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值
数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...
- SDUT-2134_数据结构实验之栈与队列四:括号匹配
数据结构实验之栈与队列四:括号匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给你一串字符,不超过50个字符,可能 ...
- SDUT-2133_数据结构实验之栈与队列三:后缀式求值
数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...
随机推荐
- Selenium | 网上教程
java selenium (一) selenium 介绍 java selenium (二) 环境搭建方法一 java selenium (三) 环境搭建 基于Maven java selenium ...
- windows 异步通知I/O模型与重叠I/O模型
一.异步IO模型(asynchronous IO) (1)什么是异步I/O 异步I/O(asynchronous I/O)由POSIX规范定义.演变成当前POSIX规范的各种早起标准所定义的实时函数中 ...
- js实时获取并显示当前时间的方法
- redis查数据
1 连接服务 [root@redis1-20 ~]# telnet 127.0.0.1 6380 Trying 127.0.0.1... Connected to 127.0.0.1. Escape ...
- docker监控系统
第一:docker监控系统之命令行式监控 第二:docker监控系统之cadvisor 第三:docker监控系统之 第四:docker监控系统之
- Iahub and Xors Codeforces - 341D
二维线段树被卡M+T...于是去学二维树状数组区间更新区间查询 树状数组维护数列区间xor的修改.删除(就是把原问题改成一维): 以下p*i实际都指i个p相xor,即(i&1)*pa表示原数列 ...
- 【转】grep 用法详解
有时会使用到,但老忘,转到博客以便学习收藏 转自http://blog.csdn.net/tenfyguo/article/details/6387786 首先要记住的是: 正则表达式与通配符不一样, ...
- JS执行保存在数据库中的JS代码
function createScript(script) { var myScript = document.createElement("script"); myScript. ...
- C#实现为类和函数代码自动添加版权注释信息的方法
这篇文章主要介绍了C#实现为类和函数代码自动添加版权注释信息的方法,主要涉及安装文件的修改及函数注释模板的修改,需要的朋友可以参考下 本文实例讲述了C#实现为类和函数代码自动添加版权注释信息的方法 ...
- 【转】PowerManager 与 WakeLock
PowerManager 与 WakeLock PowerManager 用来控制设备的电源状态. 而PowerManager.WakeLock 也称作唤醒锁, 是一种保持 CPU 运转防止设备休眠的 ...