利用 C++ 单向链表实现队列
利用C++ 单向链表实现数据结构队列,其实和上一篇基本内容相同,仅仅是插入的时候在链表的尾部插入,取元素都是一样的,都从头部取。
#pragma once #include "stdio.h"
//利用链表来实现队列,先进先出 class queue
{
public:
queue(void);
queue(int value);
~queue(void);
private:
int m_value;
queue* m_pnext;
public:
void push(int value);
bool pop(int *value);
bool top(int *value);
bool empty();
int size();
void output();
void destroy();
}; #include "stdafx.h"
#include "queue.h" //构造一个空的队列头指针
queue::queue(void)
{
m_value = 0x00;
m_pnext = NULL;
} //构建一个队列结点
queue::queue(int value)
{
m_value = value;
m_pnext = NULL;
} //输出被删除掉的结点
queue::~queue(void)
{
printf("destroy node its value=%d\n", m_value);
} //元素入队列
void queue::push(int value)
{
queue *pnode = this;
while(pnode->m_pnext != NULL)
{
pnode = pnode->m_pnext;
}
queue *newnode = new queue(value);
pnode->m_pnext = newnode;
m_value++;
} //元素出队列
bool queue::pop(int *value)
{
bool result = false;
if (m_pnext != NULL)
{
*value = m_pnext->m_value;
m_pnext = m_pnext->m_pnext;
result = true;
m_value--;
}
return result;
} //得到队列顶部的元素
bool queue::top(int *value)
{
bool result = false;
if (m_pnext != NULL)
{
*value = m_pnext->m_value;
result = true;
}
return result;
} //判断队列是否为空
bool queue::empty()
{
bool result = false;
if (m_pnext == NULL)
{
result = true;
}
return result;
} //得到队列大小
int queue::size()
{
return m_value;
} //输出队列中的元素
void queue::output()
{
queue* pnode = this;
while(pnode->m_pnext != NULL)
{
printf("index=%d\n", pnode->m_pnext->m_value);
pnode = pnode->m_pnext; }
} //销毁队列
void queue::destroy()
{
while(m_pnext != NULL)
{
queue* pnode = m_pnext;
m_pnext = m_pnext->m_pnext;
delete pnode;
}
}
主程序测试
// myqueue.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "queue.h" int _tmain(int argc, _TCHAR* argv[])
{
queue myqueue;
for(int i=0; i<10; i++)
{
myqueue.push(i * 10);
} myqueue.output();
printf("queue size=%d\n", myqueue.size()); if (myqueue.empty())
{
printf("queue is empty\n");
}
else
{
printf("queue is not empty\n");
} myqueue.destroy(); int value = 0;
for(int i=0; i<10; i++)
{
if (myqueue.pop(&value))
{
printf("pop value=%d successfully\n", value);
}
else
{
printf("pop unsuccessfully\n");
}
} printf("queue size=%d\n", myqueue.size()); if (myqueue.empty())
{
printf("queue is empty\n");
}
else
{
printf("queue is not empty\n");
} getchar();
return 0;
}
利用 C++ 单向链表实现队列的更多相关文章
- OC实现 单向链表
需要实现一个消息队列,队列具有 FIFO 特点,即先入先出,在这里采用单向链表实现队列逻辑. 本次要实现的队列要求: 1. 节点可以存放任意类型数据 2. 线程安全 简单说明一下: 1. 创建CFNo ...
- Linus:利用二级指针删除单向链表
Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...
- 【转】Linus:利用二级指针删除单向链表
原文作者:陈皓 原文链接:http://coolshell.cn/articles/8990.html 感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多, ...
- 转:Linus:利用二级指针删除单向链表
感谢网友full_of_bull投递此文(注:此文最初发表在这个这里,我对原文后半段修改了许多,并加入了插图) Linus大婶在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是 ...
- 单向链表的简单Java实现-sunziren
写在前面,csdn的那篇同名博客就是我写的,我把它现在在这边重新发布,因为我实在不想用csdn了,那边的广告太多了,还有就是那个恶心人的“阅读更多”按钮,惹不起我躲得起. 最近面试的过程中,发现有的公 ...
- C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)
#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...
- C语言单向链表
1,为什么要用到链表 数组作为存放同类数据的集合,给我们在程序设计时带来很多的方便,增加了灵活性.但数组也同样存在一些弊病.如数组的大小在定义时要事先规定,不能在程序中进行调整,这样一来,在程序设计中 ...
- Java实现单向链表基本功能
一.前言 最近在回顾数据结构与算法,有部分的算法题用到了栈的思想,说起栈又不得不说链表了.数组和链表都是线性存储结构的基础,栈和队列都是线性存储结构的应用- 本文主要讲解单链表的基础知识点,做一个简单 ...
- 教你如何使用Java手写一个基于链表的队列
在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...
随机推荐
- 14.9.2 Specifying the Row Format for a Table 指定 表的行格式
14.9.2 Specifying the Row Format for a Table 指定 表的行格式 mysql> SHOW TABLE STATUS\G; *************** ...
- Android高手进阶——Adapter深入理解与优化
Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...
- 枚举算法总结 coming~^.*
感谢CJ同学监督╭(╯^╰)╮.从放假到现在都木有更新博客了~噶呜~小娘谨记教诲,每天会更新博客==!! 看了一下POJ训练计划,虽然已经零零散散做了40多道题了,还是从头开始整理一下漏掉的知识点.T ...
- phpStorm 新建文件SVN不提交的解决的方法
phpStorm中新建文件夹,可是打开文件夹.却没有提交到SVN.导致每次都必须手动增加.假设新增的文件夹或者文件较多文件夹较深,easy遗漏.(default7#zbphp.com) 解决的方法: ...
- Swift - 使用UIView给页面添加4×4方格
1,下面是一个利用UIView来给页面上绘制灰色方块的例子,效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- 文档数据库RavenDB-介绍与初体验
文档数据库RavenDB-介绍与初体验 阅读目录 1.RavenDB概述与特性 2.RavenDB安装 3.C#开发初体验 4.RavenDB资源 不知不觉,“.NET平台开源项目速览“系列文章已经1 ...
- GitHub上最受欢迎的Android开源项目TOP20
以下这些开源项目都是从GitHub上筛选的,我强烈推荐android程序源代码有时间的时候自己在上面淘淘,或许能发现自己须要的开源程序. 了解开源项目有两个优点: 1.借鉴代码,一般来说.火爆的开源项 ...
- TComponent明明实现了IDispatch接口,但是却不加上声明,难道是因为FVCLComObject实体对象不存在?
TComponent明明实现了IDispatch接口,可是它的声明却是: TComponent = class(TPersistent, IInterface, IInterfaceComponent ...
- [Android学习笔记]使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...
- UML中类图的符号解释
在UML的定义中,描写叙述类和对象之间的关系,包含下面几种方式:依赖(Dependency).关联(Association).聚合(Aggregation).组合(Composition).泛化(Ge ...