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

线性数据结构 - 在这里,数据以线性方式组织。
例如 - 数组、结构体、栈、队列、链表。
非线性数据结构 - 在这里,数据以层次结构方式组织。
立即学习“C语言免费学习笔记(深入)”;
例如 - 树、图、集合、表格。
C语言中的栈
它是一种线性数据结构,数据只能在一端插入和删除。
操作
- Push - 将元素插入栈中。
- Pop - 从栈中删除元素。



Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。作为命令语言,它交互式地解释和执行用户输入的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。它虽然不是Linux系统核心的一部分,但它调用了系统核心的大部分功能来执行程序、建立文件并以并行的方式协调各个程序的运行。因此,对于用户来说,shell是最重要的实用程序,深入了解和熟练掌握shell的特性极其使用方法,是用好Linux系统



Deleted element = 50 Item = a [top] top --
- pop() ,pop(),pop(), pop()
Deleted element = 40 Deleted element=30 Deleted element=20 Deleted 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<= 3); getch ( ); } void push ( ){ int item; if (top = = n-1) printf ( "stack over flow”) else{ printf("enter an element for insertion”) scanf ("%d”, &item); top ++; a[top] = item; } } void pop ( ){ int item; if (top = = -1); printf ( "stack under flow”); else{ item = a[top]; top --; printf("deleted element = %d”, item); } } void display ( ){ int i; if (top = = -1) printf ( "stack is empty”); else{ printf("contents of the stack are”); for (i=0; i
输出
当执行上述程序时,它会产生以下结果 −
enter the size of the stack = 5 [given by user] Stack implementation 1. Push 2. Pop 3. exit Enter ur choice : 1 [given by user] Enter an element for insertion : 10 Contents of the stack : 10 Enter ur choice : 1 Enter an element for insertion : 2 Contents of the stack : 10 20 Enter ur choice : 2 Deleted element = 20 Contents of the stack are : 10 Enter ur choice : 2 Deleted element : 10 Contents of the stack are : stack is empty Enter ur choice : 2 Stack underflow. Enter ur choice : 1 Enter an element for insertion : 30 Contents of the stack are : 30










