how many type of Implementation of stack using C in dataStructure developerIndian.com

Updated:13/08/2023 by Computer Hope

Go Back

There are two main types of stack implementations in C within data structures:
  • 1. Array-based Implementation:

  • Uses a static array to represent the stack.
    Operations like push(), pop(), and peek() are performed by manipulating the top pointer.
    Fixed size, so it is prone to stack overflow when full.
    Simple and fast, but limited in size.

  • 2. Linked List-based Implementation:

  • Uses dynamic memory allocation to create nodes.
    Each node contains data and a pointer to the next node.
    More flexible as size grows dynamically, avoiding stack overflow.
    Slightly more complex due to pointer management.

Each type has its use case depending on flexibility and memory constraints.



A stack is a linear data structure in which the insertion or add of a new element and removal or delete of an existing element takes place at the same end represented as the top of the stack.

Want to learn how many way we can implement stack in c


              
#include<stdio.h>
# define MAX 10
int stack [MAX], top = -1;
void  push (int  val)
  {   
   if (top == MAX -1)
       {	 
           printf("\noverflow");
	 return ;  }
   stack [++top]=val;
   }
int  pop( )
  {  
   if(top==-1)
       {  
        printf("\nunderflow");
        return -999;  }
  return stack[top--];
  }

void show ( )
   {  int i;
      if(top==-1)
        {
         printf ("\nstack is empty");
         return;  }
        for (i=top; i>=0; i--)
         printf("   %d", stack[i]);
   }




void main ()
    { int no, ch;
       do
       {	printf ("\n 1 push");
	printf ("\n 2 pop");
	printf ("\n 3 show");
	printf ("\n 0 exit");
	printf ("\n enter ur your ");
	scanf ("%d", &ch);


	switch (ch)
	 {case 1 : printf("\n Enter no : ");
		  scanf("%d", &no);
		  push(no);
		  break;
	  case 2 : no = pop( );
		  if (no != -999)
		     printf ("\n % d poped ",no);
		  break;
	  case 3 : show();
		  break;
	  case 0 : break;
	  default : printf("\n invalid choice");
	  }//end of switch
	  }while ( ch !=  0);//end of while
      }


            

Conclusion

In this article , implementation of stack and how to classify stack .first one is static and second is dynamic .Each type has its use case depending on flexibility and memory constraints.