1 00:00:00,390 --> 00:00:03,270 ‫In this lesson, we will talk about stacks and Qs. 2 00:00:03,270 --> 00:00:06,750 ‫Stacks and Qs are data structures in computer science. 3 00:00:06,750 --> 00:00:12,060 ‫A data structure is a data organization, management and storage format that enables efficient access 4 00:00:12,060 --> 00:00:13,140 ‫and modification. 5 00:00:13,260 --> 00:00:18,780 ‫Each data structure has a unique way of storing and managing the data we insert into it. 6 00:00:18,840 --> 00:00:24,000 ‫There are many different types of data structures, but in this lesson we will look at two that are 7 00:00:24,000 --> 00:00:28,650 ‫very common and come in handy when we want to deal with data in some scenarios. 8 00:00:28,950 --> 00:00:33,810 ‫The first data structure we will talk about is the stack from its name. 9 00:00:33,870 --> 00:00:37,620 ‫A stack stores the data in the form of a stack. 10 00:00:37,650 --> 00:00:43,650 ‫If we look at this picture of the rock stacked on top of each other, we can easily take the first rock 11 00:00:43,650 --> 00:00:46,470 ‫without causing a stack of rocks to fall. 12 00:00:46,560 --> 00:00:50,030 ‫But it's impossible to take out the ones in the middle. 13 00:00:50,040 --> 00:00:55,710 ‫We can easily take the first rock without causing the stack of rocks to fall, but it's impossible to 14 00:00:55,710 --> 00:00:57,120 ‫take out the ones in the middle. 15 00:00:57,120 --> 00:01:00,300 ‫So it's first come last out or last in. 16 00:01:00,300 --> 00:01:04,760 ‫First out a stack basically adds and removes data from the top only. 17 00:01:04,770 --> 00:01:11,670 ‫So this concept stacks are usually or very often used in mathematical algorithms and in data management 18 00:01:11,670 --> 00:01:13,350 ‫by the operating systems. 19 00:01:13,560 --> 00:01:19,140 ‫So some examples on using stacks that we might face are, for example, reversing data. 20 00:01:19,140 --> 00:01:24,290 ‫So for reversing data or numbers using a stack, it is very simple. 21 00:01:24,300 --> 00:01:29,730 ‫If we just take the input from the user, for example, and store it in a stack, then start removing 22 00:01:29,730 --> 00:01:34,380 ‫data from the stack from top so that the data will get reversed. 23 00:01:35,010 --> 00:01:38,180 ‫So then there are browser back buttons, for example. 24 00:01:38,190 --> 00:01:45,600 ‫So while watching tutorials and tutorials you, your browser will save the website slash web pages you 25 00:01:45,600 --> 00:01:51,030 ‫clicked on and pressing the back button will load the last page you went to. 26 00:01:51,030 --> 00:01:57,480 ‫And then the one before it, which hereby is a textbook example of one, is that can come in handy. 27 00:01:57,840 --> 00:02:05,220 ‫And then there are also the undo and redo buttons that you have in many applications where you have 28 00:02:05,220 --> 00:02:12,600 ‫a stack basically storing what was performed and then undoing that performed action. 29 00:02:13,980 --> 00:02:19,530 ‫So both stacks and Q's are collections like lists and dictionaries. 30 00:02:19,710 --> 00:02:26,880 ‫Stacks have their own unique operations that we can use, for example, to manipulate and manage the 31 00:02:26,880 --> 00:02:27,250 ‫data. 32 00:02:27,250 --> 00:02:30,990 ‫And they store the ones that we're going to look at are going to be pushed. 33 00:02:30,990 --> 00:02:34,170 ‫So using the push method, we can add data to the stack. 34 00:02:34,260 --> 00:02:37,420 ‫Of course, the data will always be added on top. 35 00:02:37,440 --> 00:02:42,900 ‫Then we have pop and using the pop method we can remove data from the top of the stack. 36 00:02:42,900 --> 00:02:51,150 ‫And finally we have the peek method which will return the object data on top of the stack without removing 37 00:02:51,150 --> 00:02:51,480 ‫it. 38 00:02:52,620 --> 00:03:00,570 ‫So then there are queues and choose are another type of data structures that is commonly used in programming 39 00:03:00,600 --> 00:03:07,770 ‫QS work like real Qs in real life, like an airport check in where the first person in the front desk 40 00:03:07,770 --> 00:03:13,410 ‫gets served first, or like in a restaurant drive thru where the car in the front will get to order 41 00:03:13,410 --> 00:03:20,490 ‫first and when new cars come in they must wait in the back of the line and we'll be last served. 42 00:03:20,490 --> 00:03:24,510 ‫So we use Qs when the order the data comes in is important. 43 00:03:24,510 --> 00:03:30,810 ‫So data can be added only from the back and removed from the front and can't access the elements in 44 00:03:30,810 --> 00:03:31,380 ‫the middle. 45 00:03:31,710 --> 00:03:33,540 ‫It's the FIFO system. 46 00:03:33,540 --> 00:03:40,800 ‫So first in, first out, for example in operating systems, queuing messages, IO requests, mouse 47 00:03:40,800 --> 00:03:46,680 ‫movements and so forth so they can be executed in the order they came in. 48 00:03:47,920 --> 00:03:51,970 ‫Then managing web requests and a server to handle them one by one. 49 00:03:51,970 --> 00:03:59,800 ‫So it's a handle congestion and queuing input in video games, for example, and a fighting game with 50 00:03:59,800 --> 00:04:04,990 ‫combo attacks like Street Fighter, where you have to keep track of the user input to determine the 51 00:04:04,990 --> 00:04:06,700 ‫combo that will be performed. 52 00:04:08,050 --> 00:04:16,300 ‫Jews have their own unique operations that we can do on them to manipulate or manage the data they store. 53 00:04:16,330 --> 00:04:20,980 ‫For example, using the NQ method, we can add data to the queue. 54 00:04:21,190 --> 00:04:26,710 ‫Of course the data will be always added to the back of the queue, so it will be rare. 55 00:04:26,920 --> 00:04:28,720 ‫So at the rear, so to speak. 56 00:04:28,960 --> 00:04:31,630 ‫Then we have the queue method. 57 00:04:31,660 --> 00:04:36,400 ‫We can remove data from the front of the queue using the queue method. 58 00:04:36,670 --> 00:04:44,740 ‫And finally we have the peak method which will return the object or data in the front of the queue without 59 00:04:44,740 --> 00:04:45,490 ‫removing it. 60 00:04:45,520 --> 00:04:51,010 ‫So you're just peeking in, but you're not doing anything else with it, just looking what is there? 61 00:04:51,010 --> 00:04:52,300 ‫And then you can use it. 62 00:04:52,540 --> 00:04:59,800 ‫So next we are going to look at how we can actually implement and use stacks and queues in C-sharp.