
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the concept of stack in C language
Data structure is a collection of data organised in a structured way. It is classified into two types which are linear data structure and non-linear data structure.
Linear data structure − Here, data is organised in a linear fashion.
For example − Arrays, structures, stacks, queues, linked lists.
Non-linear data structure − Here, Data is organised in a hierarchical way.
For example − Trees, graphs, sets, tables.
Stack in C language
It is a linear data structure, where data is inserted and removed only at one end.
Operations
- Push – Inserting an element into a stack.
- Pop – Deleting an element from a stack.
Deleted element = 50 Item = a [top] top --
- pop() ,pop(),pop(), pop()
Deleted element = 40 Deleted element=30 Deleted element=20 Deleted element =10
- Pop ( )
Stack under flow
Conditions
Stack over flow − Trying to insert an element into a full stack.
Stack under flow − Try to delete an element from a stack which is empty.
Algorithms for Push ( ), Pop ( ) , Display ( )
The respective algorithms are as follows −
Push ( )
- Check for stack overflow.
if (top = = n-1) printf("stack over flow”);
- Otherwise, insert an element into the stack.
top ++ a[top] = item
Pop ( )
- Check for stack underflow.
if ( top = = -1) printf( "stack under flow”);
- Otherwise, delete the element from the stack.
item = a[top] top --
Display ( )
- Check for stack flow.
if (top == -1) printf ("stack is empty”);
- Otherwise, follow the below mentioned algorithm −
for (i=0; i<top; i++) printf ("%d”, a[i]);
Example
Following is the C program for implementation of stack by using the arrays −
#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
”); 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<top; i++) printf ("%d \t”, a[i]); } }
Output
When the above program is executed, it produces the following result −
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
- Related Articles
- Explain the concept of pointers in C language
- Explain the concept of Sorting in C language
- Explain the concept of pointer accessing in C language
- Explain the concept of Arithmetic operators in C language
- Explain the concept of Linked list in C language
- Explain the concept of union of structures in C language
- Explain the concept of Uninitialized array accessing in C language
- Explain the concept of logical and assignment operator in C language
- Explain the stack by using linked list in C language
- Explain the concept of pointer to pointer and void pointer in C language?
- Explain bit field in C language by using structure concept
- Explain the concept of one and two dimensional array processing using C language
- Demonstrate the concept of pointers using C language
- Explain the concept of delegates in C#
- Explain the concept of a class in C#
