下面通过分别用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. Linux使用问答

    1.ubuntu 查看安装的软件包? 在终端输入 sudo dpkg -l http://vardesa.blog.hexun.com/58593247_d.html 其他:http://qiuye. ...

  2. sql server 与C#数据类型对应表

  3. SQL Express几个版本的区别

    对于这三个文件:SQLEXPR32_x86_CHS.exe.SQLEXPR_x86_CHS.exe. SQLEXPR_x64_CHS.exe,大家一看就知道是sqlserver的express版本,但 ...

  4. linux JAVA JDK环境配置

    export JAVA_HOME=/usr/local/jdk1.7.0_45export JRE_HOME=/usr/local/jdk1.7.0_45/jreexport CLASSPATH=.: ...

  5. 通用php与mysql数据库配置文件

    <?php header("content-type:text/html;charset = utf-8"); $dblink = mysql_connect("l ...

  6. skiplist 跳表(2)-----细心学习

    快速了解skiplist请看:skiplist 跳表(1) http://blog.sina.com.cn/s/blog_693f08470101n2lv.html 本周我要介绍的数据结构,是我非常非 ...

  7. No Hibernate Session bound to thread, and configuration does not allow creat

    No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...

  8. bzoj 2706: [SDOI2012]棋盘覆盖 Dancing Link

    2706: [SDOI2012]棋盘覆盖 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 255  Solved: 77[Submit][Status] ...

  9. MyEclipse10.6导出war包出错

    在右键选中项目->export->java ee ->war 的时候,一点就报错SECURITY ALERT:INTEGRITY CHECK ...,之后自动关闭 这个问题是因为用的 ...

  10. 通过硬件层提高Android动画的性能

    曾有许多人问我为什么在他们开发的应用中,动画的性能表现都很差.对于这类问题,我往往会问他们:你们有尝试过在硬件层解决动画的性能问题么? 我们都知道,在播放动画的过程中View在每一帧动画的显示时重绘自 ...