#include<iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define VARTYPE char //默认存放char类型
using namespace std;
struct myNode;
struct myQueue;
typedef struct myNode Node;
typedef Node* List;
typedef Node* PtrToNode;
typedef myQueue _Queue;
struct myNode
{
VARTYPE data;
PtrToNode next;
};
struct myQueue
{
PtrToNode _rear;
PtrToNode _front;
};
void _push(_Queue* Q,VARTYPE x)
{
PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
tmp->data = x;
tmp->next = NULL;
if(Q->_front == NULL)
Q->_front = tmp;
else
Q->_rear->next = tmp;
Q->_rear = tmp;
}
void _pop(_Queue *Q)
{
PtrToNode pnode = Q->_front;
if(pnode!=NULL&&pnode->next!=NULL)
{
Q->_front = pnode->next;
free(pnode);
}
else if(pnode!=NULL&&pnode->next==NULL)
{
Q->_front = NULL;
Q->_rear = NULL;
free(pnode);
}
}
int _isempty(_Queue *Q)
{
return Q->_front == NULL;
}
void _delstack(_Queue* &Q)
{
PtrToNode tmp = Q->_front;
while(tmp!=NULL)
{
Q->_front = Q->_front->next;
free(tmp);
tmp = Q->_front;
}
Q->_rear = NULL;
Q = NULL;
}
VARTYPE _top(_Queue *Q)
{
return Q->_front->data;
}
_Queue* create(_Queue* &Q)
{
Q = (_Queue *)malloc(sizeof(_Queue));
if(Q==NULL)
{
perror("malloc failed");
return NULL;
}
else
{
Q->_front = NULL;
Q->_rear = NULL;
return Q;
}
}
int main()
{
_Queue *Q = create(Q);
_push(Q,'a');
printf("%c ",_top(Q));
_push(Q,'b');
printf("%c ",_top(Q));
_push(Q,'c');
printf("%c ",_top(Q));
_push(Q,'d');
printf("%c ",_top(Q));
_delstack(Q);
return 0;
}
链式队列实现
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 题目: 是的,我又不想打了,如题 本身这个很基础,主要及时判断一下栈是不是空。但是因为OJ用的不熟练,所以写一个这...
- 本题来自程序员代码面试指南 编写一个类,用两个栈实现队列,支持队列的基本操作(add、poll、peek) 实现思...
- 数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心...