解释C语言中的堆栈概念

数据结构是以结构化方式组织的数据集合。它分为两种类型,即线性数据结构和非线性数据结构。

解释C语言中的堆栈概念

线性数据结构 – 在这里,数据以线性方式组织。

例如 – 数组、结构体、栈、队列、链表。

非线性数据结构 – 在这里,数据以层次结构方式组织。

立即学习“C语言免费学习笔记(深入)”;

例如 – 树、图、集合、表格。

C语言中的栈

它是一种线性数据结构,数据只能在一端插入和删除。

操作

Push – 将元素插入栈中。Pop – 从栈中删除元素。

解释C语言中的堆栈概念

解释C语言中的堆栈概念

解释C语言中的堆栈概念

解释C语言中的堆栈概念

解释C语言中的堆栈概念

解释C语言中的堆栈概念

Deleted element = 50Item = a [top]top --

登录后复制pop() ,pop(),pop(), pop()

Deleted element = 40Deleted element=30Deleted element=20Deleted element =10

登录后复制Pop ( )

堆栈溢出

条件

堆栈溢出 – 尝试向满栈插入元素。

堆栈下溢 – 尝试从空栈中删除元素。

Push ( ),Pop ( ),Display ( )的算法

相应的算法如下:

Push ( )

检查堆栈是否溢出。

if (top = = n-1)printf("stack over flow”);

登录后复制否则,将一个元素插入到堆栈中。

top ++a[top] = item

登录后复制

Pop ( )

检查堆栈下溢。

if ( top = = -1)printf( "stack under flow”);

登录后复制否则,从堆栈中删除该元素。

item = a[top]top --

登录后复制

Display ( )

检查堆栈流程。

if (top == -1)printf ("stack is empty”);

登录后复制否则,按照下面提到的算法进行操作 −

for (i=0; i

示例

以下是使用数组实现堆栈的C程序:

#include#include int top = -1, n,a[100];main ( ){   int ch;   void pop ( );   void display ( );   clrscr ( );   printf ("enter the size of the stack”);   scanf ("%d”, &n);   printf("stack implementation

”);   printf ("1. push

”);   printf ("2. Pop

”);   printf ("3. exit

”);   do{      printf ( "enter ur choice”);      scanf ("%d”, &ch);      switch (ch){         case 1 : push ( );         display ( );         break;      case 2 : push ( );         display ( );         break;      case 3 : exit   }   }while (ch>=1 | | ch

输出

当执行上述程序时,它会产生以下结果 −

enter the size of the stack = 5 [given by user]Stack implementation1. Push 2. Pop 3. exitEnter ur choice : 1 [given by user]Enter an element for insertion : 10Contents of the stack : 10Enter ur choice : 1Enter an element for insertion : 2Contents of the stack : 10 20Enter ur choice : 2Deleted element = 20Contents of the stack are : 10Enter ur choice : 2Deleted element : 10Contents of the stack are : stack is emptyEnter ur choice : 2Stack underflow.Enter ur choice : 1Enter an element for insertion : 30Contents of the stack are : 30

登录后复制

以上就是解释C语言中的堆栈概念的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2581559.html

(0)
上一篇 2025年3月6日 13:59:59
下一篇 2025年3月6日 14:00:06

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论