Home  >  Article  >  Backend Development  >  Explain the stack concept in C language

Explain the stack concept in C language

王林
王林forward
2023-09-15 16:01:01637browse

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.

Explain the stack concept in C language

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.

Stack in C language

It is a linear data structure, data can only be inserted and deleted at one end.

Operation

  • Push - inserts an element into the stack.
  • Pop - Removes an element from the stack.

Explain the stack concept in C language

Explain the stack concept in C language

Explain the stack concept in C language

Explain the stack concept in C language

Explain the stack concept in C language

Explain the stack concept in C language#

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 Overflow

Conditions

  • Stack Overflow - Attempt to insert an element into the full stack.

  • Stack underflow - An attempt is made to remove an element from an empty stack.

Push ( ), Pop ( ), Display ( ) algorithm

The corresponding algorithm is as follows:

Push ( )

    Check if the stack overflows.
  • 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, remove the element from the stack.
  • item = a[top]
    top --
Display ( )

    Check the stack flow.
  • if (top == -1)
    printf ("stack is empty”);
    Otherwise, follow the algorithm mentioned below −
  • for (i=0; i<top; i++)
    printf ("%d&rdquo;, a[i]);
Example

The following is a C program to implement stack using array :

#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&rdquo;);
   scanf ("%d&rdquo;, &n);
   printf("stack implementation</p><p>&rdquo;);
   printf ("1. push </p><p>&rdquo;);
   printf ("2. Pop </p><p>&rdquo;);
   printf ("3. exit </p><p>&rdquo;);
   do{
      printf ( "enter ur choice&rdquo;);
      scanf ("%d&rdquo;, &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&rdquo;)
   else{
      printf("enter an element for insertion&rdquo;)
      scanf ("%d&rdquo;, &item);
      top ++;
      a[top] = item;
   }
}
void pop ( ){
   int item;
   if (top = = -1);
      printf ( "stack under flow&rdquo;);
   else{
      item = a[top];
      top --;
      printf("deleted element = %d&rdquo;, item);
   }
}
void display ( ){
   int i;
   if (top = = -1)
      printf ( "stack is empty&rdquo;);
   else{
      printf("contents of the stack are&rdquo;);
      for (i=0; i<top; i++)
         printf ("%d \t&rdquo;, 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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete