A data structure is a collection of data organized in a structured manner. It is divided into two types namely linear data structure and non-linear data structure.
Linear Data Structure - Here, the data is organized in a linear manner.
For example - array, structure, stack, queue, linked list.
Nonlinear Data Structure - Here, the data is organized in a hierarchical manner.
For example - tree, graph, set, table.
It is a linear data structure, data can only be inserted and deleted at one end.
#
Deleted element = 50 Item = a [top] top --
Deleted element = 40 Deleted element=30 Deleted element=20 Deleted element =10
if (top = = n-1) printf("stack over flow”);
top ++ a[top] = item
if ( top = = -1) printf( "stack under flow”);
item = a[top] top --
if (top == -1) printf ("stack is empty”);
for (i=0; i<top; i++) printf ("%d”, a[i]);
#include<stdio.h> #include <conio.h> 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</p><p>”); printf ("1. push </p><p>”); printf ("2. Pop </p><p>”); printf ("3. exit </p><p>”); 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<top; i++) printf ("%d \t”, a[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
The above is the detailed content of Explain the stack concept in C language. For more information, please follow other related articles on the PHP Chinese website!