• 6 days ago
Transcript
00:00Now Q, when we create array, the first array is fixed size.
00:05So, here is the constraint that we have to implement the stack.
00:09So, just n size can be used to be stored in the array.
00:14This is the drawback.
00:16This is the drawback to the array.
00:18One more is the implementation of the array.
00:22Which is the remove operation.
00:25The time complexity is big of n.
00:29This is the drawback to the array.
00:31So, we have to do the practical cases.
00:34But, we have to do the circular queue.
00:39We have to do the circular queue.
00:40We have to do the circular queue.
00:42So, we have to do the inefficient queue.
00:44For kids.
00:46It's a simple normal queue.
00:48This is the help of time complexity.
00:53We have to do the whole thing.
00:55This is the main function.
00:56We have to do the main function.
01:20के अंदर int n ले लेते हैं और यह int n क्या होगा यह होगा हमारा
01:26array का size तो array बन जाएगा new int of n अब इस size को हम कहीं store
01:32भी करा लेते हैं सबसे पहले तो इस array को static बनाते हैं साथ के
01:37साथ एक और variable लेते हैं static int size और इस size को initialize करते
01:45है n के साथ इसके साथ हमें एक और variable की जरुवत पड़ेगी array में एक
01:50तो होता है हमारा रियर यानि पीछे वाला पार्ट एक होता है हमारा
01:54front यानि शुरुवात वाला pointer अब array से जब भी q को हम implement कर रहे हैं तो
01:59पता हमेशा क्या होगा हमारा 0-it index लेकिन रियर हमें नहीं पता क्योंकि रियर
02:04change होता रहेगा जैसे-जैसे elements add होंगे अगर यहां पर array का 0-it index है
02:09सिर्वात में रियर हमारा यहां पर है फिर वो पीछे-पीछे-पीछे-पीछे होता जाएगा जैसे-जैसे
02:14हमारे array के अंदर elements add होंगे तो इसलिए एक static int रियर ले ले लेते हैं
02:21इस रियर को हम initialize करेंगे minus 1 के साथ
02:26अब क्यों के लिए हमें तीन major functions को implement करना है यानि add, remove और peak
02:32अब इन तीनों को करने से पहले एक और छोटा सा function implement कर लेते हैं जो है is empty
02:36इस empty वैसा ही होगा जैसे हमने stack में किया था public static boolean is empty
02:46यह boolean value represent कर रही है कि true या false return करेगा
02:49हम return कर देते हैं यहां से rear equals to equals to minus 1
02:54यानि rear की value minus 1 है जैसे हमने उसको initialize किया था
02:58तो मतलब हमारी q में एक भी element stored नहीं है और q हमारी empty है
03:02तो rear minus 1 है तो return हो जाएगा true नहीं तो return होगा false
03:07अब लिखते हैं अपना add function
03:09add function को लिखने के लिए public static void add
03:16इसके अंदर data को parameter में ले लेते हैं
03:19सबसे पहले check कर लेंगे क्या हमारी q full हो गई है
03:23full हो गई है कि condition कैसे check होगी
03:25अगर हमारे rear की value हमारे size minus 1 के equal हो गई है
03:30size minus 1 यानि n minus 1, n minus 1 यानि last index
03:34rear अगर add होते होते last index तक पहुँच गया है
03:37यानि उसके बाद तो कुछ add ही नहीं हो सकता
03:39इसका मतलब q हमारी भर गई है
03:41उस case में यहाँ simply print करवा देंगे
03:44q is full और यहाँ से return करवा देंगे
03:49अपने control को
03:52अब इसके साथ next चीज क्या कर सकते है
03:54next चीज है actually उसको add कर देना
03:57तो rear को बना देंगे rear plus 1
04:00अगर पीछे array के अंदर खाली जगे है तो
04:03और array of rear को initialize कर देंगे हम data के साथ
04:09तो कुछ इस तरीके से हमने add को complete कर लिया है
04:12अब एक बार थोड़ा सा और समझ लेते हैं कि
04:15इसका मतलब actually हुआ क्या
04:16हमने शुरुआत में एक array लिया
04:19जिसका हमारे पास fixed size है
04:21इस array के अंदर कुछ-कुछ-कुछ-कुछ
04:23elements के लिए जगए होगी
04:24इसका size है हमारे पास n
04:26शुरुआत में जो हमारा rear है वो यहाँ पर होगा
04:29यानि minus 1 index पर होगा
04:31और हमारा front हमेशा यहाँ पर होगा
04:34यानि 0 at index पर
04:36अब अगर हमें पहले element को add करना होगा
04:39यहाँ पर पहले element को कैसे add करेंगे
04:41rear को कर देंगे plus plus
04:43यानि उसको minus 1 से
04:450 पर लेकर जाएंगे और 1 को
04:47यहाँ पर add कर देंगे
04:48उसके बाद दुबारा add करना होगा तो
04:50rear को एक और बढ़ाएंगे
04:53यानि 0 से उसे
04:551 पर लेकर जाएंगे और 2 को
04:56यहाँ पर add करेंगे
04:58If we add another element, then we add another one to two, and we add our element to add our
05:05element.
05:06This way, our rear is 1-1-1-1-1, and our front is fixed.
05:11Our front is 0th index.
05:14Here is our front.
05:16So, this way, we add our array.
05:20Next function, which we write, is our remove function.
05:25Public, static, void, remove.
05:30Here we go to void, return type, because we remove the element to return.
05:37Remove, which way we do?
05:39First, check if our array is empty.
05:43If our empty is true, then we print.
05:47We print.
05:50Empty.
05:51Q.
05:52And here we return, minus 1.
05:54The negative number, this symbolize.
05:56Here we have no element stored.
05:58If we store a, from a, from a, from a, from a, from a, from a, from a, from a,
06:04from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:05from a.
06:05So, this is a, from a, from a, from a, from a, from a, from a, from a, from a,
06:09from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:09from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:21from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:26from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:35from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:35from a, from a, from a, from a, from a, from a, from a, from a, from a, from a,
06:35from a, from a, from a, from a
06:35The first index was the 0th and the second was the 0th and the second was the 0th.
06:40The front is always the 0th index, but the other elements will shift.
06:45Let's put a loop.
06:47For int i equals 0, i less than rear i++.
06:54Array of i will be i plus 1.
07:00Let's see how we remove here.
07:02We have some elements that we have.
07:06This was our front and this was our rear.
07:11What do we remove?
07:14First of all, we have a variable in front of our value store.
07:181 is equal to array of 0.
07:22We will shift all elements to 1 and 2.
07:25This 1 is 1 and 2.
07:28This 2 is 3.
07:29This 3 is equal to 1 and 3.
07:32This 3 is equal to 1.
07:33This is the last step.
07:34This is the rear.
07:37This is the rear.
07:39This is the rear.
07:40This is the rear point.
07:42This position.
07:44This is the main point.
07:45This is the same number of cells.
07:52This is the same number of cells.
07:54This is the same number of cells.
08:13हमने code के अंदर लिखा है यहाँ पर हमने rear को
08:15minus minus नहीं किया तो rear हमारा
08:17बन जाएगा rear minus
08:191 अब next function की बात करें
08:21तो next function होगा हमारा peak function
08:23peak function में
08:25हम लिखेंगे public static
08:28int peak
08:31और
08:31यहाँ पर if empty वाली
08:33condition को as it is रख देंगे
08:35और return कर देंगे
08:37अपने array of 0 को
08:39तो array of 0 हमारा front है उसको हमने
08:41directly return कर दिया
08:45तो इसको save कर लेते हैं
08:47इस तरीके से हमने अपनी q तो बना लिये
08:49अब इसको main function के अंदर use भी कर लेते हैं
08:52तो q का object बनाएंगे q
08:53is equal to new q
08:55और इसके अंदर pass करेंगे अपने array का
08:57size हमारे array का size क्या होने वाला है
08:59पांच इसको शुरुवात में रख लेते हैं
09:01q के अंदर कुछ elements को add करके देखते हैं
09:04q.add2
09:05q.add3
09:09अब एक loop लगाते हैं जब तक हमारी
09:11q खाली नहीं हो जाती
09:13तब तक हम अपने elements को
09:15peak करना जाते हैं तो q.peak
09:17और एक बार peak कर लिया तो उसको
09:19q से कर देंगे remove
09:32run करके हमारे पास
09:33जो output आएगा that is 1, 2
09:35and 3
09:371, 2 and 3 यानी जिस
09:39order में अंदर गए थे उसी order में बाहर आए
09:41अगर ये stack होता तो
09:43order opposite हो जाता यानी
09:453, 2, 1 ये print होता
09:47लेकिन 1, 2, 3 प्रिंट हुआ है
09:49और add भी पहले 1 हुआ था तो पहले 1
09:512 गया था तो फिर 2 निकला
09:543 गया था last में
09:55तो last में 3 निकला
09:56इस तरीके से q हमारी stack से थोड़ी सी
09:59अलग होती है तो ऐसे सवाल जिन
10:01में आपको पहले जब आप
10:03अपने element को भेजें तो वही element
10:05पहले process होना चाहिए और जिन
10:07cases में हमारा पहले जाने वाला
10:09element हो last में process हो
10:11उन cases में हम stack को use करते हैं
10:13अब हमने nsq को implement कर लिया है
10:15लेकिन हम time complexity की बात करें
10:17तो हमें add में लगी
10:19big of one time complexity
10:21लेकिन remove के अंदर हमें लगी
10:23time complexity big of n
10:25ऐसा क्यों हुआ? क्योंकि सारे elements को
10:27एक-इक करके हमें आगे की तरफ copy करना पड़ा
10:29जब हमने front से element को
10:31हटाया तो front हमारा जिरुएत index ही लए
10:33इसलिए हमने सारे elements को आगे
10:35push कर दिया, इस पूरे process में
10:37हमें big of n यानि linear time
10:39लगा, इसलिए कभी array से
10:41हम अपने Q को practically implement नहीं
10:43करते हैं