当前位置:首页 » 编程语言 » c语言队列代码

c语言队列代码

发布时间: 2025-08-10 21:38:33

c语言写 栈队列(先进先出的)

#include<stdio.h>
#include<malloc.h>typedef
struct
node
/*定义新类型结构体结点*/
{
int
data;/*数据成员可以是多个不同类型的数据*/
struct
node
*next;/*指针变量成员只能是一个*/
}NODE;/*节点结束*/typedef
struct
qnode
/*定义结点类型的变量名*/
{
NODE
*front;/*设定结点头指针变量*/
NODE
*rear;/*
设定结点尾指针变量*/
}QNODE;
/*
链队列的结点定义
*/
void
InitQueue(QNODE
*Q)/*定义指针Q*/
{
NODE
*p;/*定义指针p*/
p=(NODE
*)malloc(sizeof(NODE));/*分配结点字节的容量*/
Q->front=p;
/*指定头指针p*/
Q->front->next=NULL;/*建立空队列*/
Q->rear=Q->front;/*改变Q的值*/
printf("The
init
is
complete!");}
void
*QueuePush(QNODE
*Q)
{
NODE
*p;
p=(NODE
*)malloc(sizeof(NODE));/*分配结点字节的容量*/
printf("Please
input
a
number
:");/*请输入一个数*/
scanf("%d",&p->data);/*给第一个结点赋值*/
p->next=NULL;/*指定尾结点*/
Q->rear->next=p;/*指定尾新结点p的地址*/
Q->rear=p;/*指定队尾结束*/
printf("The
%d
has
been
pushed
into
the
Queue!",p->data);/*显示数据成员*/
return
0;/*程序结束*/
}
void
*QueuePop(QNODE
*Q)
{
NODE
*p;/*定义结点指针*/
if(Q->front->next==NULL)
return
0;/*判断对前是否为空,如果是就结束*/
p=Q->front->next;/*指向下以个成员*/
Q->front->next=p->next;/*依次向下循环*/
if(Q->rear==p)
Q->rear=Q->front;/*队尾与对头相同*/
printf("The
%d
has
been
pop
from
the
queue!
\n",p->data);/*显示队列成员*/
free(p);
return
0;
}
void
*PrintQueue(QNODE
*Q)
{
NODE
*p;/*定义链结点*/
p=Q->front->next;/*指定对头*/
while(p!=NULL)/*如不为空*/
{
printf("%5d",p->data);/*显示数据成员*/
p=p->next;/*指定第二个成员*/
}
return
0;
}
void
main()
{
QNODE
*T;
int
i=0;/*取值*/
printf("1.InitQueue
2.QueuePush
3.QueuePop
4.PrintQueue
5.Quit
\n");
while(i!=5)
{
printf("Please
choose
the
gongneng:");
scanf("%d",&i);
printf("\n");
switch(i)
{
case
1:
InitQueue(T);
printf("\n");
break;
case
2:
QueuePush(T);
printf("\n");
break;
case
3:
QueuePop(T);
printf("\n");
break;
case
4:
printf("The
queue's
numbers
are:");
PrintQueue(T);
printf("\n");
break;
case
5:
printf("\n");
break;}
}
}

㈡ C语言中使用队列

如果你用vc,#include<deque>就好了,但是注意要加上using naemspace std;
我是当你用的c++的STL,STL中没有真正的队列和栈,他们都是通过对双端队列的改造得到的,所以包含的文件可能和你想的不一样。而且这些头文件都没有.h结尾!很特别
如果你不是vc,当我没说

㈢ 数据结构C语言描述的链队列的基本操作(初始化,判空,入队,出队,取对头,输出队列所有值....)

void InitQueue(LiQueue *&q)
{q=(LiQueue *)malloc(sizeof(LiQueue));
q->front=q->rear-NULL;} //初始化

int QueueEmpty(LiQueue *q)
{if(q->rear==NULL)
return 1;
else
return 0;} //判空

void enQueue( LiQueue *&q,ElemType e)
{QNode *s;
s=(QNode *)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
if(q->rear==NULL)
q->front=q->rear=s;
else
{q->大局毁rear->next=s;
q->rear=s;
}} //入队

int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;
free(t);
return 1;} /腊伍/出队

int deQueue( LiQueue *&q,ElemType &e)
{QNode *t;
if(q->rear==NULL)
return 0;
t=q->front;
if(q->front==q->rear)
q->front=q->rear=NULL;
else
q->front=q->front->next;
e=t->data;break;
free(t);
return 1;} //取队头

输出滚备队列所有数就是出队

㈣ C语言中循环队列的队满和队空的判断条件各是什么有什么不同

队空时: Q.front == Q.rear;

队满时: Q.front == (Q.rear + 1) % MAXSIZE;

front指向队首元素,rear指向队尾元素的下一个元素。

maxsize是队列长度。

(4)c语言队列代码扩展阅读:

实现的代码:

#include <stdio.h>

#include <malloc.h>

#define MAXSIZE 100 //最大队列长度

#define OK 1

#define ERROR 0

typedef int ElemType;

typedef int Status;

typedef struct {

ElemType *base; //队列空间

int front; //队头指针

int rear; //队尾指针,若队尾不为空,则指向队尾元素的下一个位置

}SqQueue;

//初始化循环队列

Status initQueue(SqQueue &Q) {

Q.base = (ElemType *) malloc(MAXSIZE * sizeof(ElemType)); //申请空间

Q.front = Q.rear = 0; //队空

return OK;

}

//入队

Status enQueue(SqQueue &Q, ElemType e) {

if ((Q.rear + 1) % MAXSIZE == Q.front) return ERROR; //队满,无法添加

Q.base[Q.rear] = e; //插入元素

Q.rear = (Q.rear + 1) % MAXSIZE; //队尾指针+1

return OK;

}

//出队

Status deQueue(SqQueue &Q, ElemType &e) {

if (Q.front == Q.rear) return ERROR; //队空,无法删除

e = Q.base[Q.front]

Q.front = (Q.front + 1) % MAXSIZE; //队头指针+1

return OK;

}

//返回队列长度

Status length(SqQueue &Q) {

return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;

}

㈤ C语言的队列如何实现和表示

我能想到的有两种方法(假设队列元素都是int)
一,用链表的方法
struct A
{
int n;
struct A *a;
} *p,*head,*rear;
head=rear=NULL;/*头指针,尾指针*/
添加元素:p=(struct A*)malloc(sizeof(struct A));......给新元素赋值.....;rear->a=p;rear=p;
当然添加第一个元素的时候要给head赋值。
删除元素:p=head;head=head->a;free(p);
用的是单向链表,当然也可以用双向链表,不过删除,添加元素的过程要麻烦点。
二,利用数组,当然也可以开辟动态储存空间
int a[N],*head,*rear; N就是个常数
head=rear=a;
添加元素:scanf("%d",rear-a==N?rear=a:++rear);
删除元素:head-a==N?head=a:head++;
当然要检查队列是否溢出,可以设变量n=0;
每次添加元素n++
每次删除元素n--
当n<0后n>N数据溢出

热点内容
acfun如何上传 发布:2025-08-13 07:35:10 浏览:270
ftp共享服务器需要什么配置 发布:2025-08-13 07:33:00 浏览:543
主要数据库 发布:2025-08-13 07:15:27 浏览:178
压缩包漫画 发布:2025-08-13 07:15:25 浏览:131
服务器空岛原版如何获得矿物 发布:2025-08-13 07:08:22 浏览:437
购车时哪些是必备的配置 发布:2025-08-13 06:42:33 浏览:693
宝塔添加脚本 发布:2025-08-13 06:41:56 浏览:502
ios数据库存储 发布:2025-08-13 06:28:10 浏览:975
java学习资源 发布:2025-08-13 06:07:56 浏览:395
传奇地下宫殿脚本 发布:2025-08-13 06:07:21 浏览:956