话不多说,直接代码

 #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语言实现队列的基本操作的更多相关文章

  1. 二、 编写一个类,用两个栈实现队列,支持队列的基本操作(add,poll,peek)

    请指教交流! package com.it.hxs.c01; import java.util.Stack; /* 编写一个类,用两个栈实现队列,支持队列的基本操作(add,poll,peek) */ ...

  2. c语言描述的链队列的基本操作

    #include<stdio.h> #include<stdlib.h> #define ok 0 #define error 1 //链队列特点在于不仅有链的头指针和尾指针, ...

  3. c语言多线程队列读写

    最近用c语言写了个简单的队列服务,记录一下,文件结构为 main.c queue.c queue.h,代码如下: 主函数 #define NUM_THREADS 200 #include <st ...

  4. C语言实现二叉树的基本操作

    二叉树是一种非常重要的数据结构.本文总结了二叉树的常见操作:二叉树的构建,查找,删除,二叉树的遍历(包括前序遍历.中序遍历.后序遍历.层次遍历),二叉搜索树的构造等. 1. 二叉树的构建 二叉树的基本 ...

  5. C语言数据结构-队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作

    1.数据结构-队列的实现-C语言 //队列的存储结构 #define MAXSIZE 100 typedef struct { int* base; //基地址 int _front; //头指针 i ...

  6. Go语言基础之16--Mysql基本操作

    一.Mysql驱动及数据库连接 1.1 Golang中的Mysql驱动 A. https://github.com/go-sql-driver/mysql B. Go本身不提供具体数据库驱动,只提供驱 ...

  7. D_S 循环队列的基本操作

    //  main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...

  8. C语言 复杂队列(链表队列)

    //复杂的队列二 --链表队列 #include<stdio.h> #include<stdlib.h> #define datatype int struct queueli ...

  9. c语言对于文本的基本操作

    字符读写函数  :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式化读写函数:fscanf和fprinf   1.字符读写: fgetc函 ...

随机推荐

  1. (转)不定义JQuery插件,不要说会JQuery

    原文地址:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#" ...

  2. 【C#】特性标签中的属性解释

    第一个为特性作用于类,或者接口(interface) 第二个为是否允许重叠定义,就是连续写两个特性标签 第三个为是否继承,当继承时候,除输出子类外,父类也将输出

  3. angular 子路由

    const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ...

  4. 以太坊系列之二: 单调时间monotime-以太坊源码学习

    在程序中需要测量时间时最好使用monotime.Now()而不是time.Now(),相比之下前者更准确. 来个示例: func main() { var start, elapsed time.Du ...

  5. Android Studio无法找到tool.jar解决方法!

    今天安装并配置了JDK,可以在DOS窗口中使用“java -version”命令查看JAVA版本信息了,随后安装Android Studio,但是等Android Studio安装完毕,启动时候发现, ...

  6. spring框架所有包说明

    spring依赖的jar包如下:下面是每个jar包的说明spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet ...

  7. 【bzoj1014】: [JSOI2008]火星人prefix 平衡树-字符串-hash-二分

    [bzoj1014]: [JSOI2008]火星人 用平衡树维护字符串的hash 然后询问的时候二分一下就好了 /* http://www.cnblogs.com/karl07/ */ #includ ...

  8. win7 64位环境下,为python2.7 安装pip

    第一步: 安装python并配置好环境变量 参见:http://blog.csdn.net/donggege214/article/details/52062855 第二步: 下载setuptools ...

  9. HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  10. Web Service入门

    [IT168 技术文档] 一.什么是Web Service?     Web Service是构建互联网分布式系统的基本部件.Web Services 正成为企业应用集成(Enterprise App ...