c语言中堆栈

A stack is a linear data structure, collection of items of the same type.

堆栈是线性数据结构 ,是相同类型的项目的集合。

Stack follows the Last In First Out (LIFO) fashion wherein the last element entered is the first one to be popped out.

堆栈遵循后进先出(LIFO)的方式,其中最后输入的元素是要弹出的第一个元素。

In stacks, the insertion and deletion of elements happen only at one endpoint of it.

在堆栈中,元素的插入和删除仅发生在其一个端点。



1.在堆栈上执行的操作 (1. Operations performed on Stacks)

The following are the basic operations served by the Stacks.

以下是堆栈所服务的基本操作。

  • Push: This function adds an element to the top of the Stack.推送 :此功能将元素添加到堆栈顶部。
  • Pop: This function removes the topmost element from the stack.Pop :此函数从堆栈中删除最顶层的元素。
  • IsEmpty: Checks whether the stack is empty.IsEmpty :检查堆栈是否为空。
  • IsFull: Checks whether the stack is full.IsFull :检查堆栈是否已满。
  • Top: Displays the topmost element of the stack.顶部 :显示堆栈的最顶部元素。


2.栈的工作 (2. Working of Stacks)

Initially, we set a pointer Peek/Top to keep the track of the topmost item in the stack.

最初,我们设置一个指针Peek / Top来跟踪堆栈中最顶层的项目。

Initialize the stack to -1. Then, we check whether the stack is empty through the comparison of Peek to -1 i.e. Top == -1

初始化堆栈为-1。 然后,我们通过比较Peek与-1(即Top == -1)来检查堆栈是否为空

As we add the elements to the stack, the position of the Peek element keeps updating every time.

当我们将元素添加到堆栈中时,Peek元素的位置每次都会保持更新。

As soon as we pop or delete an item from the set of inputs, the top-most element gets deleted and thus the value of Peek/Top gets reduced.

一旦我们从一组输入中弹出或删除一个项目,最顶层的元素就会被删除,因此Peek / Top的值会减少。



3.在C中实现Stack (3. Implementing Stack in C)

Stacks can be represented using structures, pointers, arrays or linked lists.

堆栈可以使用结构 , 指针 , 数组或链表表示。

Here, We have implemented stacks using arrays in C.

在这里,我们使用C中的数组实现了堆栈。


#include<stdio.h>#include<stdlib.h>#define Size 4 int Top=-1, inp_array[Size];
void Push();
void Pop();
void show();int main()
{int choice;while(1)    {printf("\nOperations performed by Stack");printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");printf("\n\nEnter the choice:");scanf("%d",&choice);switch(choice){case 1: Push();break;case 2: Pop();break;case 3: show();break;case 4: exit(0);default: printf("\nInvalid choice!!");}}
}void Push()
{int x;if(Top==Size-1){printf("\nOverflow!!");}else{printf("\nEnter element to be inserted to the stack:");scanf("%d",&x);Top=Top+1;inp_array[Top]=x;}
}void Pop()
{if(Top==-1){printf("\nUnderflow!!");}else{printf("\nPopped element:  %d",inp_array[Top]);Top=Top-1;}
}void show()
{if(Top==-1){printf("\nUnderflow!!");}else{printf("\nElements present in the stack: \n");for(int i=Top;i>=0;--i)printf("%d\n",inp_array[i]);}
}

Output:

输出:


Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.EndEnter the choice:1Enter element to be inserted to the stack:10Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.EndEnter the choice:3Elements present in the stack:
10Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.EndEnter the choice:2
Popped element:  10Operations performed by Stack
1.Push the element
2.Pop the element
3.Show
4.EndEnter the choice:3
Underflow!!

4.堆栈操作的时间复杂度 (4. Time Complexity of Stack Operations)

As mentioned above, only a single element can be accessed at a time in Stacks.

如上所述,堆栈中一次只能访问一个元素。

While performing push() and pop() operations on the stack, it takes O(1) time.

在堆栈上执行push()和pop()操作时,需要O(1)时间。



5.堆栈的应用 (5. Applications of Stack)

As soon as the compiler encounters a function call, it gets pushed into the stack.

编译器一旦遇到函数调用,就会将其压入堆栈。

In the case of nested functions, the inner functions get executed before the outer functions. This is totally managed by stacks.

对于嵌套函数,内部函数先于外部函数执行。 这完全由堆栈管理。

The Stack is used to solve a few of the general problems like:

堆栈用于解决一些一般性问题,例如:

  1. Tower of Hanoi 河内塔
  2. N queens problemN皇后问题
  3. Infix to prefix conversion, etc.前缀转换前缀


六,结论 (6. Conclusion)

Thus, in this article, we have understood the concept of Stack data structure and its implementation using Arrays in C.

因此,在本文中,我们已经了解了堆栈数据结构的概念及其在C语言中使用数组的实现。



7.参考 (7. References)

Official Documentation of C++ Stacks

C ++堆栈的官方文档

翻译自: https://www.journaldev.com/35172/stack-in-c

c语言中堆栈

c语言中堆栈_C语言编程中的堆栈相关推荐

  1. 指针在c语言中起什么作用,编程中指针的作用是什么?

    原标题:编程中指针的作用是什么? 编程语言中,何为指针?它们都有什么用呢?其实通俗点讲,将一个椅子放进一个小房间锁上门,你若想拿出凳子,就必须有锁门的钥匙,而这钥匙就是指针. 举一个简单的例子: in ...

  2. c++和c语言的区别_C语言编程篇·····最简单的C语言程序

    C 语言的发展方向C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处 ...

  3. c语言 pow优化_C语言性能优化

    1.基本优化 (1)全局变量 全局变量绝不会位于寄存器中.使用指针或者函数调用,可以直接修改全局变量的值.因此, 编译器不能将全局变量的值缓存在寄存器中,但这在使用全局变量时便需要额外的(常常是不必要 ...

  4. c语言在线编译器_C语言和汇编语言是什么?他们之间可以有怎样的合作?为你解析...

    什么是c语言: C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言;尽管C语言提供了许 ...

  5. c语言命名规则_C语言的基本数据类型及变量

    学习目标 了解C语言的基本数据类型 了解变量的基本概念 了解变量的使用方法 了解了变量的命名方法 了解格式占位符 了解变量的输出 了解C语言程序的基本数据类型及概念的使用方法擦 在C语言编程中,系统定 ...

  6. python编程中的运算_Python编程中的四大运算法则

    接触过编程的人都知道,编程中的数学知识无处不在,通过数学建模能够解决我们实际生活中的很多问题.当然这并不是说必须要成为一名数学大神才能学编程,但掌握数学知识在编程中的表达方法却是很有必要的,今天南京小 ...

  7. c语言圆周率计算_C语言入门这一篇就够了

    c语言入门 C语言一经出现就以其功能丰富.表达能力强.灵活方便.应用面广等特点迅速在全世界普及和推广.C语言不但执行效率高而且可移植性好,可以用来开发应用软件.驱动.操作系统等.C语言也是其它众多高级 ...

  8. c语言sort函数_C语言经典面试题目及答案详解(二)

    接着上次来说,C语言经典面试题目及答案详解(一)当中大部分是一些概念和理解的东西 ,今天说一说实践操作,有关c的经典程序. 1.输出9*9口诀.共9行9列,i控制行,j控制列. #include 2. ...

  9. java和c语言的区别_C语言为何不会过时?你需要掌握多少种语言?

    关注.星标公众号,不错过精彩内容 整理/排版:付斌 转自:嵌入式ARM 01 为什么C语言不会过时 评价任何一门编程语言,都是招人骂的.永远是这样.就像是春寒料峭的季节, 街上穿棉袄和穿单衣的擦肩而过 ...

  10. c语言求素数_C语言 | 求100~200的素数

    "要成为绝世高手,并非一朝一夕,除非是天生武学奇才,但是这种人-万中无一" --包租婆这道理放在C语言学习上也一并受用.在编程方面有着天赋异禀的人毕竟是少数,我们大多数人想要从C语 ...

最新文章

  1. 各种优化算法公式快速回忆优化器-深度学习
  2. 学好C++,一个项目就够
  3. Andriod Studio 解决问题 Failed to resolve: com.android.support:appcompat-v7:28.+
  4. tkinter使用MySQL存数据_我无法从tkinter表单向mysql插入数据
  5. 一个小栗子聊聊JAVA泛型基础
  6. 2020蓝桥杯省赛---java---B---6(分类计数)
  7. 以太网 数据包速率计算方法
  8. 4.1-大秦立国-ip演变
  9. 在Linux中对硬盘进行分区、格式化和挂载
  10. 高频Linux命令小结(新手向)
  11. 使用Entity Framework Core,Swagger和Postman创建ASP.NET Core Web API的分步指南
  12. python中最基本的系列_Python中最基本的10个内容
  13. javascript 基础之手机端相关事件-touch(详细篇)(1)
  14. javacv 人脸追踪_JavaCV开发详解之5:基于 JavaCV 的人脸识别
  15. KK课表抓取教务系统
  16. Unity3D笔记第十六天——Mecanim动画系统
  17. layui使用formselect4完成的下拉框多选,拼音搜索
  18. Python第三课小节
  19. ECharts: 绘制立体柱状图【圆柱体】
  20. 图片加载 二维码 解析

热门文章

  1. 我的网站被黑了,关键词被劫持,总结一下是怎么解决的。
  2. (转)关于中国的互联网
  3. [转载] python实现三角形面积计算
  4. [转载] Python连接MySQL、Mongodb、SQLite
  5. [转载] python 调用自己的方法报错,numpy.ndarray object has no attribute brighten
  6. [转载] 使用 Python 实现鼠标键盘自动化
  7. 《Java深入解析》阅读笔记二(运算符与表达式)
  8. 第二周四则运算汇报及总结
  9. 阿里巴巴矢量图标库(Iconfont)-利于UI和前端的搭配
  10. SpringCloud学习指南【更新】