C语言循环队列出队主函数 循环队列的主函数怎么写-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

C语言循环队列出队主函数 循环队列的主函数怎么写

编程:循环队列的入队、出队(要有main函数)

#includeiostream

创新互联2013年开创至今,先为平定等服务建站,平定等地企业,进行企业商务咨询服务。为平定企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

using namespace std;

template typename T

struct Node

{

T data;

struct Node *next;

};

template typename DATA

class cycle_queue

{

public:

NodeDATA *head;//队列头,不保存数据

NodeDATA *end;//队列尾,不保存数据

unsigned int size;

cycle_queue()

{

head=new NodeDATA();

end=new NodeDATA();

size=0;

}

void push_back(DATA d)//入队列

{

NodeDATA *tmp;

tmp=new NodeDATA;

tmp-data=d;

if(size==0)

{

head-next=tmp;

end-next=tmp;

tmp-next=tmp;

}

else

{

end-next-next=tmp;

end-next=tmp;

tmp-next=head-next;

}

++size;

}

DATA front()//取队头元素,不负责检查是否为空

{

DATA re;

if(size!=0)

re=head-next-data;

return re;

}

void pop()//队头元素出列,不负责检查是否为空

{

if(size!=0)

{

NodeDATA *tmp;

tmp=head-next;

head-next=head-next-next;

end-next-next=head-next;

delete tmp;

--size;

}

}

bool empty()//队列判空

{return size==0;}

};

int main()

{

int a[10]={10,9,8,7,6,5,4,3,2,1};

class cycle_queueint cq;

short i;

for(i=0;i!=10;++i)

cq.push_back(a[i]);

cout"**************"endl;

while(!cq.empty())

{

coutcq.front()" ";

cq.pop();

}

cini;

return 0;

}

C语言用数组实现循环队列的入队出队

//定义一个int型数组que,长度为N(常量切大于2).

int que[N];

int rear=0,front=0; //队尾 队头

判断队列已满:

if((front+1)%N==rear%N)  //成立则队列已满

判断队列为空

if((rear==front)) //成立则队列空

入队(一般在入队前判断队列是否已满)

//将val入队

que[front++]=val;

front%=N;

出队(一般在出队前判断队列是否为空)

rear=(rear+1)%N;

下一个要出队的元素(一般先判断是否为空)

que[rear];

c语言 队列的操作

//定义队列结构体

typedef struct Qnode

{

int data;

struct Qnode *next;

} Queue , *QueuePtr;

typedef struct

{

QueuePtr front;

QueuePtr rear;

} linkQnode;

//创建一个队列

initQueue (linkQnode *q)

{

q - front = q - rear = (QueuePtr) malloc (sizeof (Queue));

if (!q - front) exit (0);

q - front - next = NULL;

}

//入队列

EnterQueue (linkQnode *q , int item)

{

QueuePtr p;

p = (QueuePtr) malloc (sizeof (Queue));

if (!p) exit (0);

p - data = item;

p - next = NULL;

q - rear - next = p;

q - rear = p;

}

//出队列

DelQueue (linkQnode *q , int *item)

{

QueuePtr p;

if (q - front = q - rear) return;

p = q - front - next;

*item = p - data;

q - front - next = p - next;

if (q - rear == p)

q - rear = q - front;

free (p);

}


分享题目:C语言循环队列出队主函数 循环队列的主函数怎么写
当前URL:http://kswsj.cn/article/dophgpg.html

其他资讯