- 5 days ago
Transcript
00:00Now we will implement a stack with a link list of help.
00:04This question comes from companies, so this is a very important implementation.
00:08First of all, we will implement a stack class with an array list.
00:11We will take example of that.
00:13We will delete all the other functions.
00:17This stack class will create a stack.
00:21First, we will create another class with which we will give node.
00:26This node class will create node.
00:28This node class will be our link list of nodes.
00:31We will represent nodes from node class.
00:34We will store our int data in this node.
00:36We will create a next pointer.
00:38We will create a constructor in which we will pass every time.
00:44This.data is equal to data.
00:47This.next will be initialized with null.
00:50This is the start of node class.
00:53The stack class will create a node.
00:55This node will create a link list.
00:56This is the most important node.
00:58This is our head node.
00:59This is the static type.
01:01We will create a static type so that we can access all the functions.
01:04Static node.
01:05Head.
01:06Initialize.
01:07We will do this with null.
01:09First, we will implement all the functions.
01:12We will start with is empty.
01:13We will start with public static boolean is empty.
01:22If our head is null.
01:24If our head is null.
01:25If our head is equal to null.
01:26This means that there is no one node.
01:28In that case, our link list is empty.
01:31This is empty.
01:31We will return true.
01:33We will return false.
01:35First, we will implement the major function.
01:37Which is the push function.
01:39Public static void.
01:43Push.
01:43Push.
01:44Push.
01:44Push.
01:45Push.
01:45Push.
01:45We will return to our data.
01:46Now, when we implement a stack.
01:49Link list.
01:50Push.
01:52To replace the head.
01:54We will understand.
01:56We will understand.
01:57We have already linked list.
01:59Two nodes.
02:01Already linked list.
02:03We have stored.
02:04One.
02:05Two.
02:05Two.
02:06And this is our current head.
02:10So, when we take a new node.
02:13We will create a new node.
02:15This is our node 3.
02:17This node.
02:18We will add our stack.
02:20Now, we will know that the top of stack is linked list.
02:24This is the head.
02:25And the top of stack is the top.
02:27So, we will replace this node.
02:30We will replace this node.
02:31So, linked list inside the head.
02:36Also, the lower node will remove this node.
02:39Next is the object of a new node.
02:42So, let's pause.
02:43– Next make sure we will show you next node.
02:44Then, if the new value type, qui 것 will store next.
02:48Then, values that are.
02:49Then, we will create a new node to create a new node.
02:52We will add its new nodes by new node.
02:54And then, you will create a new node.
02:54And Sé practice.
02:54So, here we will shape its exist.
02:56So, we will replace this node.
02:56So, this 여기에 browse the nodes together.
02:59It will become a new node.
03:00This forces it will become a new node.
03:02to create this code. First of all, we will check what our stack is left. If the stack is left,
03:10then we can create our new node. If our stack is empty, if it is true, then the head is
03:23the new node. The new node is equal to new node of data. The stack is empty, so the head
03:40is the new node and we will return. If this is not in the other cases, we will make the
03:47new node of next. We will make the head and store the new node. Here we will return.
03:57I hope it will be clear that the new node of next will be the head and then the new
04:02node
04:02will be stored. Then we will return the function. This way we will replace the previous head.
04:08Next we will implement the pop function. Pop will implement the public static int pop
04:17Now we will check in the pop function. First of all, we will check the condition
04:19if our stack is empty. If the stack is empty, then we will not be able to do anything.
04:27In that case, we will return directly. Minus one will represent that there is nothing
04:32in the stack. And if the stack is not empty, then we will remove the head.
04:37We will create the new node and return the other node.
04:40So, this is the code. Int top is equal to head.data and head will become next.
04:51And return we will do our top.
04:54We will understand what actually happened.
04:56How we I will use the adapter, which we will try to maintain.
05:02So, that would be under the type of next node.
05:03So, we have the most to our, you know.
05:12We will try to write the next node.
05:16So, let's have a address to the next node.
05:22It is our next node.
05:23We will build the next node Set.
05:24If we have a name, the many nodes.
05:25So, what the product node is the most to our node node.
05:26Now, we will create the next node at new node.
05:26top में और इसे करा देंगे हम return तो इसे हमने return करा दिया और
05:34head बन गया हमारे head का next यानि head के अंदर जो value stored है अब
05:42वो head के next की यानि 2 की value stored है और return हमने करा दिया 3 को
05:47जो था हमारा top तो कुछ इस तरीके से हमने pop करा दिया है अब
05:51पीक को implement कराएंगे और पीक को implement कराना है सबसे असान पीक के लिए
05:56लिखेंगे public static int peak अगर empty है तो उस case में कुछ भी नहीं
06:04peak कर सकते नहीं तो हम हमेशा return कर देंगे अपने head के data को
06:11तो head का जो data है that is our top of the stack तो कुछ इस तरीके से हमने
06:17पीक भी implement कर लिया pop भी कर लिया है push भी कर लिया है और is empty भी कर
06:21लिया है
06:22अब main function के अंदर already stack तो बनाया था ही stack class थी 123 को push किया था और
06:28एक एक करके
06:29peak कर रहे थे pop करा रहे थे इसी code को save कर लेते हैं और run करके देख लेते
06:33हैं कि output कितना
06:34पास आई होगा output में हमारे पास आया है 321 तो process क्या रहा होगा link list के अंदर actually
06:41हुआ क्या होगा
06:42internally सबसे पहले हमने add किया था element number one को तो head था शुरुआत में खाली तो उसने क्या
06:49किया एक नई
06:50node बनाई जिसके अंदर one को store कर लिया इसको बना दिया अपना head उसके बाद एक दूसरी
06:55node आई node के अंदर value थी 2 इसको हमें कराना था push तो 2 के next ने one को
07:01point किया ये head से
07:03हट गया और ये बन गया हमारा नया head उसके बाद एक नई node आई इसके अंदर हमने store
07:08कराया data 3 को 3 के next को हमने point करा दिया head की तरफ head को हटा कर 3
07:14को
07:14बना दिया हमने नया head अब pop कराने के लिए एक एक करके इनको बाहर निकालेंगे यानि यहां से
07:202 बन जाएगा हमारा नया head इस connection को हम break कर देंगे और 3 को हम print करवा देंगे
07:27उसके बाद 1 बन जाएगा हमारा
07:29नया head इस connection को हम break कर देंगे और 2 को print करवा देंगे उसके बाद नल बन जाएगा
07:35हमारा
07:35नया head 1 को हम print करवा देंगे और 3 to 1 होगा हमारा output
07:39output.