下面通过分别用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【第十七篇】jQuery

    1.jQuery是什么? jQuery是一个 JavaScript/Dom/Bom 库. jQuery 极大地简化了 JavaScript 编程. jQuery 很容易学习. 2.jQuery对象与D ...

  2. Objective-C中的const ,extern,static

    一.const 1>对于const,记住关键的一点,它只是修饰右边的变量. 例如: - (void)viewDidLoad { [super viewDidLoad]; // const两种用法 ...

  3. 要将表的限制条件写到与该表同级别的where中

    测试目的:将朱查询的限制条件放到子查询的where中,查看性能影响. 测试数据:create table t1 as select object_id,object_name from dba_obj ...

  4. C++ 11 笔记 (一) : lambda

    时至今日都是我咎由自取,错就是错,与任何人无关.掉进C++98的各种坑里无法自拔的抖M感,让我选择了华丽丽的无视C++11,导致今日面对开源的代码到各种看不懂的地步,一入C++深似海,我今天愿意承担一 ...

  5. Mongodb与关系型数据库

    MongoDB没有固定的关系约束 没有事务, 安全性不高 不一定保证数据的一致性. ACID不符合 NoSQL 放弃了传统关系型数据库严格的事务一致性和范式约束,采用弱一致性模型. http://os ...

  6. leetcode面试准备:Contains Duplicate I && II

    1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...

  7. perl post json数据

    use LWP::UserAgent; use URI::Escape; use Net::Ping; use JSON qw(encode_json); use Socket; use Net::S ...

  8. 【HDOJ】1175 连连看

    BFS.wa了一下午,原来是YES,写成了Yes. #include <iostream> #include <cstdio> #include <cstring> ...

  9. Docker在云环境中的应用实践初探:优势、局限性与效能评测

    作者 商之狄 发布于 2014年11月10日 本文依据笔者所在团队的一些近期开发和应用的实践,整理出一些有意义的信息,拿出来和社区分享.其中既包括在云端应用Docker与相关技术的讨论,同时也有实施过 ...

  10. struts2错误:The Struts dispatcher cannot be found.

    struts2错误:The Struts dispatcher cannot be found. The Struts dispatcher cannot be found. This is usua ...