You are on page 1of 3

Lecture Transcript

Text Editor using 2 Stacks

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 Gedit 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 A will disappear. 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 have to say find t
and replace it with T, this is how our entire content will look like. So notice, DaTaSTrucTure this
has become T, this has become T, this has become T and so on. In short, if we are implementing
a text editor, we should have available with us, functionalities of this kind. 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 two stack model. I would just like
to recapitulate that in early 80’s when we were implementing our unix environment here, one
of my star programmer Dhananjay Balsathe introduced me to this two stack model, look at the
simplicity of the model.

We have this text here ’DataStructuresAndAlgorithms’, 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 character to

1
the right of the cursor I will push them on the right stack, so I have now two stacks a left stack
and right stack. Notice, the ease 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 onto 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 on 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 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 backspace, 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 operations.

We shall see how this can be done through an example. First, inserting word, so consider we
need to insert ’DataStructuresAndAlgorithms’, push it to the left stack, so this is the left D, Da
etc. etc., I keep typing and every time I type a character it should be pushed on 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 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 DataStructures.
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 onto the right stack. Look at how the
left and right stack will look. DataStructures, this is one and AndAlgorithms 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 A, l and g from the word Algorithms. 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 the stack. Suppose, I want to delete seven characters to the left of the stack. That is u, r, e,
s, 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 DataStruct, all other characters
should have been deleted. My right stack will look like this. Of course, it’s an arbitrary editing
this is not a meaningful editing, but this is just to demonstrate the impact of backspace and del

2
character and how it is implemented using our two stack model. 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 DataStructorithms, 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 T, if I want to do that, how could I do that using the functionality that I have
already implemented, it’s actually simple. First, I note the position of the cursor, it is just to the
right of Struc 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
t, delete that character and insert element T. So notice that I have replaced one t with a T. I keep
doing this, if the element is not a 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 earlier. So in a
nutshell what I am doing is, I’m moving cursor to the left then I am taking each element from the
right stack, if it is not a t, I simply copy it on to the left stack and push it on to the left stack. If
it is a t, I delete that and insert a T, so when I insert it will go on top of 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 D from right to left, this 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 T. The insert operation will push
it onto the left stack, effectively what has happened is, I had a t earlier, now I have a 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 model.

Thank you.

You might also like