{"id":99,"date":"2018-07-18T10:07:46","date_gmt":"2018-07-18T10:07:46","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=99"},"modified":"2018-12-12T08:48:45","modified_gmt":"2018-12-12T08:48:45","slug":"stack-adt","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/stack-adt\/","title":{"rendered":"Stack ADT"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/HzNzJz5z2iQ\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we talk about a very important ADT \u2013 that is the Stack ADT.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the introductory module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 To understand the concept of a stack\r\n\r\n\u2022\u00a0 To appreciate the method of defining a Stack ADT\r\n\r\n\u2022\u00a0 To know about the different stack operations\r\n\r\n\u2022\u00a0 To understand some uses of stacks\r\n\r\n&nbsp;\r\n\r\n<strong>7.1 Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before we go further let us look at the use of stacks in everyday life. In general a stack is a pile of objects, typically one that is neatly arranged. Figure 7.1 shows stacks of notes, plates, sarees, chapathis, tyres and books that we see often see in our lives. In this module we will discuss the characteristics of the Stack used in the computing world.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-102 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19.png\" alt=\"\" width=\"494\" height=\"332\" \/>\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">7.1.1<\/strong><span style=\"text-align: initial;font-size: 1em\">\u00a0 <\/span><strong style=\"text-align: initial;font-size: 1em\">What is a stack?<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">A stack is a sequence of items that are accessible at only one end of the sequence. It is an ordered group of homogeneous items or elements. Elements are added to and removed from a specially designated end called the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out). In other words, a stack is a <\/span><strong style=\"text-align: justify;font-size: 1em\">restricted<\/strong> <strong style=\"text-align: justify;font-size: 1em\">linear list <\/strong><span style=\"text-align: justify;font-size: 1em\">in which all<\/span><strong style=\"text-align: justify;font-size: 1em\"> additions and deletions are made at one end, the top<\/strong><span style=\"text-align: justify;font-size: 1em\">. If we insert a series of data items into a stack and then remove them, the order of the data is reversed. This <\/span><strong style=\"text-align: justify;font-size: 1em\">reversing attribute<\/strong><span style=\"text-align: justify;font-size: 1em\"> is why stacks are known as <\/span><strong style=\"text-align: justify;font-size: 1em\">last in, first out<\/strong> <strong style=\"text-align: justify;font-size: 1em\">(LIFO) data structures. <\/strong><span style=\"text-align: justify;font-size: 1em\">Figure 7.2 shows a computer stack which also indicates that an insertion at the Top into this restricted list is called Push and deletion also at the Top is called POP.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-103 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20.png\" alt=\"\" width=\"345\" height=\"314\" \/>\r\n<p style=\"text-align: justify\">As already stated we assume that all elements of a stack are homogenous that is they are all of the same type. However the elements are unordered and are placed in the stack in the order in which they are inserted. Multiple Occurrences of the same elements is permitted as per the characteristics of the stack. The stack allows unidirectional access (LIFO(Last in First Out) \/ FILO(First In Last Out)) through the current Stack Top Pointer (Stack_Pointer).<\/p>\r\n&nbsp;\r\n\r\n<strong>7.1.2 Conceptual View of a Stack<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The main operations associated with the stack are the Push (adding an element) and the Pop (removing an element) operations. Figure 7.3 shows the conceptual view of adding an element without going into the details. The blue plate (an object to be added needs to be provided) is to be added to the stack. The old Top pointed to the orange plate that was then at the top, now after the blue plate has been added the new top points to this plate. Please note that the bottom of the stack does not play any part in the addition process.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: left\"><img class=\"alignnone size-full wp-image-104 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21.png\" alt=\"\" width=\"417\" height=\"304\" \/><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: justify;font-size: 1em\">Figure 7.4 shows the conceptual view of removing an element without going into the details. The blue plate that is on top of the stack is to be removed from the stack. In this case this is the only element that can be removed. While the old Top pointed to the blue plate that was then at the top, now after the blue plate has been removed the new top points to the orange plate that is now on top. Please note that the bottom of the stack does not play any part in the deletion process.<\/span><\/p>\r\n\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-105 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22.png\" alt=\"\" width=\"411\" height=\"274\" \/>\r\n\r\n<strong>7.2 Uses of Stack ADT<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Stacks are used in many situations especially when there is a need to process elements in a reverse order to the order in which they arrive. This is an important requirement for many problems in the computing world such as using stacks when processing algebraic expressions, using stacks to search a flight map. Another important use of stacks is in tackling recursion. Later we will discuss the applications of stacks in detail.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.3 Abstract Data Type - Stack<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us now look at developing an ADT during the design of a solution to a problem. Consider entering text using a keyboard. Now if we make mistakes then use of\u00a0<span style=\"text-align: initial;font-size: 1em\">backspace is required. For example if we first type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>abcd<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">, and then by mistake type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d <\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">again and then again, there is need to use two backspaces and then continue to type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>efg<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">, and then <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>g<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> again by mistake, then again we need to use backspace. The sequence will be as seen in Figure 7.5. As you will realize, the letter typed last is the one to be removed by the backspace. We seek a programming solution to read these keystrokes. This is a direct mapping to a stack where also we delete elements entered last.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>abcddd&lt;-&lt;-<\/strong><strong>efgg&lt;-<\/strong><\/p>\r\n<p style=\"text-align: center\"><strong>Figure 7.5 Keystrokes using a Keyboard<\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>7.3.1<\/strong>\u00a0\u00a0\u00a0 <strong>Stack ADT<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong><em>Stack Abstract Data Type (Stack ADT)<\/em><\/strong> is a collection of data together with the operations on that data. We will now formally define the <strong><em>interface<\/em><\/strong> of the collection. The interface of the collection tells us what we need to know in order to interact with it, i.e. how to use the operations associated with the stack. The stack can be specified by the user by defining<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><em>Max_Items <\/em>- Maximum number of items that might be on the stack and<\/p>\r\n<p style=\"text-align: justify\"><em>ItemType<\/em>- Data type of the items on the stack.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.3.2<\/strong>\u00a0<strong>ADT stack operations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The next step in defining the Stack ADT is to list the set of operations associated with the ADT. In this section we will list all the operations generally associated with stacks. However we have to remember that the actual set of operations depend on the application for which the Stack ADT will be used and therefore the operations necessary for that application.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first operation associated with any ADT is the creation of an empty ADT. Therefore we will first define the operation of creating an empty Stack. In some cases we may want to destroy the entire stack and hence that is the next operation we will define. A very important operation for many applications of stack is to have an operation to check whether a stack is empty. Then the stack will normally be associated with operations to add and remove elements from it. Finally there may be an operation to retrieve the top element from the stack without removing it. From the ADT perspective a program can use a stack independently of the stack\u2019s implementation.<\/p>\r\n&nbsp;\r\n\r\nNow let us describe in a little more in detail four important operations associated with the stack. They are:\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li style=\"text-align: justify\"><strong>bool isEmpty <\/strong>\u2013 This is a Boolean operation that checks whether a stack is empty and signals a true value if the stack is empty.<\/li>\r\n \t<li style=\"text-align: justify\"><strong>push (ItemType newItem) <\/strong>\u2013 This is the push operation that adds a new item newItem of type ItemType to the stack.<\/li>\r\n \t<li style=\"text-align: justify\"><strong>pop () <\/strong>\u2013 This is the pop operation where we remove from the Stack, the item that has been added most recently. In this definition of pop we do not output the item removed, we just change the contents of the stack.<\/li>\r\n \t<li style=\"text-align: justify\"><strong>top() \u2013 <\/strong>This returns the last inserted element without removing it in other words gets the item that was added to stack most recently.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">In addition to the most important operations described above, a set of auxiliary stack operations are also described below which will be part of the definition of a Stack ADT depending on the application. These auxiliary operations are described below:<\/span><\/p>\r\n\r\n<div>\r\n<ul>\r\n \t<li style=\"text-align: justify\"><strong>size() <\/strong>\u2013 This returns the number of elements that is currently available in the stack<\/li>\r\n \t<li style=\"text-align: justify\"><strong>Create <\/strong>\u2013 This operation creates an Empty Stack with Stack Pointer (top) pointing to null and the number of Stack Elements set to 0<\/li>\r\n \t<li style=\"text-align: justify\"><strong>Check if Full <\/strong>\u2013 This is a Boolean operation that will be true when the number of Elements = maximum number of elements allowed. However this operation is valid only for array based Implementation.<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a07.3.2.1 Creation of an Empty Stack<\/strong>\r\n\r\n&nbsp;\r\n\r\nThis <strong><em>stack<\/em><\/strong> operation CreateStack(stackname) creates an empty stack as shown in Figure 7.6.\r\n\r\n<img class=\"alignnone size-full wp-image-106 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23.png\" alt=\"\" width=\"375\" height=\"295\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>7.3.2.2 Checking whether Stack is Empty<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 7.7 shows the details of the operation that checks whether the Stack is empty. Here it is stated that the operation is Boolean, it has no preconditions and the post-condition is that a true value is returned if the stack is empty and false otherwise.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-107 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24.png\" alt=\"\" width=\"354\" height=\"193\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure 7.7 Details of the Operation \u2013 Checking whether Stack is Empty<\/strong><\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0\u00a0<\/strong><strong style=\"text-align: initial;font-size: 1em\">7.3.2.3 Push Operation<\/strong>\r\n<div>\r\n<p style=\"text-align: justify\">The <strong><em>push<\/em><\/strong> operation inserts an item at the top of the stack (Figure 7.8). Here the push operation is called with the push(<strong><em>stackName,dataitem<\/em><\/strong>) into which addition is to be done and the element (<strong><em>dataItem<\/em><\/strong>) which is to be inserted. The figure shows the example of a stack before the push operation where the top points to the top item <strong><em>79.<\/em><\/strong> The item to be pushed is <strong><em>40<\/em><\/strong> and after the operation <strong><em>40<\/em><\/strong> is now at the top of the stack with top now pointing to <strong><em>40<\/em><\/strong>.<\/p>\r\n<img class=\"alignnone size-full wp-image-108 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25.png\" alt=\"\" width=\"474\" height=\"268\" \/>\r\n<p style=\"text-align: justify\">Figure 7.9 shows the definition of the push operation defined in another way. Here push is defined as a Boolean operation with input being the <strong>newItem<\/strong> of type <strong><em>StackItem Type. <\/em><\/strong>The precondition is that the<strong><em> newItem <\/em><\/strong>is the item to be added and post-conditions are that the operation will signal a true value if push is successful and the <strong><em>newItem<\/em><\/strong> will then be on top of the stack. If the operation is unsuccessful (due to errors or exceptions such as the stack being full in the case of an array implementation) then the operation will signal a false value.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-109 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26.png\" alt=\"\" width=\"372\" height=\"246\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>7.2.3.4 POP Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong><em>pop<\/em><\/strong> operation removes an item from the top of the stack (Figure 7.10). Here the pop operation is called with the stack (<strong><em>stackName<\/em><\/strong>) from which deletion is to be done. The element <strong><em>dataItem<\/em><\/strong> is returned and is the item that has been removed . The figure shows the example of a stack before the pop operation with <strong><em>40<\/em><\/strong> on top of the\u00a0<span style=\"text-align: initial;font-size: 1em\">stack which is then removed by the pop operation as dataItem. The Top now points to the element <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>79<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">. Note that pop can be defined as an operation that is not Boolean but an operation that returns the removed dataitem. In this case before dequeue operation can commence there is a need to check whether the stack is empty and signal an error if it is so.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-110 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27.png\" alt=\"\" width=\"521\" height=\"262\" \/>\r\n<p style=\"text-align: justify\">Figure 7.11 shows the definition of the pop operation defined in another way. Here pop is defined as a Boolean operation with input being the access to the top of the stack <strong>(stackTop).<\/strong> There is no precondition and the post-conditions are that the operation will signal a true value if stack is not empty and the item at <strong><em>stackTop<\/em><\/strong> is removed. However if the stack is empty the operation signals a false value and <strong><em>stackTop <\/em><\/strong>remains unchanged.<\/p>\r\n<img class=\"alignnone size-full wp-image-111 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28.png\" alt=\"\" width=\"558\" height=\"299\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a07.2.3.5 Top Operation<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-112 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29.png\" alt=\"\" width=\"439\" height=\"250\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 7.12 gives the details of the Boolean operation Top. In this case the input given is the same as Pop that is <strong><em>stackTop<\/em><\/strong> however this operation retrieves the element at the top without deleting it. There is no precondition and the post-conditions are that the operation will signal a true value if stack is not empty but <strong><em>stackTop <\/em><\/strong>that contains the element currently on top of the stack is returned. However if the stack is empty the operation signals a false value. In either case the stack remains unchanged.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.2.4 Exceptions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Attempting the execution of an operation of ADT may sometimes cause error conditions or exceptions. Exceptions are said to be \u201cthrown\u201d by an operation that cannot be executed. In the case of Stack ADT, operations pop and top cannot be performed if the stack is empty. Attempting the execution of pop or top on an empty stack throws an EmptyStackException<\/p>\r\n&nbsp;\r\n\r\n<strong>7.3 Series of Stack Operations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this section we have discussed a series of typical stack operations. However this figure must be understood only in terms of what happens in the stack since we have used a linked list type of visualization. This implementation of stacks will be discussed in future modules.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 7.13 shows the stack at various stages during which a series of Stack ADT operations are carried out. The first operation (Figure 7.13 (a)) to be carried out is the creation of an empty stack where count is set to zero and top is set to null. The second operation is a Push operation (Figure 7.13 (b)) where an element Green is inserted into the stack. Now count is set to one and Top points to the added element. The third operation is again a Push operation (Figure 7.13 (c)) where an element Blue is inserted into the stack. Now count is set to two and Top points to the newly added element (Blue). The fourth operation is a POP operation (Figure 7.13 (d)) where the element at the top of the stack (that is Blue) is removed. Now count is reset to one and Top once again points to Green. Finally we show the operation Destroy (Figure 7.13 (e)) where the entire stack is removed. These series of operations should give you an idea of how stacks work. Please note that we have not discussed the details of the implementation.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-113 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30.png\" alt=\"\" width=\"484\" height=\"532\" \/>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a07.4 Applications of Stacks<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Stack is one ADT that is widely used in many areas of computing. There are some direct applications of stacks. These include keeping track of Page-visited history in a Web browser, undoing the sequence of operations in a text editor, keeping track of the chain of method calls in the Java Virtual Machine or C++ runtime environment, etc.. There are also some indirect applications of stacks in the area of computing such as auxiliary data structure for algorithms and as a component of other data structures.<\/p>\r\n&nbsp;\r\n\r\n<strong>7.4.1 Uses of Stacks in Computing<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us look at some of the uses of stacks in computing in more detail. Stacks are useful for any kind of problem involving Last-in-First-Out or <strong><em>LIFO<\/em><\/strong> data. One such example is in <strong><em>Backtracking<\/em><\/strong> which is a common situation in puzzles and games. In this context, we explore a path and find that we have reached a dead end, then we backtrack to find an alternative solution. When we do backtracking, we normally retract to the last position in which we have made a decision where there were other\u00a0<span style=\"text-align: initial;font-size: 1em\">alternatives. Another typical application is in the development of <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Browsers<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> where stacks are used to keep track of pages visited in a browser tab. Another application of stacks is in <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Word Processors, editors<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> where stacks are used to check expressions or strings of text for matching parentheses \/ brackets e.g. if (a == b) { c<\/span><span style=\"text-align: initial;font-size: 1em\">=\u00a0 (d + e) * f;} and to implement <\/span><em style=\"text-align: initial;font-size: 1em\">undo<\/em><span style=\"text-align: initial;font-size: 1em\"> operations in order to keep track of the most recent operations. Stacks are also used with <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Markup languages<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e.g.<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> HTML, XML): which have formatting information (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>tags<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) that need matching (Figure 7.14)<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\">e.g. <strong>&lt;HEAD&gt;<\/strong><\/p>\r\n<p style=\"text-align: center\"><strong>&lt;TITLE&gt;Computer Science 1027a&lt;\/TITLE&gt; &lt;\/HEAD&gt;<\/strong><\/p>\r\n<strong>Figure 7.14 Tags of a Markup Language<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Stacks are also in <strong><em>Stack Calculators<\/em><\/strong> to convert an <strong><em>infix<\/em><\/strong> expression to <strong><em>postfix<\/em><\/strong><em>,<\/em> to make evaluation easier (we will be discussing this more in detail in future modules). The same conversion of infix expressions to postfix is carried out to make translation in <strong><em>Compilers<\/em><\/strong> of a high-level language such as Java or C to a lower level language easier is done using a stack. Another common stack \u2013 the <strong><em>Call stack (Runtime<\/em><\/strong> <strong><em>stack) <\/em><\/strong>is used by runtime system when methods are invoked, for method call \/ return processing (Figure 7.15). Stacks holds the \u201c<em>call frame<\/em>\u201d containing local variables, parameters, etc. The reason for using stacks in this context will also be discussed later.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>e.g. main calls method1<\/strong><\/p>\r\n<p style=\"text-align: center\"><strong>method1 calls method 2<\/strong><\/p>\r\n<p style=\"text-align: center\"><strong>method 2 returns \u2026<\/strong><\/p>\r\n<p style=\"text-align: center\"><strong>Figure 7.15 Method Call and Return<\/strong><\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>7.5 Implementations of the ADT Stack<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before we finish this module about Stack ADT let us list some of the ways in which it can be implemented. Stacks can be implemented using (Figure 7.16):<\/p>\r\n\r\n<ul>\r\n \t<li>An array<\/li>\r\n \t<li>A linked list<\/li>\r\n \t<li>The ADT list<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Since Stack is a restricted list, any list implementation could be used to implement a stack. When we use arrays, the stack is static that is the size of stack has to be fixed initially. On the other hand when we use linked lists, the stack is dynamic and the stack will never become full. In addition we will also see how to implement stacks using the ADT list. In future modules we will explore implementations based on array, linked list and ADT list<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-114 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31.png\" alt=\"\" width=\"382\" height=\"227\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn this module we\r\n<ul>\r\n \t<li>Discussed the concept of Stack<\/li>\r\n \t<li>Explained the Stack ADT<\/li>\r\n \t<li>Outlined most operations possible with the stack<\/li>\r\n \t<li>Discussed possible uses of the stack<\/li>\r\n \t<li>Listed possible implementations of the stack<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Stack ADT<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/HzNzJz5z2iQ\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"alignnone wp-image-115 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32.png\" alt=\"\" width=\"836\" height=\"612\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/HzNzJz5z2iQ\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. In this module we talk about a very important ADT \u2013 that is the Stack ADT.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the introductory module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 To understand the concept of a stack<\/p>\n<p>\u2022\u00a0 To appreciate the method of defining a Stack ADT<\/p>\n<p>\u2022\u00a0 To know about the different stack operations<\/p>\n<p>\u2022\u00a0 To understand some uses of stacks<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.1 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before we go further let us look at the use of stacks in everyday life. In general a stack is a pile of objects, typically one that is neatly arranged. Figure 7.1 shows stacks of notes, plates, sarees, chapathis, tyres and books that we see often see in our lives. In this module we will discuss the characteristics of the Stack used in the computing world.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-102 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19.png\" alt=\"\" width=\"494\" height=\"332\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19.png 494w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19-300x202.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19-225x151.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-19-350x235.png 350w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">7.1.1<\/strong><span style=\"text-align: initial;font-size: 1em\">\u00a0 <\/span><strong style=\"text-align: initial;font-size: 1em\">What is a stack?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">A stack is a sequence of items that are accessible at only one end of the sequence. It is an ordered group of homogeneous items or elements. Elements are added to and removed from a specially designated end called the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out). In other words, a stack is a <\/span><strong style=\"text-align: justify;font-size: 1em\">restricted<\/strong> <strong style=\"text-align: justify;font-size: 1em\">linear list <\/strong><span style=\"text-align: justify;font-size: 1em\">in which all<\/span><strong style=\"text-align: justify;font-size: 1em\"> additions and deletions are made at one end, the top<\/strong><span style=\"text-align: justify;font-size: 1em\">. If we insert a series of data items into a stack and then remove them, the order of the data is reversed. This <\/span><strong style=\"text-align: justify;font-size: 1em\">reversing attribute<\/strong><span style=\"text-align: justify;font-size: 1em\"> is why stacks are known as <\/span><strong style=\"text-align: justify;font-size: 1em\">last in, first out<\/strong> <strong style=\"text-align: justify;font-size: 1em\">(LIFO) data structures. <\/strong><span style=\"text-align: justify;font-size: 1em\">Figure 7.2 shows a computer stack which also indicates that an insertion at the Top into this restricted list is called Push and deletion also at the Top is called POP.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-103 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20.png\" alt=\"\" width=\"345\" height=\"314\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20.png 345w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20-300x273.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20-65x59.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-20-225x205.png 225w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/p>\n<p style=\"text-align: justify\">As already stated we assume that all elements of a stack are homogenous that is they are all of the same type. However the elements are unordered and are placed in the stack in the order in which they are inserted. Multiple Occurrences of the same elements is permitted as per the characteristics of the stack. The stack allows unidirectional access (LIFO(Last in First Out) \/ FILO(First In Last Out)) through the current Stack Top Pointer (Stack_Pointer).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.1.2 Conceptual View of a Stack<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The main operations associated with the stack are the Push (adding an element) and the Pop (removing an element) operations. Figure 7.3 shows the conceptual view of adding an element without going into the details. The blue plate (an object to be added needs to be provided) is to be added to the stack. The old Top pointed to the orange plate that was then at the top, now after the blue plate has been added the new top points to this plate. Please note that the bottom of the stack does not play any part in the addition process.<\/p>\n<\/div>\n<p style=\"text-align: left\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-104 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21.png\" alt=\"\" width=\"417\" height=\"304\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21.png 417w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21-300x219.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21-225x164.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-21-350x255.png 350w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: justify;font-size: 1em\">Figure 7.4 shows the conceptual view of removing an element without going into the details. The blue plate that is on top of the stack is to be removed from the stack. In this case this is the only element that can be removed. While the old Top pointed to the blue plate that was then at the top, now after the blue plate has been removed the new top points to the orange plate that is now on top. Please note that the bottom of the stack does not play any part in the deletion process.<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-105 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22.png\" alt=\"\" width=\"411\" height=\"274\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22.png 411w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22-300x200.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22-225x150.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-22-350x233.png 350w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/p>\n<p><strong>7.2 Uses of Stack ADT<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Stacks are used in many situations especially when there is a need to process elements in a reverse order to the order in which they arrive. This is an important requirement for many problems in the computing world such as using stacks when processing algebraic expressions, using stacks to search a flight map. Another important use of stacks is in tackling recursion. Later we will discuss the applications of stacks in detail.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3 Abstract Data Type &#8211; Stack<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us now look at developing an ADT during the design of a solution to a problem. Consider entering text using a keyboard. Now if we make mistakes then use of\u00a0<span style=\"text-align: initial;font-size: 1em\">backspace is required. For example if we first type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>abcd<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">, and then by mistake type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>d <\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">again and then again, there is need to use two backspaces and then continue to type <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>efg<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">, and then <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>g<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> again by mistake, then again we need to use backspace. The sequence will be as seen in Figure 7.5. As you will realize, the letter typed last is the one to be removed by the backspace. We seek a programming solution to read these keystrokes. This is a direct mapping to a stack where also we delete elements entered last.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>abcddd&lt;-&lt;-<\/strong><strong>efgg&lt;-<\/strong><\/p>\n<p style=\"text-align: center\"><strong>Figure 7.5 Keystrokes using a Keyboard<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3.1<\/strong>\u00a0\u00a0\u00a0 <strong>Stack ADT<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong><em>Stack Abstract Data Type (Stack ADT)<\/em><\/strong> is a collection of data together with the operations on that data. We will now formally define the <strong><em>interface<\/em><\/strong> of the collection. The interface of the collection tells us what we need to know in order to interact with it, i.e. how to use the operations associated with the stack. The stack can be specified by the user by defining<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><em>Max_Items <\/em>&#8211; Maximum number of items that might be on the stack and<\/p>\n<p style=\"text-align: justify\"><em>ItemType<\/em>&#8211; Data type of the items on the stack.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3.2<\/strong>\u00a0<strong>ADT stack operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The next step in defining the Stack ADT is to list the set of operations associated with the ADT. In this section we will list all the operations generally associated with stacks. However we have to remember that the actual set of operations depend on the application for which the Stack ADT will be used and therefore the operations necessary for that application.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first operation associated with any ADT is the creation of an empty ADT. Therefore we will first define the operation of creating an empty Stack. In some cases we may want to destroy the entire stack and hence that is the next operation we will define. A very important operation for many applications of stack is to have an operation to check whether a stack is empty. Then the stack will normally be associated with operations to add and remove elements from it. Finally there may be an operation to retrieve the top element from the stack without removing it. From the ADT perspective a program can use a stack independently of the stack\u2019s implementation.<\/p>\n<p>&nbsp;<\/p>\n<p>Now let us describe in a little more in detail four important operations associated with the stack. They are:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"text-align: justify\"><strong>bool isEmpty <\/strong>\u2013 This is a Boolean operation that checks whether a stack is empty and signals a true value if the stack is empty.<\/li>\n<li style=\"text-align: justify\"><strong>push (ItemType newItem) <\/strong>\u2013 This is the push operation that adds a new item newItem of type ItemType to the stack.<\/li>\n<li style=\"text-align: justify\"><strong>pop () <\/strong>\u2013 This is the pop operation where we remove from the Stack, the item that has been added most recently. In this definition of pop we do not output the item removed, we just change the contents of the stack.<\/li>\n<li style=\"text-align: justify\"><strong>top() \u2013 <\/strong>This returns the last inserted element without removing it in other words gets the item that was added to stack most recently.<\/li>\n<\/ul>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">In addition to the most important operations described above, a set of auxiliary stack operations are also described below which will be part of the definition of a Stack ADT depending on the application. These auxiliary operations are described below:<\/span><\/p>\n<div>\n<ul>\n<li style=\"text-align: justify\"><strong>size() <\/strong>\u2013 This returns the number of elements that is currently available in the stack<\/li>\n<li style=\"text-align: justify\"><strong>Create <\/strong>\u2013 This operation creates an Empty Stack with Stack Pointer (top) pointing to null and the number of Stack Elements set to 0<\/li>\n<li style=\"text-align: justify\"><strong>Check if Full <\/strong>\u2013 This is a Boolean operation that will be true when the number of Elements = maximum number of elements allowed. However this operation is valid only for array based Implementation.<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a07.3.2.1 Creation of an Empty Stack<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This <strong><em>stack<\/em><\/strong> operation CreateStack(stackname) creates an empty stack as shown in Figure 7.6.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-106 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23.png\" alt=\"\" width=\"375\" height=\"295\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23.png 375w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23-300x236.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23-65x51.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23-225x177.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-23-350x275.png 350w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3.2.2 Checking whether Stack is Empty<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 7.7 shows the details of the operation that checks whether the Stack is empty. Here it is stated that the operation is Boolean, it has no preconditions and the post-condition is that a true value is returned if the stack is empty and false otherwise.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-107 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24.png\" alt=\"\" width=\"354\" height=\"193\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24.png 354w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-24-350x191.png 350w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure 7.7 Details of the Operation \u2013 Checking whether Stack is Empty<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0\u00a0<\/strong><strong style=\"text-align: initial;font-size: 1em\">7.3.2.3 Push Operation<\/strong><\/p>\n<div>\n<p style=\"text-align: justify\">The <strong><em>push<\/em><\/strong> operation inserts an item at the top of the stack (Figure 7.8). Here the push operation is called with the push(<strong><em>stackName,dataitem<\/em><\/strong>) into which addition is to be done and the element (<strong><em>dataItem<\/em><\/strong>) which is to be inserted. The figure shows the example of a stack before the push operation where the top points to the top item <strong><em>79.<\/em><\/strong> The item to be pushed is <strong><em>40<\/em><\/strong> and after the operation <strong><em>40<\/em><\/strong> is now at the top of the stack with top now pointing to <strong><em>40<\/em><\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-108 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25.png\" alt=\"\" width=\"474\" height=\"268\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25.png 474w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25-225x127.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-25-350x198.png 350w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p style=\"text-align: justify\">Figure 7.9 shows the definition of the push operation defined in another way. Here push is defined as a Boolean operation with input being the <strong>newItem<\/strong> of type <strong><em>StackItem Type. <\/em><\/strong>The precondition is that the<strong><em> newItem <\/em><\/strong>is the item to be added and post-conditions are that the operation will signal a true value if push is successful and the <strong><em>newItem<\/em><\/strong> will then be on top of the stack. If the operation is unsuccessful (due to errors or exceptions such as the stack being full in the case of an array implementation) then the operation will signal a false value.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-109 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26.png\" alt=\"\" width=\"372\" height=\"246\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26.png 372w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26-300x198.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26-225x149.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-26-350x231.png 350w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.2.3.4 POP Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong><em>pop<\/em><\/strong> operation removes an item from the top of the stack (Figure 7.10). Here the pop operation is called with the stack (<strong><em>stackName<\/em><\/strong>) from which deletion is to be done. The element <strong><em>dataItem<\/em><\/strong> is returned and is the item that has been removed . The figure shows the example of a stack before the pop operation with <strong><em>40<\/em><\/strong> on top of the\u00a0<span style=\"text-align: initial;font-size: 1em\">stack which is then removed by the pop operation as dataItem. The Top now points to the element <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>79<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">. Note that pop can be defined as an operation that is not Boolean but an operation that returns the removed dataitem. In this case before dequeue operation can commence there is a need to check whether the stack is empty and signal an error if it is so.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-110 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27.png\" alt=\"\" width=\"521\" height=\"262\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27.png 521w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-27-350x176.png 350w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/p>\n<p style=\"text-align: justify\">Figure 7.11 shows the definition of the pop operation defined in another way. Here pop is defined as a Boolean operation with input being the access to the top of the stack <strong>(stackTop).<\/strong> There is no precondition and the post-conditions are that the operation will signal a true value if stack is not empty and the item at <strong><em>stackTop<\/em><\/strong> is removed. However if the stack is empty the operation signals a false value and <strong><em>stackTop <\/em><\/strong>remains unchanged.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-111 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28.png\" alt=\"\" width=\"558\" height=\"299\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28.png 558w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28-300x161.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-28-350x188.png 350w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a07.2.3.5 Top Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-112 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29.png\" alt=\"\" width=\"439\" height=\"250\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29.png 439w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29-300x171.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-29-350x199.png 350w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 7.12 gives the details of the Boolean operation Top. In this case the input given is the same as Pop that is <strong><em>stackTop<\/em><\/strong> however this operation retrieves the element at the top without deleting it. There is no precondition and the post-conditions are that the operation will signal a true value if stack is not empty but <strong><em>stackTop <\/em><\/strong>that contains the element currently on top of the stack is returned. However if the stack is empty the operation signals a false value. In either case the stack remains unchanged.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.2.4 Exceptions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Attempting the execution of an operation of ADT may sometimes cause error conditions or exceptions. Exceptions are said to be \u201cthrown\u201d by an operation that cannot be executed. In the case of Stack ADT, operations pop and top cannot be performed if the stack is empty. Attempting the execution of pop or top on an empty stack throws an EmptyStackException<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.3 Series of Stack Operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this section we have discussed a series of typical stack operations. However this figure must be understood only in terms of what happens in the stack since we have used a linked list type of visualization. This implementation of stacks will be discussed in future modules.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 7.13 shows the stack at various stages during which a series of Stack ADT operations are carried out. The first operation (Figure 7.13 (a)) to be carried out is the creation of an empty stack where count is set to zero and top is set to null. The second operation is a Push operation (Figure 7.13 (b)) where an element Green is inserted into the stack. Now count is set to one and Top points to the added element. The third operation is again a Push operation (Figure 7.13 (c)) where an element Blue is inserted into the stack. Now count is set to two and Top points to the newly added element (Blue). The fourth operation is a POP operation (Figure 7.13 (d)) where the element at the top of the stack (that is Blue) is removed. Now count is reset to one and Top once again points to Green. Finally we show the operation Destroy (Figure 7.13 (e)) where the entire stack is removed. These series of operations should give you an idea of how stacks work. Please note that we have not discussed the details of the implementation.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-113 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30.png\" alt=\"\" width=\"484\" height=\"532\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30.png 484w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30-273x300.png 273w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30-65x71.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30-225x247.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-30-350x385.png 350w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a07.4 Applications of Stacks<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Stack is one ADT that is widely used in many areas of computing. There are some direct applications of stacks. These include keeping track of Page-visited history in a Web browser, undoing the sequence of operations in a text editor, keeping track of the chain of method calls in the Java Virtual Machine or C++ runtime environment, etc.. There are also some indirect applications of stacks in the area of computing such as auxiliary data structure for algorithms and as a component of other data structures.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.4.1 Uses of Stacks in Computing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us look at some of the uses of stacks in computing in more detail. Stacks are useful for any kind of problem involving Last-in-First-Out or <strong><em>LIFO<\/em><\/strong> data. One such example is in <strong><em>Backtracking<\/em><\/strong> which is a common situation in puzzles and games. In this context, we explore a path and find that we have reached a dead end, then we backtrack to find an alternative solution. When we do backtracking, we normally retract to the last position in which we have made a decision where there were other\u00a0<span style=\"text-align: initial;font-size: 1em\">alternatives. Another typical application is in the development of <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Browsers<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> where stacks are used to keep track of pages visited in a browser tab. Another application of stacks is in <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Word Processors, editors<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> where stacks are used to check expressions or strings of text for matching parentheses \/ brackets e.g. if (a == b) { c<\/span><span style=\"text-align: initial;font-size: 1em\">=\u00a0 (d + e) * f;} and to implement <\/span><em style=\"text-align: initial;font-size: 1em\">undo<\/em><span style=\"text-align: initial;font-size: 1em\"> operations in order to keep track of the most recent operations. Stacks are also used with <\/span><strong style=\"text-align: initial;font-size: 1em\"><em>Markup languages<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>e.g.<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\"> HTML, XML): which have formatting information (<\/span><strong style=\"text-align: initial;font-size: 1em\"><em>tags<\/em><\/strong><span style=\"text-align: initial;font-size: 1em\">) that need matching (Figure 7.14)<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\">e.g. <strong>&lt;HEAD&gt;<\/strong><\/p>\n<p style=\"text-align: center\"><strong>&lt;TITLE&gt;Computer Science 1027a&lt;\/TITLE&gt; &lt;\/HEAD&gt;<\/strong><\/p>\n<p><strong>Figure 7.14 Tags of a Markup Language<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Stacks are also in <strong><em>Stack Calculators<\/em><\/strong> to convert an <strong><em>infix<\/em><\/strong> expression to <strong><em>postfix<\/em><\/strong><em>,<\/em> to make evaluation easier (we will be discussing this more in detail in future modules). The same conversion of infix expressions to postfix is carried out to make translation in <strong><em>Compilers<\/em><\/strong> of a high-level language such as Java or C to a lower level language easier is done using a stack. Another common stack \u2013 the <strong><em>Call stack (Runtime<\/em><\/strong> <strong><em>stack) <\/em><\/strong>is used by runtime system when methods are invoked, for method call \/ return processing (Figure 7.15). Stacks holds the \u201c<em>call frame<\/em>\u201d containing local variables, parameters, etc. The reason for using stacks in this context will also be discussed later.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>e.g. main calls method1<\/strong><\/p>\n<p style=\"text-align: center\"><strong>method1 calls method 2<\/strong><\/p>\n<p style=\"text-align: center\"><strong>method 2 returns \u2026<\/strong><\/p>\n<p style=\"text-align: center\"><strong>Figure 7.15 Method Call and Return<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.5 Implementations of the ADT Stack<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before we finish this module about Stack ADT let us list some of the ways in which it can be implemented. Stacks can be implemented using (Figure 7.16):<\/p>\n<ul>\n<li>An array<\/li>\n<li>A linked list<\/li>\n<li>The ADT list<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Since Stack is a restricted list, any list implementation could be used to implement a stack. When we use arrays, the stack is static that is the size of stack has to be fixed initially. On the other hand when we use linked lists, the stack is dynamic and the stack will never become full. In addition we will also see how to implement stacks using the ADT list. In future modules we will explore implementations based on array, linked list and ADT list<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-114 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31.png\" alt=\"\" width=\"382\" height=\"227\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31.png 382w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-31-350x208.png 350w\" sizes=\"auto, (max-width: 382px) 100vw, 382px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In this module we<\/p>\n<ul>\n<li>Discussed the concept of Stack<\/li>\n<li>Explained the Stack ADT<\/li>\n<li>Outlined most operations possible with the stack<\/li>\n<li>Discussed possible uses of the stack<\/li>\n<li>Listed possible implementations of the stack<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Stack ADT<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/HzNzJz5z2iQ\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-115 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32.png\" alt=\"\" width=\"836\" height=\"612\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32.png 692w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32-300x220.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32-225x165.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-32-350x256.png 350w\" sizes=\"auto, (max-width: 836px) 100vw, 836px\" \/><\/p>\n","protected":false},"author":3,"menu_order":7,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-99","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":13,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/99\/revisions"}],"predecessor-version":[{"id":910,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/99\/revisions\/910"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/99\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=99"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=99"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}