下面通过分别用C和C++来实现一个链栈(链表实现),从中体会数据封装抽象的思想:

C语言实现:

 C++ Code 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

 
#include <stdio.h>


#include <stdlib.h>


#include <assert.h>

struct Link

{

    
int data;

    
struct Link *next;

};

struct Stack

{

    
struct Link *head;

    
int size;

};

void StackInit(
struct Stack *stack)

{

    stack->head = 
NULL;

    stack->size = 
;

}

void StackPush(
struct Stack *stack, 
const 
int data)

{

    
struct Link *node;

    node = (
struct Link *)malloc(
sizeof(
struct Link));

    assert(node != 
NULL);

    node->data = data;

    node->next = stack->head;

    stack->head = node;

    ++stack->size;

}

int StackEmpty(
struct Stack *stack)

{

    
return (stack->size == 
);

}

int StackPop(
struct Stack *stack, 
int *data)

{

    
if (StackEmpty(stack))

    {

        
return 
;

    }

struct Link *tmp = stack->head;

    *data = stack->head->data;

    stack->head = stack->head->next;

    free(tmp);

    --stack->size;

return 
;

}

void StackCleanup(
struct Stack *stack)

{

    
struct Link *tmp;

    
while (stack->head)

    {

        tmp = stack->head;

        stack->head = stack->head->next;

        free(tmp);

    }

stack->size = 
;

}

int main(
void)

{

    
struct Stack stack;

    StackInit(&stack);

    
int i;

    
for (i = 
; i < 
; i++)

    {

        StackPush(&stack, i);

    }

while (!StackEmpty(&stack))

    {

        StackPop(&stack, &i);

        printf(
"%d ", i);

    }

    printf(
"\n");

return 
;

}

C++实现:

 C++ Code 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

 
#include <iostream>


using 
namespace std;

class Stack

{


private:

    
struct Link

    {

        
int data_;

        Link *next_;

        Link(
int data, Link *next) : data_(data), next_(next)

        {

}

    };

public:

    Stack() : head_(
), size_(
)

    {

}

~Stack()

    {

        Link *tmp;

        
while (head_)

        {

            tmp = head_;

            head_ = head_->next_;

            
delete tmp;

        }

    }

void Push(
const 
int data)

    {

        Link *node = 
new Link(data, head_);

        head_ = node;

        ++size_;

    }

bool Empty()

    {

        
return (size_ == 
);

    }

bool Pop(
int &data)

    {

        
if (Empty())

        {

            
return 
false;

        }

Link *tmp = head_;

        data = head_->data_;

        head_ = head_->next_;

        
delete tmp;

        --size_;

return 
true;

    }

private:

    Link *head_;

    
int size_;

};

// 避免名称冲突
// 类型的扩充
// 数据封装、能够保护内部的数据结构不遭受外界破坏

int main(
void)

{

    Stack stack;        
// 抽象数据类型  类类型
    
int i;

    
for (i = 
; i < 
; i++)

    {

        stack.Push(i);      
// this = &stack
    }

while (!stack.Empty())

    {

        stack.Pop(i);

        cout << i << 
" ";

    }

cout << endl;

return 
;

}

输出都是一致的,对比不同的写法,可以体会两种语言的一些不同之处,当然这只是比较显而易见的方面了。

从零开始学C++之数据封装与抽象:分别用C和C++来实现一个链栈的更多相关文章

  1. 从零开始学 Web 之 jQuery(七)事件冒泡,事件参数对象,链式编程原理

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  2. 从零开始学 Web 系列教程

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新…… github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:http:/ ...

  3. 从零开始学Kotlin-使用接口(7)

    从零开始学Kotlin基础篇系列文章 定义接口 使用关键字interface定义接口 interface InterfaceDemo7 { } 类或对象可以实现一个或者多个接口 class demo7 ...

  4. 54. spring boot日志升级篇—logback【从零开始学Spring Boot】

    在<44. Spring Boot日志记录SLF4J>章节中有关相关的介绍,这里我们在深入的了解下logback框架. 为什么要使用logback ? --在开发中不建议使用System. ...

  5. (36)Spring Boot Cache理论篇【从零开始学Spring Boot】

    Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...

  6. 从零开始学Graph Database:什么是图

    摘要:本文从零开始引导与大家一起学习图知识.希望大家可以通过本教程学习如何使用图数据库与图计算引擎.本篇将以华为云图引擎服务来辅助大家学习如何使用图数据库与图计算引擎. 本文分享自华为云社区<从 ...

  7. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  8. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  9. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

随机推荐

  1. GitHub使用教程for Eclipse

    1.下载egit插件http://www.eclipse.org/egit/ http://www.eclipse.org/egit/download/ Installing the Latest R ...

  2. android SurfaceView绘制 重新学习--切图clipRect详解

    解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...

  3. Python的数字类型及其技巧

    Python中的数字类型 int float fractions.Fraction decimal.Decimal 数字的舍与入 int(f):舍去小数部分,只保留整数部分,所以int(-3.8)的结 ...

  4. ubuntu 安装mysql及修改编码

    643  netstat -tap | grep mysql  645  apt-get install mysql-server mysql-client  646  netstat -tap | ...

  5. 如何有效申请TI的免费样片

     转自如何有效申请TI的免费样片 TI公司愿意为支持中国大学的师生们的教学.实验.创新实践.竞赛和科研项目,提供有限数量的免费样片.首先需要指出的是:所有的样片申请应该是诚实正当的,所有不恰当的申 ...

  6. PowerDesigner从SqlServer 数据库中导入实体模型

    此篇是之前写的,从我的CSDN博客挖过来的- 一.开启数据库服务并配置ODBC数据源 1.开启数据库服务 (1)通过SQL Server Configuration Manager配置工具启动SQL ...

  7. Linux用户环境变量

    Linux用户环境变量 环境变量就是系统或软件设置的一些参数,用户环境变量就是用户登录系统后,都有自已专用的运行环境.在Windows系统中用户环境变量保存在用户家目录,Linux也是同样的.本文主要 ...

  8. 【BZOJ 3926】 [Zjoi2015]诸神眷顾的幻想乡 (广义SAM)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 974  Solved: 573 Descriptio ...

  9. VS2013 ASP.NET MVC 修改Web项目的IISExpress的端口固定

    [首先]关闭防火墙,或防火墙开放端口  在解决方案中,右键某项目,属性——Web——服务器——选择IISExpress URL输入:http://localhost:8000/   直接将8000更改 ...

  10. EditPlus+MinGW搭建简易的C/C++开发环境

    EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...