您的位置:首页 > 其它

抽象数据类型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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: