
pq->rear->next
=
pnew这个代码从队列的尾部增加新节点,
然后pq->rear
=
pnew更新队列尾部指针。队列的数据结构形式就是由一个头front指针,一个尾rear指针来表征,items的设计是用空间换时间,涉及队列大小的 *** 作会非常方便。
队列的特征是先进先出,你给出的链式实现,其实就跟一个链表一样,链表的添加删除如果能理解了,队列只是链表的元素增加/删除
按先进先出特点的一种实现。
但对于队列来说,实现方式不是重点,先进先出的性质才是重点,这在实际应用中很多,比如排队叫号。
// Queuecpp : Defines the entry point for the console application
//
#include "stdafxh"
#include <stdioh>
#include <stdlibh>
#define MAXQSIZE 10
typedef int QElemType;
typedef struct{
QElemType base;
int front;
int rear;
}SqQueue;//你把这个struct分开就是你想要的了,我这里没有环境测试不了,你自己去测试吧
int InitQueue(SqQueue &Q){
Qbase = (QElemType )malloc(MAXQSIZE sizeof(SqQueue));
if(!Qbase){
printf("overflow\n");
return 0;
}
Qfront = Qrear = 0;
return 1;
}
int EnQueue(SqQueue &Q, QElemType e){
if((Qrear + 1) % MAXQSIZE == Qfront){
printf("queue is full\n");
return 0;
}
Qbase[Qrear] = e;
Qrear = (Qrear + 1) % MAXQSIZE;
return 1;
}
int DeQueue(SqQueue &Q, QElemType &e){
if(Qrear == Qfront){
printf("queue is empty\n");
return 0;
}
e = Qbase[Qfront];
Qfront = (Qfront + 1) % MAXQSIZE;
return 1;
}
int GetHead(SqQueue &Q, QElemType &e){
if(Qrear == Qfront){
printf("queue is empty\n");
return 0;
}
e = Qbase[Qfront];
return 1;
}
int QueueLength(SqQueue &Q){
return ((Qrear + MAXQSIZE) - Qfront) % MAXQSIZE;
}
bool QueueEmpty(SqQueue &Q){
return (Qrear == Qfront);
}
void QueueTraverse(SqQueue &Q){
int i = Qfront;
while(i != Qrear)
{
printf("%d: %d \n", i, Qbase[i]);
i = (i + 1) % MAXQSIZE;
}
}
int main(int argc, char argv[])
{
SqQueue Q;
InitQueue(Q);
for(int i = 0; i < 7; i++)
{
EnQueue(Q, i);
}
QueueTraverse(Q);
int e;
for( i = 0; i < 5; i++)
{
DeQueue(Q, e);
printf("%d ", e);
}
printf("\n");
for( i = 7; i < 15; i++)
{
EnQueue(Q, i);
}
QueueTraverse(Q);
printf("length = %d\n", QueueLength(Q) );
return 0;
}
# include "stdioh"
# include "malloch"
# include "stdlibh"
typedef struct Queue
{
int data;
int Priority;
Queue Next;
} PQUEUE;
bool insert(PQUEUE p,int i, int j);
bool pop(PQUEUE p);
void sort(PQUEUE p);
int length = 0;
PQUEUE pT;
int main(void)
{
PQUEUE pH = (PQUEUE)malloc(sizeof(Queue));
insert(pH, 75, 8);
insert(pH, 54, 4);
insert(pH, 75, 6);
insert(pH, 23, 5);
insert(pH, 81, 4);
insert(pH, 65, 3);
insert(pH, 43, 4);
insert(pH, 34, 2);
sort(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
return 0;
}
bool insert(PQUEUE p,int i, int j)
{
if(i>= 0 && i<= 100 && j>=0 && j<=100)
{
PQUEUE pNew = (PQUEUE)malloc(sizeof(Queue));
if(length == 0)
{
pT = NULL;
}
if(pT == NULL)
{
pT = p;
}
if(NULL == pNew)
{
printf("动态内存分配失败~!");
exit(-1);
}
pNew->data = i;
pNew->Priority = j;
pT->Next = pNew;
pNew->Next = NULL;
pT = pNew;
length++;
return true;
}
return false;
}
PQUEUE p2;
bool pop(PQUEUE p)
{
if(length != 0)
{
p2 = p;
p = p->Next;
printf("%d,", p->data);
printf("%d\n", p->Priority);
p2->Next = p->Next;
length--;
return true;
}
return false;
}
void sort(PQUEUE p)
{
if(length != 0)
{
PQUEUE w,q;
int i, j, t1,t2;
for(i=0,w=p->Next; i < length-1; ++i,w = w->Next)
{
for(j=i+1,q=w->Next; j < length; ++j,q = q->Next)
{
if(w->Priority < q->Priority)
{
t1 = w->data;
w->data = q->data;
q->data = t1;
t2 = w->Priority;
w->Priority = q->Priority;
q->Priority = t2;
}
}
}
}
return;
}
/
都满足你的要求了,以上是使用链表结构的队列
/
以上就是关于c语言队列 *** 作全部的内容,包括:c语言队列 *** 作、请大神帮我编一个c语言版的链队列、C语言实现一个优先队列等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)