Stack : Implementation Using Linked List in c language in hindi , स्टैक को लिंक लिस्ट का उपयोग करने इम्प्लीमेंट करना

स्टैक को लिंक लिस्ट का उपयोग करने इम्प्लीमेंट करना , Stack : Implementation Using Linked List in c language in hindi :-

Stack को दो प्रकार से Implementation कर सकते है (i) using array (ii) using linked list |linked लिस्ट की तुलना मे array मे push (insertion ) और pop (delete) operation को करना easy होता है |लेकिन array के साथ dynamic memory एलोकेशन possible नहीं है | इस कमी को दूर करने के लिए stack को linked लिस्ट से
Implementation  किया जाता है | linked लिस्ट run time पर memory allocate करती है |इसलिए stack Implementation  using linked list ही सबसे ज्यादा अपनाया जाता है |

push operation :
push operation की algorithm है :-1. सबसे पहले एक नये node new 1 को बनाया जाता है जिसमे data value को assign होती है |
2.stack की emptyness को check किया जाता है |अगर top == null से check किया जाता है |
3.अगर stack empty है तब new node के address field को null से set करते है |
4.अगर stack empty नहीं होती है तब new के address field को top से set कर देते है |
5.उसके बाद top को new से set कर देगे |Pop operation

pop operation की algorithm है :-
1.सबसे पहले stack की emptyness को check किया जाता है |अगर stack empty है तब empty stack का message आ जायेगा |

2.अगर stack empty नहीं होती तब node pointer temp को define करेगे |temp को top से set करेगे |
3.उसके बाद top को top के address field से set करेगे |
4.top के द्वारा allocate space को free कर देते है |Source Code
#include<stdio.h>
#include<conio.h>
struct employee
{
   int data;
   struct employee *next;
}*head  = NULL;
void push(int number);
void pop();
void display_stack();
void main()
{
   int ch, new;
   clrscr();
   while(1){
      printf(“\n\t Enter ‘1’ for push operation “);
      printf(“\n\t Enter ‘2’ for pop operation “);
      printf(“\n\t Enter ‘3’ for display operation “);
      printf(“\n\t Enter ‘4’ for exit “);
      pritnf(“Enter your choice”);
      scanf(“%d”,&ch);
      switch(ch){
case 1: printf(“Enter the data that you want to insert: “);
scanf(“%d”, &new);
push(new);
break;
case 2: pop();
     break;
case 3: display_stack();
     break;
case 4: exit(0);
default: printf(“\nWrong choice”);
          printf(“Enter a valid choice among ‘1’,’2′,’3′ or ‘4’.”)
      }
   }
}
void push(int new)
{
   struct employee *newdata;
   newdata = (struct employee*)malloc(sizeof(struct employee));
   newdata->data = new;
   if(head  == NULL)
      newdata->next = NULL;
   else
      newdata->next = head ;
   head  = newdata;
   printf(“\n data inserted \n”);
}
void pop()
{
   if(head  == NULL)
      printf(“\n Empty Stack \n”);
   else{
      struct employee *temp = head ;
      printf(“\n Deleted element: %d”, temp->data);
      head  = temp->next;
      free(temp);
   }
}
void display_stack()
{
   if(head  == NULL)
      printf(“\nStack is Empty!!!\n”);
   else{
      struct employee *temp = head ;
      while(temp->next != NULL){
printf(“%d—>”,temp->data);
temp = temp -> next;
      }
      printf(“%d—>NULL”,temp->data);
   }
}

इस प्रोग्राम मे , char function है :

1.main() : इस function मे , प्रोग्राम की main body होती है |जिसमे सभी function push() ,pop() और display_stack () को call किया जाता है |

2.push() : इस function का use ,stack array मे element को add करने के लिए किया जाता है |

3.pop() : इस function का use , stack element मे element को डिलीट करने के लिए किया जाता है |

4.display_stack() : इस function का use ,stack के सभी element को display करने के लिए किया जाता है |

सबसे पहले ,header portion मे एक structure को declare किया गया है | जो की linked लिस्ट एक node को define करता है | इसका एक variable है जिसका नाम head है |

main() मे ,

इस function मे ,

1.सबसे पहले दो variable ch , और new को declare किया गया है | (i) ch मे , यूजर द्वारा दिए गये choice को assign किया जाता है |और (ii) new मे , यूजर द्वारा दिए गये नए data को assign किया जाता है |

2.उसके बाद यूजर से choice के लिए input लिया जाता है जिससे कोनसा function perform होगा |ये decide होता है |जैसे’1′ से push operation होगा |

‘2’ से pop operation होगा |

‘3’ से display operation होगा |

‘4’ से exit operation होगा |

यूजर द्वारा दिए गये input के आधार पर हो function को call किया जाता है |

push operation :
1.सबसे पहले एक variable newdata को declare किया जाता है जिसका data type struture employee है|

2.फिर उसके बाद dynamic memory allocation से ,एक memory block को बनाया जाता है जिसका address variable newdata मे store किया जाता है |
3.newdata के item field मे function मे data (जो की formal parameter मे received हुई) को assign किया जाता है |
4.अगर top null है तब
4.1-newdata के address field null से set हो जाता है |
4.2 अन्यथा newdata का address feild मे top का address set हो जाता है |और newdata ,top बन जाता है |
pop operation 1.सबसे पहले top को check किया जाता है |अगर top की value null होती है तब
1.i-stack empty का message आ जाता है |
1.ii अन्यथा temp variable को top से set हो जाता है |और डिलीट हुए data को print किया जाता है |
2.top को temp के address field से set करेगे |
3,temp के लिए allocate space को डिलीट कर देगे |display operation
1.सबसे पहले top को null से check किया जाता है |अगर top की value null होती तब empty stack का message आ जाता है |
अन्यथा
2.एक नया pointer variable को define किया जाता है जिसका नाम temp होता है |
3.temp को top से initial किया जाता है |
4.उसके बाद एक loop चलाया जाता है |loop की body मे ,
4.i-temp की item field मे store data को print करा देते है |
4.ii-temp को temp के address field मे store address से increment कर देगे |
ये loop जब तक चलेगा जब तक temp के address field की value null नहीं हो जाती है |
इसके आगे के article मे , searching और sorting operation को पढेगे |