00:00Now we implement a stack in an array list.
00:03We will create a class which we will create a static class and the name of the stack.
00:09In this stack class, we will create an array list of integers.
00:17We will store our data in integers.
00:20The name of the list is called new array list.
00:27Now we will import a package, java.utils-array-list.
00:38Now we will create a stack for 4 functions.
00:41Push, pop, peak.
00:44This function is empty.
00:47This stack is empty or not.
00:50So, first of all, we will start.
00:53Public static boolean is empty.
01:01So, when the stack is empty, we will return to true, not return to false.
01:07So, here we will return to list.size equal to equal to 0.
01:14So, when the list size is 0, we will return to true, not return to false.
01:21Now we will implement our major function, which is our push function.
01:25For push to implement, we will write public static, void, push.
01:31Push, we will pass the data, which we will enter in array list or stack.
01:38And in list, we will add the data to .add function.
01:44Now, the list of .add function is automatically, the list of the index of the index of our data.
01:50So, automatically, the stack of the top of our data is added.
01:56Then, we will implement the pop function.
01:59Public static int pop.
02:03Now, the pop function will delete our data from our stack.
02:07But, here we have return type.
02:10Because, before the data, we will store it and return it.
02:14Whatever data is deleted, it will return to the default implementation.
02:19So, first of all, we will remove our top.
02:22Int, top.
02:24What is our array list?
02:26The last index stored value is our stack top.
02:30So, here we take list.get.
02:32Get function big of one.
02:34So, this is a single operation.
02:36So, the time complexity is big of one.
02:39And, here we will write our list list.size-1.
02:46Top, we have calculated.
02:48Now, we will use list.remove function.
02:52And, in the last index stored value, it will be remove.
02:56The last index stored value, when we do remove operation in the form,
03:01So, it will also be a big of one operation.
03:04It will also be a constant time operation.
03:06So, here we will return to our top.
03:09So, we have implemented pop.
03:11Top first, remove top.
03:13Then, top is removed.
03:14Then, top is returned.
03:17Now, we will implement the peak operation.
03:21Public static int peak.
03:25Peak goes implement it.
03:27To be implemented always by pop.
03:28We must implement it.
03:28You can make it easy to write.
03:29Because, here we have this delete route.
03:31Not only the top.
03:32We need to remove this column and return.
03:34So, this single statement.
03:36Here we will attach the top to the other job.
03:52peak को implement कर लिया, अब main function के अंदर stack बनाएंगे, stack s is equal to new stack, और
04:01इस stack के अंदर सबसे पहले
04:02एक एक करके elements को push करेंगे, तो s.push1, s.push2, s.push3, अब एक loop चलाएंगे, जब तक
04:16हमारा stack
04:17empty नहीं हो जाता, यानि जब तक is empty हमें true नहीं दे देता, यह statement true नहीं हो जाती,
04:24तब तक यह loop चलेगा, और इस loop से
04:26एक एक करके elements को peak करेंगे, अपने stack में, उनको print करवाएंगे, और फिर उसको delete कर देंगे, तो
04:33system.out.println, s.peak, और next line में s.pop,
04:43elements को जाक लेंगे, print करवादेंगे, फिर delete कर देंगे और यह तब तक करेंगे, जब तक stack हमारा खाली
04:49नहीं हो जाता, तो इसको save कर लेते हैं, इसको run कर लेते हैं,
04:56output हमारे पास जो आया है, that is 3, 2, 1, इसको एक बार देख लेते हैं, कि implement actually
05:01हुआ कैसे,
05:03जब हमने array list से stack को बनाया, तो हमने कुछ इस तरीके का array list imagine किया होगा,
05:08जिसके अंदर हमने 1, 2, 3 करके elements डाले और यह हो गया हमारा top, अब इसी को एक stack
05:13की form में visualize करें,
05:15यानि balty की form में, तो यह कुछ ऐसा होगा, जिसमें सबसे पहले आएगा 1, फिर आएगा 2, फिर आएगा
05:203, और यह बन गया हमारा top,
05:23अब जब हमने 1, 2, 3 elements डाले, तो वो बाहर आएगे लीफो fashion में, यानि उल्टे होकर, reverse होकर,
05:30तो 3 last में गया था, तो 3 सबसे पहले आएगा, सबसे पहले 3 आएगा, तो 3 आउटपुट में, फिर
05:352 आएगा, तो 2 आउटपुट में, फिर 1 आएगा, तो 1 आउटपुट में, तो हमारा जो आउटपुट होगा, वो होगा
05:403, 2, 1, which is same as जो आउटपुट हमारे पास screen पर print ह�
05:44अपने इस array list के code में, हमें corner case भी add करना है, वो ये corner case है, कि
05:50pop करते time, अगर हमारी array list खाली हुई, हमारा stack खाली हुआ, तो हमें क्या करना है, तो हम
05:55return कर देंगे simply minus 1, minus 1 यानि negative number ये दिखा रहा है, कि stack खाली है, stack
06:00के अंदर क्योंकि सिर positive numbers हम डाल सकते हैं, तो इस तरीके से implementation कर �
06:05तो अगर is empty जो है, वो हमें true return कर देगा, तो उस case में हम return कर देंगे,
06:11minus 1, जो represent करेगा कि हमारा stack खाली है, अब ये same चीज हम peak में implement कर रहे
06:19होंगे, क्योंकि peak में भी, अगर stack खाली है, तो हम कुछ भी नहीं देख पाएंगे, क्योंकि top exist ही
06:24नहीं कर दा, तो उस case में भी minus 1 को return
06:26करेंगे, तो इस तरीके से जब भी stack खाली होगा, तब हमारे पास एक condition होगी, जो implement हो सकती
06:32है,