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 对于一个基于二元运算符的后缀表示式 ...
随机推荐
- iOS 更改通知栏为白色
1.在 info.plist 中添加View controller-based status bar appearance,值为NO. 2.在设置状态栏的地方添加代码: UIApplication.s ...
- G. 24 观察 + 树状数组
http://codeforces.com/gym/101257/problem/G 首先要看到题目,题目是本来严格大于score[i] > score[j].然后score[i] < s ...
- Java 设置Word页面背景色
Word中可以针对不同文档排版设计要求来设置背景设置颜色.常见的可设置单一颜色.渐变色或加载指定图片来设置成背景.下面通过Java来设置以上3种Word页面背景色. 使用工具:Spire.Doc fo ...
- leetcode410 Split Array Largest Sum
思路: dp. 实现: class Solution { public: int splitArray(vector<int>& nums, int m) { int n = nu ...
- hihocoder1718 最长一次上升子序列
思路: 对于每个i,分别求1~i和i+1~N两部分的最长下降子序列“拼”起来,最终取最大长度即可.学习了如何使用BIT把LIS问题O(N2)算法优化为O(Nlog(N))的算法. https://ww ...
- 【学习笔记】Sass入门指南
本文将介绍Sass的一些基本概念,比如说“变量”.“混合参数”.“嵌套”和“选择器继承”等.著作权归作者所有. 什么是Sass? Sass是一门非常优秀的CSS预处语言,他是由Hampton Catl ...
- 如何创建你的第一个手机APP?
本文使用helloworld来作为android的入门项目,通过这个最简单的项目来帮助大家了解android程序开发包含哪些部分,以及如何运行android程序,本次开发android程序的工具是ec ...
- 接口自动化- 基于 Python
准备工作 这部分其实在谷歌或者百度上搜索下就可以完成的,可是我就是想再啰嗦一遍,说不定有比我更懒的同学呢哈哈~ 第一步 Python的安装配置 打开官网: https://www.python.org ...
- codevs 2905 足球晋级
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description A市举行了一场足球比赛 一共有4n支队伍参加,分成n个小组(每小组4支队伍)进 ...
- Python3简明教程(三)—— 运算符和表达式
运算符 什么是运算符? 举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python支持以下类型的运算符: 算术运算符 关系运算符 赋 ...