c语言建立队列函数 c语言队列库函数

如何用C语言建立一个空队列?简单点的话
直接用数组模拟就行了吧
简单写了一下:
/******************************
author : delta
version: 1.0
date: 2010-8-31
*******************************/
#include stdio.h
#include memory.h
#define debug 1
#define MaxN 1024
struct queue {
int q[MaxN];
int front;
int rear;
// initial the queue
void initial() {
front = 0;
rear = 0;
memset(q, 0, sizeof(q));
}
// judge wether the queue is empty or not
bool empty() {
return front == rear;
}
// insert an element
void push(int x) {
if (rear == MaxN) {
printf("The queue is fool !\n");
}
q[rear] = x;
rear;
}
// delete an element
void pop() {
if (empty()) {
printf("There is no element in the queue !\n");
}
front;
}
// look up the first element
int top() {
if (empty()) {
printf("There is no element in the queue !\n");
}
return q[front];
}
};
int main()
{
#if debug
int x;
struct queue q;
q.initial();
q.push(1);
q.push(2);
x = q.top();
printf("q's first elememt is: %d\n", x);
q.pop();
printf("q is %s\n", q.empty()?"empty":"not empty");
q.push(3);
x = q.top();
printf("q's first element is: %d\n", x);
q.initial();
printf("q is %s\n", q.empty()?"empty":"not empty");
#endif
return 0;
}
队列的建立与查询【用C语言编写的完整程序(包括main()函数】我的理解是 你想用数组模拟 队列?不是的话下面不用看,回复我给我再说一下题意,我重新给你写!
首先输入一个操作,1入队,2出队,3退出
如果是1,再输入一个将要入队列的 数据 ,
#include stdio.h
#include stdlib.h
#include string.h
#define LEN 1000
int queue[LEN], fir, end;
void printQueue()
{
int i = 0;
for(i = fir; iend;i)
{
printf("%d ", queue[i]);
}
printf("\n");
}
void insertQueue()
{
int value = https://www.04ip.com/post/0, i = 0;
printf("Enter the data which you want to insert to queue...\n");
scanf("%d", value);
queue[end] = value;
printQueue();
}
void deleteQueue()
{
printf("after delete the top data!\n");
fir;
printQueue();
}
void demo()
{
int Number = 0;
while(1)
{
printf("Enter the number:\n");
printf("1.insert...\n");
printf("2.delete...\n");
printf("3.exit!\n");
scanf("%d", Number);
if(Number1 || Number4) return;
switch(Number)
{
case 1: insertQueue(); break;
case 2: deleteQueue(); break;
case 3: exit(0);
default: return;
}
}
}
void creatQueue()
{
int i = 0;
fir = 0, end = 0;
for(i = 0; iLEN;i)
{
queue[i] = 0;
}
}
int main()
{
creatQueue();
demo();
return 0;
}
c语言 队列问题//改正如下 , 但是我觉得你那个出队有点问题,不应当是自己输入要出队的元素吧 , 是让他自己出队吧 , 然后把这个出队的元素值带回来,再在主函数中输出吧,没给你改,嘿嘿嘿嘿
#include "stdio.h"
#include "stdlib.h"
#define MAXSIZE 5
typedef struct k
{
int data[MAXSIZE];
int front,rear;
int num;
}c_k;//声明循环顺序队列的数据类型
//创建循环队列
c_k *create_k()
{
c_k * cq=(c_k *)malloc(sizeof(c_k));//开辟循环队列的空间,并将地址存到变量cq中
cq-front=-1;
cq-rear=-1;
cq-num=0;//初始化为空队列
return cq;//返回队列的地址
}
intIn_ck(c_k * cq,int *x)//-----在传的时候是个地址,所以这要用int *x-------///如果要用int x的话,在调用该函数时就要写成In_ck(cq,rear_x)--//
{
if(cq-num==MAXSIZE)//队列已满
{
printf("The c_k is full!\n");return -1;
}
else//队列不满
{
cq-rear=(cq-rear 1)%MAXSIZE;//形成循环
cq-data[cq-rear]=*x;/////------传的是地址,所以这要用*x----/////
cq-num;//队列中元素个数增1
return 1;
}
}
//出队
int Out_ck(c_k * cq, int *x)//对cq所指向的队列出队,将队首元素存到x所指向的变量,并返回是否出队成功
{
if(cq-num==0)//队列为空
{
printf("The k is null!\n");return -1;
}
else//队列非空
{
cq-front=(cq-front 1)%MAXSIZE;//形成循环
*x=cq-data[cq-front];//将队首元素存到x所指向的变量
cq-num--;//队列中元素个数减1
return 1;
}
}
void main()
{
//调用创建队列函数
c_k * cq=create_k();
int rear_x,front_x;
int op,result;
printf("\n请输入 1入队,2出队,3退出:\n");
scanf("%d",op);
while(op!=3)
{
switch(op)
{
case 1:printf("请入入队元素:\n");
scanf("%d",rear_x);////----这要加号,!!!!!
if((result=In_ck(cq,rear_x))==1)////----加了一个if语句 , 在队空时就不输出元素了-----////
printf("入对元素是 %d\n",rear_x);
break;
case 2:printf("请输入出队元素:\n");
scanf("%d",front_x);////----这要加号,!!!!!
if ((result= Out_ck(cq,front_x))==1)////----加了一个if语句,在队空时就不输出元素了-----////
printf("出对元素是 %d\n",front_x);
break;
}
printf("\n请输入 1入队 2出队 3退出:\n");scanf("%d",op);
}
free(cq);/////---动态开辟的 , 用后要用free()释放;---////
}
C语言,用数组实现队列的入队,出队函数编程这样的话应该符合你的要求:
#includestdio.h
void add(int queue[],int x);
int Top(int queue[]);
void del(int queue[]);
int end=0;
int main()
{
int n;
scanf("%d",n);//将要入队列n个元素
int queue[1000];
for(int i=1;i=n;i)//输入n个元素
{
add(queue,i);//将i加入队列
}
//验证加入队列的元素 , 将队列中的元素按照输入的顺序输出:
for( i=1;i=n;i)
{
printf("%d ",Top(queue));//Top函数返回队头元素
del(queue);//删除队头元素
}
//验证输出已经出队列后的队列(数组)元素:
printf("\n");
for(i=1;i=n;i)
printf("%d ",queue[i]);
printf("\n");
return 0;
}
void add(int queue[],int x)
{
queue[end]=x;
}
int Top(int queue[])
{
return queue[1];//注意 , 这里的函数始终return queue[1];这里是和将普通数组中的元素输出最大的不同之处 。?。。。。。?
}
void del(int queue[])
{
for(int i=2;i=end;i)
{
queue[i-1]=queue[i];
}
queuec语言建立队列函数的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于c语言队列库函数、c语言建立队列函数的信息别忘了在本站进行查找喔 。
=0;//将删除后的地方置0
end--;
}
【c语言建立队列函数 c语言队列库函数】c语言建立队列函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言队列库函数、c语言建立队列函数的信息别忘了在本站进行查找喔 。

    推荐阅读