c语言实现队列的基本操作
话不多说,直接代码
#include"stdio.h"
#include"stdlib.h"
typedef struct QNode{
int date;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//初始化
int InitStack(LinkQueue &S){
S.front=(QueuePtr)malloc(sizeof(QNode));
//S.front=NULL;
S.rear=S.front;
if(!S.front)
return ;
S.front->next=NULL;
return ;
}
//进
int EnQueue(LinkQueue &S,int e){
QueuePtr p=NULL;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit();
p->date=e;
p->next=;
S.rear->next=p;
S.rear=p;
return ;
}
//置空
void ClearQueue(LinkQueue &S){
QueuePtr p=NULL;
p=S.front;
while(S.front!=S.rear){
p=S.front->next;
free(S.front);
S.front=p;
}
}
//判空
void QueueEmpty(LinkQueue &S){
if(S.front==S.rear)
printf("判空:是\n");
else
printf("判空:否\n"); }
//长度
int QueueLen(LinkQueue &S){
QueuePtr p=NULL;
int len=;
p=S.front;
if(S.front==S.rear)
return len;
else{
while(p!=S.rear){
len++;
p=p->next;
}
return len;
}
}
void len(LinkQueue &S){
printf("%d\n",S.front);
printf("%d\n",S.front->next);
printf("%d\n",S.rear);
printf("大小%d\n",sizeof(QNode));
printf("%d\n",S.rear-S.front->next);
printf("%d\n",S.rear-S.front);
printf("%d\n",(S.rear-S.front->next)/sizeof(QNode)); }
//出
int pop(LinkQueue &S){
int tem=;
QueuePtr p=NULL;
if(S.front->next==NULL)
return ;
else{
tem=S.front->next->date;
p=S.front->next;
free(S.front);
S.front=p;
return tem;
}
}
//输出
void QueueTraverse(LinkQueue &S){
printf("输出:");
QueuePtr p=S.front;
while(p!=S.rear){
printf("%d ",p->next->date);
p=p->next;
} }
void main(){
LinkQueue S;
printf("初始化:");
printf("%d\n",InitStack(S));
printf("%d\n",EnQueue(S,));
printf("%d\n",EnQueue(S,));
printf("%d\n",EnQueue(S,));
printf("%d\n",EnQueue(S,));
printf("%d\n",EnQueue(S,));
printf("长度:%d\n",QueueLen(S));
len(S);
QueueTraverse(S);
printf("输出:%d\n",pop(S));
QueueTraverse(S);
EnQueue(S,);
QueueTraverse(S);
QueueEmpty(S);
ClearQueue(S);
QueueEmpty(S);
}
在使用构造体的时候注意应该有两个构造体,分别对应整个链表和链表的一个结点。
在获取队列长度的时候不能像栈一样队首队尾直接相减,具体我也搞不懂为什么,代码中的len函数就是对该方法的测试。
另外,一定在。要注意S.front并不是第一个元素的位置,S.front->next才是,见图
c语言实现队列的基本操作的更多相关文章
- 二、 编写一个类,用两个栈实现队列,支持队列的基本操作(add,poll,peek)
请指教交流! package com.it.hxs.c01; import java.util.Stack; /* 编写一个类,用两个栈实现队列,支持队列的基本操作(add,poll,peek) */ ...
- c语言描述的链队列的基本操作
#include<stdio.h> #include<stdlib.h> #define ok 0 #define error 1 //链队列特点在于不仅有链的头指针和尾指针, ...
- c语言多线程队列读写
最近用c语言写了个简单的队列服务,记录一下,文件结构为 main.c queue.c queue.h,代码如下: 主函数 #define NUM_THREADS 200 #include <st ...
- C语言实现二叉树的基本操作
二叉树是一种非常重要的数据结构.本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历.中序遍历.后序遍历.层次遍历),二叉搜索树的构造等. 1. 二叉树的构建 二叉树的基本 ...
- C语言数据结构-队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-队列的实现-C语言 //队列的存储结构 #define MAXSIZE 100 typedef struct { int* base; //基地址 int _front; //头指针 i ...
- Go语言基础之16--Mysql基本操作
一.Mysql驱动及数据库连接 1.1 Golang中的Mysql驱动 A. https://github.com/go-sql-driver/mysql B. Go本身不提供具体数据库驱动,只提供驱 ...
- D_S 循环队列的基本操作
// main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...
- C语言 复杂队列(链表队列)
//复杂的队列二 --链表队列 #include<stdio.h> #include<stdlib.h> #define datatype int struct queueli ...
- c语言对于文本的基本操作
字符读写函数 :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式化读写函数:fscanf和fprinf 1.字符读写: fgetc函 ...
随机推荐
- mysql_5.6内存过高问题解决
MySQL 5.6安装完之后,每过一段时间就会莫名其妙挂掉.而且还很难启动.非要重启服务器,才能拉起mysql. 后来分析是由于mysql启动后内存过高,跑一段时间就会由于内存不足而被杀死. 今天分析 ...
- springMVC工作原理趣味解析
springMVC 涉及的人有: 1:浏览器 2:DispatherServlet 3:Handler 4:HandlerAdapter ...
- SOA IN Real World
微软发布了一个名为“真实世界里的面向服务架构(SOA)”的电子书.这本书表达了微软对面向服务架构的观点,并包括了数个展示如何用微软产品和技术实现SOA的真实案例.书中解释到,SOA的功能型架构本身是松 ...
- 比较php字符串连接的效率
php字符串连接有三种方式 1)使用 . 链接 2)使用 .= 连接 3)implode 函数连接数组元素 /*以下测试在ci框架进行*/ private function get_mcrotime( ...
- Kotlin when 流程判断
如果学过C或者java C#等语言. 一定熟悉SWITCH这个流程判断 但是在kotlin中却没有这个.而是 使用了When来代替. 当什么时候. 下面我觉一个简单的例子: import java.u ...
- [SinGuLaRiTy] 高级搜索算法
[SinGuLaRiTy-1039] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 迭代加深搜索(ID) 迭代加深搜索,实质上就是限定下界的 ...
- kali linux之主动信息收集(二层发现)
主动信息收集: 直接与目标系统交互信息,无法避免留下访问的痕迹 使用受控的第三方电脑进行探测,如(使用代理或者使用肉鸡,做好被封杀的准备,使用噪声迷惑目标,淹没真实的探测流量) 识别活着的主机,会有潜 ...
- loj #6201. 「YNOI2016」掉进兔子洞
#6201. 「YNOI2016」掉进兔子洞 您正在打galgame,然后突然发现您今天太颓了,于是想写个数据结构题练练手: 给出一个长为 nnn 的序列 aaa. 有 mmm 个询问,每次询问三个区 ...
- 如何理解java采用Unicode编码
http://blog.csdn.net/gjb724332682/article/details/43229563 Java中字符仅以一种形式存在,那就是Unicode.由于java采用unicod ...
- 用Hadoop,还是不用Hadoop?
本文由 伯乐在线 - Lex Lian 翻译.英文出处:Anand Krishnaswamy.欢迎加入翻译小组. Hadoop通常被认定是能够帮助你解决所有问题的唯一方案. 当人们提到“大数据”或是“ ...