用链表做一个FIFO队列,如何实现????

tomsx 2003-11-11 08:53:54
如提
...全文
355 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuantao 2003-11-12
  • 打赏
  • 举报
回复
不要这么麻烦把,你可以用stl里面的list来实现queue
只要对list做一些限制即可
ZhangYv 2003-11-11
  • 打赏
  • 举报
回复
以下是一个图的广度优先周游算法C语言实现,数据结构图章节一般都会有算法介绍,数据结构使用到队列和邻接表.

#include <stdio.h>
#include <malloc.h>
enum boolean {false, true};
const int MaxSize = 20;
struct celltype{
int id;
celltype *next;
};
struct queue{
celltype *front, *rear;
};
struct EdgeNode{
int adjvex;
EdgeNode *next;
};
struct vexNode{
int info;
EdgeNode *next;
};
vexNode Graph[MaxSize];
int N;

void MakeNull(queue *head)
{

head->front->next = NULL;
head->rear = head->front;
}
void CreateQueue(queue *head)
{
head->front = (celltype*) malloc (sizeof(celltype));
head->rear = (celltype*) malloc (sizeof(celltype));
MakeNull(head);
}
boolean IsEmpty(queue *head)
{
if (head->front == head->rear)
return true;
else
return false;
}
void Dequeue(queue *head)
{
celltype *t;
if (!IsEmpty(head)){
t = head->front;
head->front = head->front->next;
free(t);
} else
printf("%s", "DeleteError!");
}
void Enqueue(queue *head, int id)
{
head->rear->next = (celltype*) malloc (sizeof(celltype));
head->rear = head->rear->next;
head->rear->id = id;
}

void CreateGraph()
{
int i,k;
EdgeNode *p;
printf("%s", "Please Input MaxNode = ");
scanf("%d", &N);
for (i = 0; i < N; i++){
Graph[i].info = i;
printf("%s %d\n", "Input Node", i,"'s adjvexNode = ,input -1 for end");
scanf("%d", &k);
if (k >= 0){
Graph[i].next = (EdgeNode*) malloc (sizeof(EdgeNode));
p = Graph[i].next;
p->adjvex = k;
p->next = NULL;
}
while (k >= 0){
scanf("%d", &k);
if (k >= 0){
p->next = (EdgeNode*) malloc (sizeof(EdgeNode));
p = p->next;
p->adjvex = k;
}
}
p->next = NULL;
}
}
void Print()
{
int i;
EdgeNode *p;
for (i = 0; i < N; i++){
printf("%s %d %s ", "Node ", i, "'s adjvex = ");
for (p = Graph[i].next; p; p = p->next)
printf("%d ", p->adjvex);
printf("\n");
}
}
void BFS()
{
void Search(int, boolean *);
boolean visited[MaxSize];
int i;
for (i = 0; i < N; i++)
visited[i] = false;
for (i = 0; i < N; i++)
if (!visited[i])
Search(0, visited);
}
void Search(int i, boolean *visited)
{
queue *Q;
celltype *p;
EdgeNode *adj;
CreateQueue(Q);
Enqueue(Q, i);
visited[i] = true;
printf("%d ", i);
while (!IsEmpty(Q)){
p = Q->front->next;
adj = Graph[p->id].next;
while (adj){
if (!visited[adj->adjvex]){
Enqueue(Q, adj->adjvex);
printf("%d ", adj->adjvex);
visited[adj->adjvex] = true;
}
adj = adj->next;
}
Dequeue(Q);
}
}
void main()
{
CreateGraph();
BFS();
}

viscky 2003-11-11
  • 打赏
  • 举报
回复
typedef struct Slnode{
datatype data;
struct Slnode *link;
}*Qlink;
struct Qnode{
Qlink front;
Qlink rear;
};
Qnode Q;
void Init_Qnode(Qnode Q){
new(Q.front);
Q.front->link=NULL;
Q.rear=Q.front;
}
tibetan 2003-11-11
  • 打赏
  • 举报
回复
看你都两个角了。呵呵,还这样问问题。你可以写出来,看看什么地方有错误。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧