• C语言实现栈和C++实现栈的区别


    C语言版本

    #include
    #include
    #include
    typedef int DataType;
    typedef struct Stack
    {
    	DataType* array;
    	int capacity;
    	int size;
    }Stack;
    //初始化
    void StackInit(Stack* ps)
    {
    	assert(ps);
    
    	ps->array = (DataType*)malloc(3 * sizeof(DataType));  // 为ps->array分配初始内存空间
    
    	ps->capacity = 3;
    	ps->size = 0;
    }
    //销毁
    void StackDestroy(Stack* ps)
    {
    	assert(ps);
    	if (ps->array)
    	{
    		free(ps->array);
    		ps->array = NULL;
    		ps->capacity = 0;
    		ps->size = 0;
    	}
    }
    //扩容
    void CheckCapacity(Stack* ps)
    {
    	if (ps->size == ps->capacity)
    	{
    		int newcapacity = ps->capacity * 2;
    		DataType* temp = (DataType*)realloc(ps->array,
    			newcapacity * sizeof(DataType));
    		if (temp == NULL)
    		{
    			perror("realloc申请空间失败!!!");
    			return;
    		}
    		ps->array = temp;
    		ps->capacity = newcapacity;
    	}
    }
    //插入数据
    void StackPush(Stack* ps, DataType data)
    {
    	assert(ps);
    	CheckCapacity(ps);
    	ps->array[ps->size] = data;
    	ps->size++;
    }
    //是否为空
    int StackEmpty(Stack* ps)
    {
    	assert(ps);
    	return 0 == ps->size;
    }
    //删除数据
    void StackPop(Stack* ps)
    {
    	if (StackEmpty(ps))
    		return;
    	ps->size--;
    }
    //返回栈顶元素
    DataType StackTop(Stack* ps)
    {
    	assert(!StackEmpty(ps));
    	return ps->array[ps->size - 1];
    }
    
    //大小
    int Stacksize(Stack* ps)
    {
    	assert(ps);
    	return ps->size;
    }
    int main()
    {
    	Stack s;
    	StackInit(&s);
    	StackPush(&s, 1);
    	StackPush(&s, 2);
    	StackPush(&s, 3);
    	StackPush(&s, 4);
    	StackPush(&s, 5);
    	printf("%d\n", StackTop(&s));
    	printf("%d\n", Stacksize(&s));
    	while (!StackEmpty(&s))
    	{
    		printf("%d ", StackTop(&s));
    		StackPop(&s);
    	}
    	StackDestroy(&s);
    }
    
    • 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
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    用C语言实现栈,栈的相关函数有以下特性:

    1. 每个函数的第一个参数都是Stack*
    2. 每个函数中必须要对第一个参数检测,因为该参数可能会为NULL
    3. 函数中都是通过Stack*参数操作栈的
    4. 调用时必须传递Stack结构体变量的地址
      结构体中只能定义存放数据的结构,操作数据的方法不能放在结构体中,即数据和操作数据的方式是分离开的,而且实现上相当复杂一点,涉及到大量指针操作,稍不注意可能就会出错

    C++版本

    #include
    #include
    using namespace std;
    typedef int DataType;
    class Stack
    {
    public:
    	//初始化
    	void Init()
    	{
    		_array = (DataType*)malloc(sizeof(DataType) * 3);
    		if (NULL == _array)
    		{
    			perror("malloc申请空间失败!!!");
    			return;
    		}
    		_capacity = 3;
    		_size = 0;
    	}
    	//插入
    	void Push(DataType data)
    	{
    		CheckCapacity();
    		_array[_size] = data;
    		_size++;
    	}
    	//删除
    	void Pop()
    	{
    		if (Empty())
    		{
    			return;
    		}
    		_size--;
    	}
    	//栈顶元素
    	DataType Top()
    	{
    		return _array[_size - 1];
    	}
    	//栈顶元素
    	int Empty()
    	{
    		return 0 == _size;
    	}
    	//大小
    	int Size()
    	{
    		return _size;
    	}
    	//销毁
    	void Destroy()
    	{
    		if (_array)
    		{
    			free(_array);
    			_array = NULL;
    			_capacity = 0;
    			_size = 0;
    		}
    	}
    private:
    	//扩容
    	void CheckCapacity()
    	{
    		if (_size == _capacity)
    		{
    			int newcapacity = _capacity * 2;
    			DataType* tmp = (DataType*)realloc(_array, newcapacity * sizeof(DataType));
    			if (tmp == NULL)
    			{
    				perror("realloc申请空间失败!!!");
    				return;
    			}
    			_array = tmp;
    			_capacity = newcapacity;
    		}
    	}
    	DataType* _array;
    	int _capacity;
    	int _size;
    };
    int main()
    {
    	Stack s;
    	s.Init();
    	s.Push(1);
    	s.Push(2);
    	s.Push(3);
    	s.Push(4);
    	s.Push(5);
    	while (!s.Empty())
    	{
    		cout << s.Top() << endl;
    		s.Pop();
    	}
    	s.Destroy();
    	return 0;
    }
    
    • 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
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    C++中通过类可以将数据以及操作数据的方法进行完美结合,通过访问权限可以控制那些方法在类外可以被调用,即封装
    在使用时就像自己成员变量一样,更符合人类对一件事物的认知。而且每个方法不需要传递Stack的参数了,编译器编译之后该参数会自动还原,即C++中Stack参数是编译器维护的,C语言中需要用户自己维护

  • 相关阅读:
    JupyterLab使用指南(二):JupyterLab基础
    如何在Docker中列出容器
    [Linux]-----_inode与软硬连接
    面试 Java 并发编程八股文十问十答第四期
    基础算法练习200题12、统计奇偶数
    【100个 Unity实用技能】| Unity中常用的几种路径 分析,不同平台路径总结
    一种轻量级单体springboot防重复提交的解决方案
    Day63 处理机调度的概念和层次
    MySQL存储引擎
    你想学 Python 爬虫?看看这篇关于开发者工具神器的博客吧
  • 原文地址:https://blog.csdn.net/weixin_51799303/article/details/133554331