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函 ...
随机推荐
- Selenium如何定位动态id/class的元素?
今天再给大家分享一个定位不到元素的原因——动态id. 没有打开新页面.没有alert.没有frame.加了等待时间,但是还是定位不到元素?很有可能是你要定位的元素的属性是动态的,即每次打开页面,这个 ...
- SVG素材整理(原)
why SVG? 1.矢量的国际图形标准,以后随着浏览器的发展,相信矢量会更多的出现 2.illustartor等多数矢量绘图软件可以导出为这种格式 so SVG现状 基本介绍:http:// ...
- SharePoint Server 2013 Excel Web Access无法显示
环境信息:SharePoint Server 2013 中文版,版本为15.0.4420.1017 Windows Server 2008 r2中文版 Sql Server 2012 问题描述:在Sh ...
- 深度剖析MQTT协议的整个通信流程
http://www.elecfans.com/d/587483.html MQTT,目前物联网的最主要的协议,基本所有收费的云平台都是基于MQTT协议,比如机智云,和所有的开放云平台比如中国移动的o ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- 条目六《当心C++编译器中最烦人的分析机制》
当心C++编译器中最烦人的分析机制 C++是较为底层的面相对象语言,在底层的语法规则分析中,有很多隐藏的分析机制. C++中的普遍规律相符,即尽可能地解释为函数声明. 把形式参数的声明用括号括起来是非 ...
- 两种unix网络编程线程池的设计方法
unp27章节中的27.12中,我们的子线程是通过操作共享任务缓冲区,得到task的,也就是通过线程间共享的clifd[]数组,这个数组其实就是我们的任务数组,得到其中的connfd资源. 我们对这个 ...
- 用 GitLab CI 进行持续集成
简介 从 GitLab 8.0 开始,GitLab CI 就已经集成在 GitLab 中,我们只要在项目中添加一个 .gitlab-ci.yml 文件,然后添加一个 Runner,即可进行持续集成. ...
- Hadoop Hive概念学习系列之什么是Hive?
参考 <Hadoop大数据分析与挖掘实战>的在线电子书阅读 http://yuedu.baidu.com/ebook/d128cf8e33687e21 ...
- apache shiro学习笔记
一.权限概述 1.1 认证与授权 认证:系统提供的用于识别用户身份的功能,通常登录功能就是认证功能-----让系统知道你是谁?? 授权:系统授予用户可以访问哪些功能的许可(证书)----让系统知道你能 ...