Home > Backend Development > C++ > body text

Explain the stack concept in C language

王林
Release: 2023-09-15 16:01:01
forward
684 people have browsed it

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 --
Copy after login

  • pop() ,pop(),pop(), pop()
  • Deleted element = 40
    Deleted element=30
    Deleted element=20
    Deleted element =10
    Copy after login
  • 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”);
    Copy after login
    Otherwise, insert an element into the stack.
  • top ++
    a[top] = item
    Copy after login
Pop ( )

    Check for stack underflow.
  • if ( top = = -1)
    printf( "stack under flow”);
    Copy after login
    Otherwise, remove the element from the stack.
  • item = a[top]
    top --
    Copy after login
Display ( )

    Check the stack flow.
  • if (top == -1)
    printf ("stack is empty”);
    Copy after login
    Otherwise, follow the algorithm mentioned below −
  • for (i=0; i<top; i++)
    printf ("%d&rdquo;, a[i]);
    Copy after login
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]);
   }
}
Copy after login
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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template