抽象数据类型ADT之队列的构建
2017-10-21 09:06
204 查看
注意搞清楚一个问题,那就是队列的操作中:
入队是从最后面入,
出队是从最前面出。
然而
栈操作的入队出队都是从最后面(最上面)出。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef int elemtype ;
typedef int status;
typedef struct node
{
elemtype data;
struct node *next;
}node ,*queuenode; //构建一个链表类型的结构体数组
typedef struct
{
queuenode front;
queuenode rear;
}linkqueue;
// queue
void initqueue(linkqueue &q)
{
q.front = q.rear = new node;
q.front ->next =NULL;
}
void push(linkqueue &q, elemtype e)//从队列的最后一个元素push
{
queuenode p;
p = new node;
p->data = e;
p->next= NULL;
q.rear->next = p;
q.rear = p;
}
void pop(linkqueue &q, elemtype &e)// pop的时候,从队首的第一个元素
{
if(q.front == q.rear) return;
else
{
queuenode p;
p=q.front->next;
e = p->data;
q.front->next = p->next;
if(q.rear == p) // 注意,特殊情况队中只有一个元素的时候,删除该元素之后操作注意事项
{
q.rear = q.front;
}
free(p);
}
}
int isempty(linkqueue &q)
{
if(q.front == q.rear) return 1;
else return 0;
}
int main ()
{
return 0;
}
入队是从最后面入,
出队是从最前面出。
然而
栈操作的入队出队都是从最后面(最上面)出。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef int elemtype ;
typedef int status;
typedef struct node
{
elemtype data;
struct node *next;
}node ,*queuenode; //构建一个链表类型的结构体数组
typedef struct
{
queuenode front;
queuenode rear;
}linkqueue;
// queue
void initqueue(linkqueue &q)
{
q.front = q.rear = new node;
q.front ->next =NULL;
}
void push(linkqueue &q, elemtype e)//从队列的最后一个元素push
{
queuenode p;
p = new node;
p->data = e;
p->next= NULL;
q.rear->next = p;
q.rear = p;
}
void pop(linkqueue &q, elemtype &e)// pop的时候,从队首的第一个元素
{
if(q.front == q.rear) return;
else
{
queuenode p;
p=q.front->next;
e = p->data;
q.front->next = p->next;
if(q.rear == p) // 注意,特殊情况队中只有一个元素的时候,删除该元素之后操作注意事项
{
q.rear = q.front;
}
free(p);
}
}
int isempty(linkqueue &q)
{
if(q.front == q.rear) return 1;
else return 0;
}
int main ()
{
return 0;
}
相关文章推荐
- 队列的抽象数据类型ADT
- 数据结构(scheme) -- 抽象数据类型(ADT) -- 4000 队列
- 抽象数据类型总结:复数 adt 和 FIFO 队列adt
- 抽象数据类型ADT之栈的构建
- 队列的抽象数据类型(abstract data type,ADT)
- 构建栈和队列
- C Primer Plus学习 六十二 队列 ADT(二)
- 3.1--3.3.2抽象数据类型ADT、单链表初始化删除插入、Collection接口、Iterator接口的简单介绍
- 队列ADT
- ActiveMQ+STOMP协议构建消息队列
- RabbitMQ系列二(构建消息队列机制)
- python数据结构与算法 13 队列的抽象数据类型
- 抽象数据类型-线性结构-先进先出:队列
- 队列ADT
- ADT(抽象数据类型)的理解
- 构建XHR连接队列
- 以循环顺序表实现的队列ADT(C语言版)
- 「C语言精髓_高级数据表示」抽象数据类型(ADT)
- 自己动手构建无锁的并发容器(栈和队列)
- 第七话:抽象数据类型ADT