 Welcome to this session of the course data structures and algorithms. In this session we shall look at one particular application of stacks. I have called it a text editor application. You are all familiar with the text editor. We shall see how a text editor is implemented using a two stack model. First some simple things. You would have been familiar by the way you must have used either g edit on a Ubuntu Unix system or even a simple notepad in a Microsoft operating system where you have some text and you want to edit that text. Typically you would see a cursor and this cursor can be moved left and right. Whenever we start typing on an empty file the characters start appearing on the left of the cursor and cursor keeps moving ahead to the right. In any text editor we are able to move the cursor. For example we can move the cursor to the left. We can move the cursor to the right. At the cursor point we have option of inserting a new character or deleting a character either on the left or right of that character. How can we simplify this notion of inserting or deleting characters at the cursor and moving the cursor to the left and right using stacks. The other operations that we may perform as I just mentioned is that at the cursor when we delete a character using delete key the character on the right of the cursor gets deleted. For example here was a character A. If I press delete key the capital A will disappear here. If I delete a character using backspace key then the character to the left of the cursor gets deleted. So the D gets deleted here. This is how the content will look like. We can replace any one character by any other alternate character in any text editor typically like find and replace. For example if we were to say find small t and replace it with capital T this is how our entire content will look like. So notice data structure this has become capital T this has become capital T this has become capital T and so on. In short if we are implementing a text editor we should have available with us functionalities of this guy. To recapitulate move the cursor to the left or right insert characters at the cursor point which will be inserted on the left and then delete the character either to the left or to the right using either the delete key or backspace key and a special functionality replace a given character. There are of course variety of other functionalities which can be built using these basic functionalities. Let us now look at a novel way of implementing such a text editor. We call it a two stack model. I would just like to recapitulate that in early 80s when we were implementing our Unix environment here one of my star programmers Dhananjay Bhasate introduced me to this two stack model look at the simplicity of the model. We have this text here data structure and algorithm. Suppose we use the cursor position as some kind of a divider. So if this is dividing the text I have characters to the left of it and I have characters to the right of it. I decide to maintain two stacks to hold characters on either side. All characters to the left of the cursor I will push them into the left stack. All characters to the right of the cursor I will push them on the right stack. So I now have two stacks a left stack and right stack. Notice the is with which now I can do manipulation. If I have to move the cursor either to the left or right I simply pop elements from one stack and push them on to another stack. If I wish to delete a character I can either pop the left stack character or pop the right stack character. Similarly insertion is very straight forward I keep inserting to the left stack. This makes my operations of the text editor very easy. If I want to visualize the entire text I can simply move all characters from the left stack to the right stack and start examining the characters to the right of the cursor. Let us look at in a sample implementation of a text editor. First the overview the two stacks are used to depict the text editor. If I want to insert a character or word push it on the left stack. If I want to delete a character using del key perform pop operation on the right stack so that character goes away. If I want to delete a character using back space I do the same pop operation on the left stack. If I want to move cursor to the left copy the required characters from left stack to right stack. If I want to move cursor to the right simply copy the required characters from right stack to the left stack so easy. And of course if I want to replace one character by some other character then I will have to use the combination of move cursor del and insert operation. We shall see how this can be done through an example. First inserting word so consider we need to insert data structures and algorithms push it to the left stack. So this is the left d d a etcetera I keep typing and every time I type a character it should be pushed on to the left stack. So when I push this entire string of characters this will be the position. The right stack will contain nothing the left stack will contain all these characters and of course my cursor continues to act as a divider. Suppose I want to move cursor to the left imagine the cursor is here at the end of the last operation that I just explained and I want to move it to the left. These are the two stacks shown left stack and right stack currently there is nothing on the right stack. Now current cursor position is 27 which is the size of the left stack. Suppose I want to move it to position 14 to the left of the cursor that means I want to move my cursor at this position after data structure. What should I do? Move the elements from left stack to the right stack. So this was the left stack earlier all these characters up to this point I should push on to the right stack. Look at how the left and right stack will look. Data structures this is one and algorithms is actually inserted in exactly the opposite way because that is how the elements will be pushed on to the right stack. So I have now left stack and right stack and I have the cursor at this position. Suppose I want to move the cursor right when the cursor is at this position suppose I want to move it to position 17 all that I need to do is currently I am at 14. So 15, 16 and 17 three characters have to move from right stack to left stack. So move the position that means move elements from right stack to left stack and my stacks will look like this now. How do I delete a character? Very simple. Current cursor position is 17. Delete suppose three characters AL and G from the word algorithm. What I do? I want to delete these characters. I will perform the pop operation three times on the right stack. Left remains the same. You would have already guessed how I can delete characters to the left of the stack. So backspace character. The backspace character will delete characters to the left of this stack. Suppose I want to delete seven characters to the left of the stack that is U, R, E, S, capital A, N, D. These characters I want to delete. All that I need to do is perform pop operation seven times on the left stack. So what will happen is I will just have data struct. All other characters should have been deleted. My right stack will look like this. Of course it is an arbitrary editing. This is not a meaningful editing but this is just to demonstrate the impact of backspace and del character and how it is implemented using our two stack models. We will now look at the special functionality which is often required in a text editor namely finding and replacing a character. Consider the current state of our text. It says data struct orythems because we have already edited part of it. This will be my left stack and this is my right stack. Now suppose I want to examine in my text whether there is any small character T and I want to replace it with a capital T. If I want to do that how could I do that using the functionality that I have already implemented? It is actually simple. First I note the position of the cursor. It is just to the right of struct or left of OR. Now I move all elements from left stack to right stack. What will it mean? It will mean that the cursor has effectively moved to the beginning of the entire text. Now I start moving elements from right to left one by one. What are the steps? If the element is small t delete that character and insert element capital T. So notice that I have replaced one T with a capital T. I keep doing this. If the element is not a small t I simply move it to the left stack. Now I move the cursor at the end of this entire process to the original position which was already described that. So in a nutshell what I am doing is I am moving cursor to the left then I am taking each element from the right stack. If it is not a small t I simply copy it onto the left stack and push it onto the left stack. If it is a small t I delete that and insert a capital T. So when I insert it to go on top of the left stack and keep on doing this with all characters on the string at the end I would get. Currently I have this. Right is this. So move all elements from left to right. So notice left has nothing. Right stack has all these. Now move capital D from right to left is the first character. So it has moved. Next move A. So A has moved. Next perform delete operation because I find the character T here. So when T is deleted there is no T here. But now perform insert operation for capital T. The insert operation will push it onto the left stack. Effectively what has happened is I had a small t earlier. Now I have a capital T in its place. I continue till the right becomes empty. So when there is no more text in the string I will have this particular thing on my left. Now I note that originally the cursor was somewhere here in between these two. I will move the cursor back to the original position getting this in my left stack and this in my right stack. This is how I have achieved find and replace a character. To conclude we have seen a simple two stack model for implementing a text editor. We of course do not have any visual implementation in this but we have demonstrated how simply two stacks can be used with cursor as the dividing point and we actually have a working text editor. In the next session we shall see implementation of such a text editor using two stack models. Thank you.