dequeue函数C语言 queue c语言
一道C语言数据结构队列代码 调用出队子函数dequeue时不知哪里出错,运行无输出。
bool dequeue(sqqueue *q,char e)
成都创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站开发、程序开发、网站优化、微网站、微信小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站设计、网站建设、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。
{
if(queueempty(q))
return false;
q-front++;
e=q-date[q-front];
return true;
}
一个很简单的小问题,dequeue的参数e 应该是引用哦
求一个广度优先算法的实例及其C语言程序(L-dequeue)
#include stdio.h
#define max 100
typedef struct anode
{
int adjvex; //边的终点位置
struct anode *nextarc;
}arcnode;
typedef struct node
{
int data;
arcnode *firstout;
}vnode;
typedef struct
{
vnode adjlist[max];
int n;
int e;
}Agraph;
static int visit[max];
//深度遍历
void DFS(Agraph G,int v) //v为初始顶点编号
{
int k;
arcnode *p;
for(k=0;kG.n;k++)
visit[k]=0;
printf("%d ",v);
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p-adjvex])
DFS(G,p-adjvex);
p=p-nextarc;
}
}
void BFS(Agraph G,int v)
{
arcnode *p;
int q[max];
int front=0;
int rear=0;
int w,i;
for(i=0;iG.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
while(front!=rear)
{
front=(front+1)%max;
w=q[front];
p=G.adjlist[w].firstout;
while(p)
{
if(!visit[p-adjvex])
{
printf("%d ",p-adjvex);
visit[p-adjvex]=1;
rear=(rear+1)%max;
q[rear]=p-adjvex;
}
p=p-nextarc;
}
printf("\n");
}
}
//层序遍历二叉树
struct btnode
{
int data;
btnode *lchild,*rchild;
};
void level(struct btnode *bt)
{
if(!bt)
return;
btnode *q[max];
int front,rear;
front=0;
rear=0;
printf("%d ",bt-data);
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt-lchild)
{
printf("%d ",bt-lchild-data);
rear=(rear+1)%max;
q[rear]=bt-lchild;
}
if(bt-rchild)
{
printf("%d ",bt-rchild-data);
rear=(rear+1)%max;
q[rear]=bt-rchild;
}
}
}
void DFS1(Agraph G,int v)
{
arcnode *p;
printf("%d ",v);
visit[v]=1;
p=G.adjlist[v].firstout;
while(p)
{
if(!visit[p-adjvex])
{
DFS1(G,p-adjvex);
}
p=p-nextarc;
}
}
void level1(struct btnode *bt)
{
if(!bt)
return;
printf("%d ",bt-data);
struct btnode *q[max];
int front=0;
int rear=0;
rear=(rear+1)%max;
q[rear]=bt;
while(front!=rear)
{
front=(front+1)%max;
bt=q[front];
if(bt-lchild)
{
printf("%d ",bt-lchild-data);
rear=(rear+1)%max;
q[rear]=bt-lchild;
}
if(bt-rchild)
{
printf("%d ",bt-rchild-data);
rear=(rear+1)%max;
q[rear]=bt-rchild;
}
}
}
void BFS1(Agraph G,int v)
{
int q[max];
int front=0;
int rear=0;
int i;
for(i=0;iG.n;i++)
visit[i]=0;
printf("%d ",v);
visit[v]=1;
rear=(rear+1)%max;
q[rear]=v;
arcnode *p;
while(front!=rear)
{
front=(front+1)%max;
i=q[front];
p=G.adjlist[i].firstout;
while(p)
{
if(!visit[p-adjvex])
{
printf("%d ",p-adjvex);
visit[p-adjvex]=1;
rear=(rear+1)%max;
q[rear]=p-adjvex;
}
p=p-nextarc;
}
}
}
C语言队列的插入与删除
#include stdio.h
#include stdlib.h
#define MAXQSIZE 100 //最大队列长度
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct
{
int *base;
int front;
int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
void InitQueue(SqQueue *Q)
{
Q-front = Q-rear = 0;
if (Q-base == NULL) {
Q-base = (int*)malloc(sizeof(int)* MAXQSIZE);
}
}
void DesQueue(SqQueue *Q) {
free(Q-base);
Q-base = NULL;
Q-front = Q-rear = 0;
}
int QueueLength(SqQueue *Q)
{
if (Q-base == NULL) return ERROR;
return (Q-rear - Q-front + MAXQSIZE) % MAXQSIZE;
}
void display(SqQueue *Q)
{
int i;
if (Q-base == NULL) {
printf("\n ERROR ");
return;
}
for (i = Q-front; i != Q-rear; i++) {
i = i % MAXQSIZE;
printf("%3d", Q-base[i]);
}
printf("\n");
}
int InQueue(SqQueue *Q, int e)
{
if (Q-base == NULL) return ERROR;
if ((Q-rear + 1) % MAXQSIZE == Q-front)
return OVERFLOW;
Q-base[Q-rear] = e;
Q-rear = (Q-rear + 1) % MAXQSIZE;
return OK;
}
int DeQueue(SqQueue *Q, int m)
{
int i = 0;
if (Q-base == NULL) return ERROR;
if (Q-front == Q-rear)
return ERROR;
while (i != m Q-front != Q-rear)
{
printf("\n%dDeleted\n", Q-base[Q-front]);
Q-front = (Q-front + 1) % MAXQSIZE;
i++;
}
if (i != m) {
printf("\n ERROR ");
return ERROR;
}
return OK;
}
void main()
{
int m, n, d, i;
SqQueue Q = { 0, 0, 0 };
InitQueue(Q);
printf("请输入要插入的元素个数:");
scanf("%d", m);
printf("要插入的元素:");
for (i = 1; i = m; i++)
{
scanf("%d", n);
InQueue(Q, n);
}
printf("插入元素后,队列中的元素为:");
display(Q);
printf("队列长度为:");
printf("%d\n", QueueLength(Q));
printf("输入要删除的元素个数:");
scanf("%d", d);
DeQueue(Q, d);
printf("\n删除元素后,队列中元素为:");
display(Q);
printf("\n");
DesQueue(Q);
}
求大家帮忙 关于C语言里的queue
这是链表的,部分是c++的输入输出,改一下cout,cin换成printf,scanf就行了
#includestdio.h
#includeiostream
#define MAX 30
using namespace std;
typedef struct pqueue
{
char str[MAX];
int priority;
pqueue* next;
}pqueue;
int enqueuepriority(pqueue *pq, char str[MAX], int priority)
{
if(priority0)
{
pqueue *p=(pqueue*)malloc(sizeof(pqueue));
for(int i=0;iMAX;i++)
p-str[i]=str[i];
p-priority=priority;
p-next=pq-next;
pq-next=p;
return 1;
}
else return 0;
}
char* dequeue(pqueue *pq)
{
pqueue *p;
if(pq-next!=NULL)
{
p=pq-next;
pq-next=pq-next-next;
return p-str;
}
else return "";
}
int main()
{
cout"(1) Enqueue (single)"endl;
cout"(2) Enqueue (multiple)"endl;
cout"(3) Dequeue (single)"endl;
cout"(4) Dequeue (all)"endl;
cout"(5) Quit"endl;
int re=0,t=1;
static char* str=new char[MAX];
int priority=0;
pqueue *pq=(pqueue*)malloc(sizeof(pqueue));
pq-next=NULL;
while(re!=5)
{
cout"Choose an action:";
cinre;
switch(re)
{
case 1:
cout"Enter a name to save and its priority"endl;
scanf("%s%d",str,priority);
if(enqueuepriority(pq,str,priority))
break;
else
{
cout"INPUT ERROR!!"endl;
break;
}
case 2:
cout"Enter names to save and their priority. Enter “done” to quit"endl;
do
{
scanf("%s",str);
if(strcmp(str,"done")!=0)
{
scanf("%d",priority);
enqueuepriority(pq,str,priority);
}
else t=0;
}while(t==1);
break;
case 3:
str=dequeue(pq);
if(str=="")
cout"NULL"endl;
else
printf("%s\n",str);
break;
case 4:
while(pq-next!=NULL)
{
printf("%s\n",dequeue(pq));
}
break;
case 5: break;
default:
cout"INPUT ERROR!!"endl;
break;
}
}
delete [] str;
return 0;
}
网页标题:dequeue函数C语言 queue c语言
当前路径:http://azwzsj.com/article/doijioe.html