下面通过分别用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. python调用Moxa PCOMM Lite通过串口Ymodem协议发送文件

    本文采用python 2.7编写. 经过长期搜寻,终于找到了Moxa PCOMM Lite.调用PCOMM.DLL可以非常方便的通过串口的Xmodem.Ymodem.Zmodem等协议传输文件,而无需 ...

  2. 2016030208 - sql50题练习题

    数据库建表脚本和使用的数据请参考:http://www.cnblogs.com/zhtzyh2012/p/5235826.html sql50题练习参看:http://blog.sina.com.cn ...

  3. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

  4. web design tools

    https://www.google.com/webdesigner/ http://html.adobe.com/edge/inspect/ http://www.creativebloq.com/ ...

  5. C# 网页自动填表自动登录(转)

    自动填表的方式有很多,关键是获取控件的id或者name. 比如源代码有 <input id="pwdInput" tabindex="2" class=& ...

  6. VCC,VDD,VEE,VSS,VPP 表示的意义

    转自VCC,VDD,VEE,VSS,VPP 表示的意义 VCC,VDD,VEE,VSS,VPP 表示的意义 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, ...

  7. 【HDOJ】1166 敌兵布阵

    线段树. #include <stdio.h> #define maxn 55555 ]; void PushUP(int rt) { sums[rt] = sums[rt<< ...

  8. 用sql语句写排名

    使用SQL语句求排名 表jh03有下列数据: name score aa 99 bb 56 cc 56 dd 77 ee 78 ff 76 gg 78 ff 50 1. 名次生成方式1 , Score ...

  9. 【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!

    转自:http://blog.csdn.net/gqqnb/article/details/6412364 2012.1.24更新 增加了“安装方法” eclipse是一个程序开发平台,它本身并不限制 ...

  10. codeforce

    A. Playing with Dice time limit per test 1 second memory limit per test 256 megabytes input standard ...