C++实现链队类——合肥工业大学数据结构实验5:链式队列

时间:2023-12-29 14:02:20

实验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 运行结果截图及说明

C++实现链队类——合肥工业大学数据结构实验5:链式队列

图1 测试(1)、(2)、(3)

C++实现链队类——合肥工业大学数据结构实验5:链式队列

图2 测试(5)、(6)、(7)

C++实现链队类——合肥工业大学数据结构实验5:链式队列

图3 测试(4)

C++实现链队类——合肥工业大学数据结构实验5:链式队列

图4 测试(4)

C++实现链队类——合肥工业大学数据结构实验5:链式队列

图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 ;
}