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 对于一个基于二元运算符的后缀表示式 ...
随机推荐
- Jmeter之Json Path Extractor 接受上一个请求的响应参数
最近在使用Jmeter进行接口测试,被一个问题困扰了很久,就是第二个请求如何接收上一个请求响应中的参数,刚开始尝试着用网上普遍说的正则表达式,长了了N多次之,都没有达到我想要的效果,被整的够惨,于是, ...
- 使用Ctex中遇到的一些问题
一般下载好Ctex,我是使用Latex+dvi2pdf完成编译的,但是发现推荐的使用为:1)运行CCT & Latex命令生成两次dvi和ps文件 2)使用dvi2pdf编译dvi文件生成pd ...
- 并查集 HDOJ 5441 Travel
题目传送门 题意:给一张无向图,问存在多少(a, b)表示a点到b点经过的边值小于等于x ((a,b) 和 (b, a)属于不同的方案) 分析:首先将边权值和查询x值升序排序,从前往后扫描边,累加从u ...
- 147 Insertion Sort List 链表插入排序
用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ Java实现: 链表的插入排序实现原理很 ...
- 使用真正的 Redux 和 React-redux
现在 make-react-redux 工程代码中的 Redux 和 React-redux 都是我们自己写的,现在让我们来使用真正的官方版本的 Redux 和 React-redux. 在工程目录下 ...
- Haproxy+Rabbitmq中的问题
问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...
- hashlib加密模块详解
1.hash是把任意长度的消息压缩到某一固定长度的数值的函数. hash主要用于安全加密,把一些不同长度的信息转化成杂乱的128位编码里,叫做hash值. hash就是把内容和内容地址之间找到一种映射 ...
- linux下自定义pid实现任意数据采集
当你需要采集特殊的数据,而不满足于现有的你所知的数据模版时,自定义oid将是你必须而且非常好的解决方式. oid是snmp服务器为每条系统信息提供的唯一标识符,如果不能很好理解snmp服务的话,可以将 ...
- SQL Server时间类型datetime
SQL Server时间类型datetime 兼容ADO的COleDateTime. SQL datetime 日期和时间数据,可表示1753.1.1 至 9999.12.31的时间,精度为1/300 ...
- NIO入门之轻松读取大文件
NIO入门之轻松读取大文件 今天同事碰到了一个问题,从游戏服务器下载下来的输出log有一个多G大.用记事本打不开,EditPlus也打不开,都提示文件太大.用word也打不开,提示文件大于512M.打 ...