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 对于一个基于二元运算符的后缀表示式 ...
随机推荐
- C++ typedef typename 作用
C++ typedef typename 作用 C++的一些语法让人看着费解,其中就有: typedef typename std::vector<T>::size_type size_t ...
- 洛谷 P1072 Hankson 的趣味题 || 打质数表的分解质因数
方法就是枚举,根据b0和b1可以大大减小枚举范围,方法类似这个http://blog.csdn.net/hehe_54321/article/details/76021615 将b0和b1都分解质因数 ...
- double发生精度丢失的解决办法
发生精度丢失的原因: 个人理解:机器在运行时,使用2进制形式的计数方式,而我们日常生活中的计算是10进制的,对于整数的加减乘除,double还能适用,但是对于有小数的,则容易发生精度丢失,即用2进制表 ...
- HTTP提交方式之PUT详细介绍及POST和PUT的区别
Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标准(现行的HTTP/1.1)其实还有OPTIONS,GET,HEAD, ...
- 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数
给定一个整数数组,除了某个元素外其余元素均出现两次.请找出这个只出现一次的元素.备注:你的算法应该是一个线性时间复杂度. 你可以不用额外空间来实现它吗? 详见:https://leetcode.com ...
- 机器学习概念之特征处理(Feature processing)
不多说,直接上干货! 肯定也有不少博友,跟我一样,刚开始接触的时候,会对这三个概念混淆. 以下是,特征处理.特征提取.特征转换和特征选择的区别! 特征处理主要包含三个方面:特征提取.特征转换和特征选择 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第六天(非原创)
文章大纲 一.课程介绍二.今日内容简单介绍三.Httpclient介绍与实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工 ...
- CF779B(round 402 div.2 B) Weird Rounding
题意: Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the ...
- Vue 2.0入门基础知识之内部指令
1.Vue.js介绍 当前前端三大主流框架:Angular.React.Vue.React前段时间由于许可证风波,使得Vue的热度蹭蹭地上升.另外,Vue友好的API文档更是一大特色.Vue.js是一 ...
- nginx 访问localhost老是下载文件不能打开网页什么情况?
nginx打开网页直接下载文件的问题 nginx sites-available文件里的default已经修改过root 路径了. 但是访问localhost的时候总是直接下载网页而不是打开网址 很奇 ...