(1)初始化尘山队列q;
(2)判断队列q是否非空;
(3)依次进队元素100、909、44、8;
(4)出队一个元素,输出该元素;
(5)派世中输出队列q的元素个数;
(6)依次进队元素-67、55、99、70;
(7)输出队列q的元素个数;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status
typedef int QElemType
typedef struct
{
QElemType *base
int front
int rear
}SqQueue
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType))
if(!Q.base)
exit(OVERFLOW)
Q.front=Q.rear=0
return OK
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR
Q.base[Q.rear]=e
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE
return OK
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i
if(Q.front==Q.rear)
printf("队为空!\n")
for(i=ei<Q.reari++)
Q.base[i]=Q.base[i+1]
Q.rear--
return Q
}
int main()
{
SqQueue Q,Q1
static int qele=0
int i,j=0,k=0,m=0
static int frontd=Q.front
i=InitQueue(Q)
if(i==1)
printf("队初始化成功!!\n")
else
printf("队初始化失败!!\n")
if(Q.front!=Q.rear)
printf("队不为空!!\n")
else
printf("队为空L!!\n")
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
frontd=Q.front
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q.front++
j++
}
printf("\n")
Q.front=frontd
printf("返敬输入要出队的元素:")
scanf("%d",&j)
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
break
}
Q.front++
}
Q.front=frontd
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front])
Q.front++
}
printf("\n")
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
Q.front=frontd
printf("出队顺序:")
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
}
printf("end\n")
Q.front=0
Q.rear=m
while(Q.rear!=Q.front)
{
free(Q.base)
//Q.base++
Q.front++
if(Q.rear-1==Q.front)
printf("队已经释放!\n")
}
return 0
}
#include<stdio.h>#include<process.h>
#include<iostream.h>
#include<stdlib.h>#define MaxSize 100
typedef int ElemType
typedef struct
{
ElemType data[MaxSize]
int front
int rear
}CircSeqQueue//顺序循环队列的初始化唤瞎旦
void QueueInitial(CircSeqQueue *pQ)
{
//创建一个又指针pQ所指向的空顺序循环队列
pQ->front=pQ->rear=0
}//顺序循环队列判空
int IsEmpty(CircSeqQueue *pQ)
{//顺序循环队列为空时返回1,否则返回0
return pQ->front==pQ->rear
}//顺序循环队列判满
int IsFull(CircSeqQueue *pQ)
{//循环队列为满时返回1,否则返回0
return (pQ->rear+1)%MaxSize==pQ->front
}//元素进队
void EnQueue(CircSeqQueue *pQ, ElemType e)
{//若队列不满,则元素e进队
if(IsFull(pQ))//队列已满,退出
{
printf("队列溢出!\n")
exit(1)
}
pQ->rear=(pQ->rear+1)%MaxSize//队尾指针后移
pQ->data[pQ->rear]=e
}//元素出队
ElemType DeQueue(CircSeqQueue *pQ)
{//若循环队列不为空,则删除队头元素,并返回它的值
if(IsEmpty(pQ))//队列为空,退出
{
printf("空队列!\n")
exit(1)
}
pQ->front=(pQ->front+1)%MaxSize//队头指针后移
return pQ->data[pQ->front]
}//和扰取队头元素
ElemType GetFront(CircSeqQueue *pQ)
{//若队列不为空,则返回队头元素的值
if(IsEmpty(pQ))
{
printf("空队列!\n")
exit(1)
}
return pQ->data[(pQ->front+1)%MaxSize]
}//循环队列置空
void MakeEmpty(CircSeqQueue *pQ)
{//将由指针pQ所指向的队列变为孔队
pQ->front=pQ->rear=0
} void output()
{
int i
for(i=0i<32i++)
printf("+")
printf("\n")
printf("1.元素进队\n")
printf("2.元素出队神宽\n")
printf("3.队首元素读取\n")
printf("4对列制空\n")
printf("5.退出\n")
for(i=0i<32i++)
printf("+")
printf("\n")
} void main()
{
int k,m,n,i
CircSeqQueue *pQ
ElemType e
pQ=new CircSeqQueue
QueueInitial(pQ)
output()
while(k)
{
printf("请选择:1-5\n")
scanf("%d",&m)
switch(m)
{
case 0:return
case 1:
printf("请输入入队元素的个数:\n")
scanf("%d",&n)
printf("输入元素,入队\n")
for(i=0i<ni++)
{
scanf("%d",&e)
EnQueue(pQ,e) } break
case 2:
{
printf("请输入出队元素的个数\n")
scanf("%d",&n)
for(i=0i<ni++)
{
e=DeQueue(pQ)
printf("%d\n",e)
} break
}
case 3:
{
printf("取出队首元素\n")
e=GetFront(pQ)
printf("%d\n",e)
break
}
case 4:
{
printf("队列置空\n")
MakeEmpty(pQ)
printf("\n")
break
}
default:return
}
printf("是否继续(1|0)\n")
scanf("%d",&k)
if(!k) return
}
}看下吧!不是很完善!
#pragma once#include <iostream>
//声明模板,以便在Node中将Queue、operator<<声明为Node的特殊类型的友元
template<class Type>class Queue
template<class Type>std::ostream&operator<<(std::ostream&, const Queue<Type>&)
//节点类
template<class T>
class Node
{
friend class Queue<T>
friend std::ostream&operator<<<T>(std::ostream&, const Queue<T>&)
private:
Node() //声明无参构造函数,不定义这个成员函数,可以禁止任何方式以无参形式创建Node对象
Node(const T &elem): m_element(elem),m_pNext(0){}
private:
T m_element //队列每个元素保存数据的成员变量
Node *m_pNext //后缀指针,指向队当前元素后面的下一个元素
}
//接口类
template<class Type>
class Queue
{
//输出 *** 作脊雹符重载友元声明
friend std::ostream&operator<<<Type>(std::ostream&, const Queue<Type>&)
public:
Queue(): prear(0){}
public:
template<class It>Queue(It beg, It end): prear(0)
{copy_elems(beg, end) }
//复制控制(拷贝构造函数、赋值 *** 作符、析构函数)
Queue(const Queue &q): prear(0)
{copy_elems(q) }
Queue&operator== (const Queue&)
~Queue(){destroy()}
//user Interface
// unchecked operation: front on an empty Queue is undefined
Type&front(){return prear->m_pNext->m_element}
const Type&front(){return prear->m_pNext->m_element}
void push(const Type&)
void pop()
bool empty(){//测试队列是否为空
return prear == 0
}
private:
Node<Type>*prear //对尾指针,这个元素的m_pNext又指向对头,形参循环队列
/utility function
void destroy()
void copy_elems(const Queue&)
private: //成员模板,迭代一个其他类型的序列创建队列
template<class Iter>void copy_elems(Iter, Iter)
}
//成员定义
/********************友元 *** 作符<<重定义********************/
template<class Type>
std::ostream&operator<<(std::ostream &os, const Queue<Type>&q)
{
os <<"<"
Node<Type>*p
for(p = q.prear->m_pNextpp = p->m_pNext)
os <樱余帆<p->m_element <<" "
os <<">"
return os
}
/********************push定义********************/
template<class Type>毁枯
void Queue<Type>::push(const Type &elems)
{
Node<Type>*pt = new Node<Type>(elems)
if(empty()){
prear = pt
pt->m_pNext = prear
}
else{
pt.m_pNext = prear->m_pNext
prear->m_pNext = pt
prear = pt
}
}
/********************pop定义********************/
template<class Type>
void Queue<Type>::pop()
{
if (!empty())
{
Node<Type>*op = prear->m_pNext
prear->m_pNext = op->m_pNext
if (op == prear)
prear = 0
delete op
}
}
/********************destroy定义********************/
template<class Type>
void Queue<Type>::destroy()
{
while(!empty())
pop()
}
/********************copy_elems重载版本之一定义********************/
template<class Type>
void Queue<Type>::copy_elems(const Queue &orig)
{
if (empty())
return
Node<Type>*pt=orig.prear->m_pNext
do
{
push(pt->m_element)
pt = pt->m_pNext
} while ( pt != orig.prear->m_pNext )
}
/********************copy_elems重载版本之二定义********************/
template<class Type>template<class Iter>
void Queue<Type>::copy_elems(Iter beg, Iter end)
{
while(beg != end)
{
push(*beg)
++beg
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)