Definition of keywords of stack
जब Stack का प्रयोग किया जा सकता है। इसके लिए Stack पर किए जाने वाले ऑपरेशन हमें समझने होंगे। इन ऑपरेशन को समझने के लिए निम्नांकित शब्दों का ज्ञान होना आवश्यक हैं:
- Push (पुश)
- Pop (पॉप)
- Peek (पीक)
- Top (टॉप)
- MAXSIZE (मैक्ससाइज़)
- Overflow (ओवेरफ्लो)
- Underflow (अन्डरफ्लो)
- Traversal (ट्रावर्ज़ल )
Push:-
Stack (स्टैक) में जब कोई नया Element (एलीमेंट) जोड़ा जाता है तो उस ऑपरेशन को Push कहा जाता है। Stack (स्टैक) में Push (पुश) किया गया एलीमेंट हमेशा top (अंत) में ही जुड़ता है।
Pop:-
Stack (स्टैक) से जब कोई Stack (स्टैक) Delete (हटाया) किया जाता है तो उस ऑपरेशन को Pop (पॉप) कहा जाता है। Stack (स्टैक) से Pop (पॉप) किया गया एलीमेंट हमेशा अंत से ही Delete (हटाया) किया जाता है। सामान्यतया एलीमेंट को डिलीट करने से पूर्व उसे प्रयोग में ले लिया जाता है।
Peek:-
Stack (स्टैक) से जब कोई एलीमेंट Use तो किया जाता है किंतु डिलीट नहीं किया जाता है तो उस ऑपरेशन को Peek (पीक) कहा जाता है। यह ऑपरेशन भी Pop (पॉप) की जैसे ही स्टैक के अंत पर ही किया जाता है।
Top:-
Stack (स्टैक) पर Operation करते समय यह पता होना चाहिए कि Push / Pop (पुश / पॉप) करते समय कौनसा Element प्रयोग में आएगा। सामान्यतः यह कार्य top नाम के एक वेरिएबल के माध्यम से किया जाता है। इस प्रकार top ऐसा Pointer होता है जो स्टैक के अंतिम (top) एलीमेंट को दर्शाता है।
MAXSIZE:-
जब Stack (स्टैक) को एरे में माध्यम से प्रयोग में लिया जाता है तो यह बताना होता है तो कि Array Stack (स्टैक) में अधिकतम कितने एलीमेंट स्टोर किए जा सकेंगे। सामान्यतः यह कार्य MAXSIZE (मैक्ससाइज़) नाम के वेरिएबल के माध्यम से किया जाता है।
Overflow:-
जब Stack (स्टैक) पूरा भर जाता है तो इस Condition (कंडीशन) को Overflow (ओवरफ्लो) कहा जाता है। अन्य शब्दों में जब Top वेरिएबल की वैल्यू MAXSIZE की वैल्यू के बराबर हो जाती है तथा यूज़र नया एलीमेंट पुश करने कोशिश करता है तो ऐसी कंडीशन को Overflow (ओवरफ्लो) कहा जाता है। ऐसी स्थिति में User सिर्फ एलीमेंट्स को Delete कर सकता है, किंतु नया एलीमेंट जोड़ नहीं सकता।
Underflow:-
जब Stack (स्टैक) खाली होता है तथा यूज़र किसी एलीमेंट को Delete करने की कोशिश करता है तो ऐसी स्थिति को Underflow (अंडरफ्लो) कहा जाता है। ऐसी स्थिति में User सिर्फ नया एलीमेंट जोड सकता है, किंतु Pop नहीं कर सकता है। ऐसी स्थिति में Top वेरिएबल की वैल्यू को 1 पर रखा जाता है।
Traversal:-
Traversal (ट्रावर्ज़ल) शब्द Travel (ट्रेवल) शब्द से बना है, जिसका अर्थ होता है घूमना। Stack (स्टैक) पर Traversal (ट्रावर्ज़ल) Operation से आशय Stack (स्टैक) के सभी एलीमेंट्स को Visite करना । विज़िट करते समय प्रत्येक एलीमेंट को प्रिंट करवाया जा सकता है या कोई अन्य ऑपरेशन किया जा सकता है।
Representation as an Array
Stack (स्टैक) एक linear data structure है जो (Last-In-First-Out) LIFO के सिद्धांत का पालन करता है । Stack (स्टैक) में एक छोर होता है जिसके माध्यम से insertion and deletion होता है। जब भी Stack (स्टैक) में कोई Element insert किया जाता है, या Element को केवल स्टैक से delete जाता है, तो इसे स्टैक के Top (लास्ट) में insert किया जा सकता है। दूसरे शब्दों में, एक Stack (स्टैक) को एक Container के रूप में परिभाषित किया जा सकता है जिसमें एक छोर से insertion and deletion किया जा सकता है जिसे स्टैक के Top के रूप में जाना जाता है।
Push Operation (Adding an element onto the stack)
Stack के Top में एक Element जोड़ना Push Operation के रूप में जाना जाता है। Push Operation (पुश ऑपरेशन) में निम्नलिखित दो चरण शामिल हैं।
- Variable Top को Increment ताकि यह अब अगले memory location पर refere कर सके।
- incremente किए गए Top variable की position में Element जोड़ें। इसे Stack के Top पर new Element जोड़ने के रूप में जाना जाता है।
जब stack पूरा भरा हो (Top == MAXSIZE) और हम उस stack में एक element Add करने का प्रयास करते हैं तो स्टैक Overflow (ओवरफ्लो) हो जाता है, इसलिए हमारे main function को हमेशा stack overflow स्थिति से बचना चाहिए।
Algorithm
Algorithm for Push value in the stack
ST[] : Array for Stack
MAXSIZE: Size of the Array
Top: The last index of the Array
Val: value to be pushed
push(ST[], MAXSIZE, Top, Val)
Step 1 | // checking, overflow condition if (Top = MAXSIZE - 1) then print "Np More space (Overflow) in the Array" return end if |
Step 2 | // increment top by one Top = Top + 1 |
Step 3 | ST[Top] = Val |
Step 4 | return |
Short:-
Time Complexity: o(1)
implementation of push algorithm in C language
Pop operation (Deletion of an element from a stack)
Algorithm
Algorithm for pop value in the stack
ST[] : Array for Stack
MAXSIZE: Size of the Array
Top: The last index of the Array
pop(ST[], MAXSIZE, Top)
Step 1 // checking, Underflow condition if (Top = -1) then print "No More Elements (stack Underflow) in the Array" return end if Step 2 Val = St[Top] Step 3 Top = Top - 1 Step 4 return (Val)
ST[] : Array for Stack
MAXSIZE: Size of the Array
Top: The last index of the Array
pop(ST[], MAXSIZE, Top)
Step 1 | // checking, Underflow condition if (Top = -1) then print "No More Elements (stack Underflow) in the Array" return end if |
Step 2 | Val = St[Top] |
Step 3 | Top = Top - 1 |
Step 4 | return (Val) |
Short:-
Time Complexity : o(1)
ऊपर दी गई algorithum में step 1 में यह जांच की गई हैं कि कहीं stack खाली तो नहीं हैं। ऐसी स्थिति में pop operation नहीं किया जा सकेगा। ऐसी condition को underflow कहा जाता हैं
Implementation of the POP algorithm using C language
Peek operation (Visiting each element of the stack)
किए Stack (स्टैक) से जब कोई एलीमेंट Use तो किया जाता है किंतु डिलीट नहीं किया जाता है तो उस ऑपरेशन को Peek (पीक) कहा जाता है। यह ऑपरेशन भी Pop (पॉप) की जैसे ही स्टैक के अंत पर ही किया जाता है।
Algorithm
PEEK (STACK, TOP)
Time complexity: o(n)
Implementation of Peek algorithm in C language
Example #1
Example #02:-
Output:-
1. PUSH
2. PEEK
3. DISPLAY
4. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 29
1. PUSH
2. PEEK
3. DISPLAY
4. EXIT
Enter your option: 1
Enter the number to be pushed on stack: 143
1. PUSH
2. PEEK
3. DISPLAY
4. EXIT
Enter your option: 2
The value stored at top of the stack is 143
1. PUSH
2. PEEK
3. DISPLAY
4. EXIT
Enter your option : 3
143
29
17
1. PUSH
2. PEEK
3. DISPLAY
4. EXIT
Enter your option: 4
0 Comments