下面通过分别用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. MVVM Light 一个窗口承载两个视图

    MVVM Light 一个窗口承载两个视图   原文地址:http://www.codeproject.com/Articles/323187/MVVMLight-Using-Two-Views 本文 ...

  2. Remark of BLENDFUNCTION from MSDN

    Remarks When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, ...

  3. win7+ubuntu双系统安装攻略

    一1.下载分区软件,为ubuntu安装分出一个区 2.磁盘管理器,选中该区,右键,删除卷,该区变为绿色,成为空闲区 3.成功 二为ubunt添加开机导引项 1,安装好easybcd2.0后,启动软件: ...

  4. 利用TEA算法进行数据加密

    TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计. ...

  5. Android CoordinatorLayout + AppBarLayout(向上滚动隐藏指定的View)

    在新的Android Support Library里面,新增了CoordinatorLayout, AppBarLayout等. 实现的效果: 向下滚动RecylerView,Tab会被隐藏,向上滚 ...

  6. 利用switch case 来运行咱们结婚吧

    static void Main(string[] args)        {            while (true)            {                int x, ...

  7. Codevs_1048_石子归并_(动态规划)

    描述 http://codevs.cn/problem/1048/  1048 石子归并 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Des ...

  8. BZOJ_1269_文本编辑器_[AHOI2006]_(Spaly)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1269 和BZOJ_1507很像的题,多了一个反转操作,还是Splay简单区间操作的模板题. 1 ...

  9. [ZOJ 3622] Magic Number

    Magic Number Time Limit: 2 Seconds      Memory Limit: 32768 KB A positive number y is called magic n ...

  10. Makefile中include、-include、sinclude的区别

    如果指示符“include”指定的文件不是以斜线开始(绝对路径,如/usr/src/Makefile...),而且当前目录下也不存在此文件:make将根据文件名试图在以下几个目录下查找:首先,查找使用 ...