Computer Science
Stack in data structure
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It has two main operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. Stacks are commonly used in programming languages, compilers, and operating systems.
Written by Perlego with AI-assistance
Related key terms
1 of 5
12 Key excerpts on "Stack in data structure"
- No longer available |Learn more
Data Structures and Program Design Using Python
A Self-Teaching Introduction
- D. Malhotra, N. Malhotra, Dheeraj Malhotra, Neha Malhotra(Authors)
- 2020(Publication Date)
- Mercury Learning and Information(Publisher)
CHAPTER 7STACKS7.1 INTRODUCTIONA stack is an important data structure that is widely used in many computer applications. A stack can be visualized with familiar examples from our everyday lives. A very simple illustration of a stack is a pile of books, where one book is placed on top of another as shown in Figure 7.1 . When we want to remove a book, we remove the topmost book first. Hence, we can add or remove an element (i.e., a book) only at or from one position, that is, the topmost position. In a stack, the element in the last position is served first. Thus, a stack can be described as a LIFO (Last In, First Out) data structure; that is, the element that is inserted last will be the first one to be taken out.FIGURE 7.1 A stack of books7.2 DEFINITION OF A STACKA stack is a linear collection of data elements in that the element inserted last will be the element taken out first (i.e., a stack is a LIFO data structure). The stack is an abstract data structure, somewhat similar to queues. Unlike queues, a stack is open only on one end. A stack is a linear data structure in that the insertion and deletion of elements are done only from the end called TOP. One end is always closed, and the other end is used to insert and remove data.Stacks can be implemented by using arrays or linked lists. We discuss the implementation of stacks using arrays and linked lists in this section.FIGURE 7.2 Representation of a stackPractical Application:1. A real-life example of a stack is a pile of dishes, where one dish is placed on top of another. Now, when we want to remove a dish, we remove the topmost dish first.2. - eBook - PDF
A Textbook of Data Structures and Algorithms, Volume 1
Mastering Linear Data Structures
- G. A. Vijayalakshmi Pai(Author)
- 2022(Publication Date)
- Wiley-ISTE(Publisher)
4 Stacks In this chapter, we introduce the stack data structure, the operations supported by it and their implementations. Additionally, we illustrate two of its useful applications in computer science, namely, recursive programming and evaluation of expressions, among the innumerable available. 4.1. Introduction A stack is an ordered list with the restriction that elements are added or deleted from only one end of the list termed the top of stack. The other end of the list that lies “inactive” is termed the bottom of stack. Thus, if S is a stack with three elements a, b, c where c occupies the top of stack position, and if d were to be added, the resultant stack contents would be a, b, c, d. Note that d occupies the top of stack position. Again, initiating a delete or remove operation would automatically throw out the element occupying the top of the stack, namely, d. Figure 4.1 illustrates this functionality of the stack data structure. Figure 4.1. Stack and its functionality 72 A Textbook of Data Structures and Algorithms 1 It needs to be observed that during the insertion of elements into the stack, it is essential that their identities be specified, whereas for removal, no identity needs to be specified, since by virtue of its functionality, the element that occupies the top of the stack position is automatically removed. The stack data structure therefore obeys the principle of Last In First Out (LIFO). In other words, elements inserted or added into the stack join last, and those that joined last are the first to be removed. Some common examples of a stack occur during the serving of slices of bread arranged as a pile on a platter or during the usage of an elevator (see Figure 4.2). It is obvious that when a slice is added to a pile or removed when serving, it is the top of the pile that is affected. - No longer available |Learn more
Data Structures and Program Design Using Java
A Self-Teaching Introduction
- D. Malhotra, N. Malhotra(Authors)
- 2020(Publication Date)
- Mercury Learning and Information(Publisher)
7STACKS
7.1 Introduction
A stack is an important data structure which is widely used in many computer applications. A stack can be visualized with many familiar examples from our day-to-day lives. A very simple illustration of a stack is a pile of books where one book is placed on top of another as in Figure 7.1 .When we want to remove a book, we remove the topmost book first. Hence, we can add or remove an element (i.e., book) only at or from one position, which is the topmost position. There are many other daily life examples in which we can see how a stack is implemented. We observe that whenever we talk about a stack, we see that the element at the last position will be served first. Thus, a stack can be described as a LIFO (last in, first out) data structure; that is, the element which is inserted last will be the first one to be taken out. Now, let us discuss stacks in detail.Figure 7.1. Stack of books.7.2 Definition of a Stack
A stack is a linear collection of data elements in which the element inserted last will be the element taken out first (i.e., a stack is a LIFO data structure).The stack is an abstract data structure, somewhat similar to queues. Unlike queues, a stack is open only on one end. The stack is a linear data structure in which the insertion and deletion of elements is done only from the end called TOP. One end is always closed, and the other end is used to insert and remove data.Stacks can be implemented by using arrays or linked lists. We will discuss the implementation of stacks using arrays and linked lists in this section.Figure 7.2. Representation of a stack.Practical Application- A real-life example of a stack is a pile of dishes where one dish is placed on top of another. Now, when we want to remove a dish, we remove the topmost dish first.
- Another real-life example of a stack is a pile of discs where one disc is placed on top of another. Now, when we want to remove a disc, we remove the topmost disc first.
- No longer available |Learn more
- D. Malhotra, N. Malhotra(Authors)
- 2019(Publication Date)
- Mercury Learning and Information(Publisher)
7STACKS
In This Chapter• Introduction• Definitions of a stack• Overflow and underflow in stacks• Operations on stacks• Implementation of stacks• Applications of stacks• Summary• Exercises7.1Introduction
A stack is an important data structure which is widely used in many computer applications. A stack can be visualized with many familiar examples from our day-to-day lives. A very simple illustration of a stack is a pile of books where one book is placed on top of another as in Figure 7.1 . When we want to remove a book, we remove the topmost book first. Hence, we can add or remove an element (i.e., book) only at or from one position, which is the topmost position. There are many other daily life examples in which we can see how a stack is implemented. We observe that whenever we talk about a stack, we see that the element at the last position will be served first. Thus, a stack can be described as a LIFO (last in, first out) data structure; that is, the element which is inserted last will be the first one to be taken out. Now, let us discuss stacks in detail.FIGURE 7.1 Stack of books.7.2Definition of a Stack
A stack is a linear collection of data elements in which the element inserted last will be the element taken out first (i.e., a stack is a LIFO data structure). The stack is an abstract data structure, somewhat similar to queues. Unlike queues, a stack is open only on one end. The stack is a linear data structure in which the insertion and deletion of elements is done only from the end called TOP . One end is always closed, and the other end is used to insert and remove data.Stacks can be implemented by using arrays or linked lists. We will discuss the implementation of stacks using arrays and linked lists in this section.FIGURE 7.2 Representation of a stack.Practical Application: 1. - eBook - PDF
C++ Programming
Program Design Including Data Structures
- D. Malik(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
What if you want to write a nonrecursive algorithm to print a linked list backward? This section discusses the data structure called the stack , which the computer uses to implement function calls. You can also use stacks to convert recursive algorithms into nonrecursive algorithms, especially recursive algorithms that are not tail recursive. Stacks have numerous applications in computer science. After developing the tools necessary to implement a stack, we will examine some applications of stacks. A stack is a list of homogeneous elements in which the addition and deletion of elements occur only at one end, called the top of the stack. For example, in a cafeteria, the second tray in a stack of trays can be removed only if the first tray has been removed. For another example, to get to your favorite computer science book, which is under-neath your math and history books, you must first remove the math and history books. After removing these books, the computer science book becomes the top book—that is, the top element of the stack. Figure 17-1 shows some examples of stacks. FIGURE 17-1 Various types of stacks Stack of coins Stack of trays Stack of boxes Stack of books 5 Chemistry English C++ Programming World History Applied Math Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Stacks | 1181 1 7 The elements at the bottom of the stack have been in the stack the longest. The top element of the stack is the last element added to the stack. Because the elements are added and removed from one end (that is, the top), it follows that the item that is added last will be removed first. For this reason, a stack is also called a Last In First Out (LIFO) data structure. Stack: A data structure in which the elements are added and removed from one end only; a Last In First Out (LIFO) data structure. Now that you know what a stack is, let us see what kinds of operations can be per-formed on a stack. - eBook - PDF
- Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
Let’s start with stacks. Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. PROGRAMMING WITH C++ 354 In programming, a stack is a limited access, linear data structure in which the last data element added is the first element removed. As an analogy, visualize a busy cafeteria. The trays are stacked one on top of another. The top tray on the stack—the last one added—is the first one that can be removed. To reach the tray at the bottom of the stack, all the other trays have to be removed first. The data in a stack is controlled by a last-in-first-out algorithm referred to as LIFO (pronounced LIE foh). Adding a data element to the top of the stack is accomplished with a push operation. Deleting an element from the top of a stack is accomplished with a pop operation. Stacks may also support a peek operation that retrieves the value of the top element without removing it from the stack. Figure 20-2 shows a conceptual diagram of a stack, along with its push and pop operations. Figure 20-2 The stack data structure Push Top Pop Stack Figure 20-1 A cafeteria has stacks and queues Claudio Divizia/Shutterstock.com Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. - eBook - PDF
C++ Programming
From Problem Analysis to Program Design
- D. Malik(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Stacks | 1211 1 8 The elements at the bottom of the stack have been in the stack the longest. The top element of the stack is the last element added to the stack. Because the elements are added and removed from one end (that is, the top), it follows that the item that is added last will be removed first. For this reason, a stack is also called a Last In First Out (LIFO) data structure. Stack: A data structure in which the elements are added and removed from one end only; a Last In First Out (LIFO) data structure. Now that you know what a stack is, let us see what kinds of operations can be per- formed on a stack. Because new items can be added to the stack, we can perform the add operation, called push, to add an element onto the stack. Similarly, because the top item can be retrieved and/or removed from the stack, we can perform the opera- tion top to retrieve the top element of the stack and the operation pop to remove the top element from the stack. The push, top, and pop operations work as follows: Suppose there are boxes lying on the floor that need to be stacked on a table. Initially, all of the boxes are on the floor, and the stack is empty (see Figure 18-2). FIGURE 18-2 Empty stack Empty stack A B D C E FIGURE 18-3 Stack operations Push Box A A Push Box C Push Box B B C Peek at the top element Push Box D Pop stack (a) (b) (c) (d) (e) (f) C C D First, we push box A onto the stack. After the push operation, the stack is as shown in Figure 18-3(a). Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. - eBook - ePub
Quick Reference to Data Structures and Computer Algorithms, A
An Insight on the Beauty of Blockchain
- Divya Joseph, Raji Ramakrishnan Nair, Divya Joseph, Alen Joseph(Authors)
- 2019(Publication Date)
- BPB Publications(Publisher)
HAPTER 3Stacks and Queues
3.1 Definition and Concepts
It may be possible that there is a need for a data structure which takes operation on only one end. i.e., beginning or end of the list. In linked list and arrays, we can perform operations at any place of the list. Stack and Queue, are data structures which fulfill the requirements to perform operation on only at one end.3.2 Stack
A stack is a special purpose data structure, containing a list, where data are arranged in order, in which all operations such as insertions and deletions are carried out in only one place, called the TOP of the stack.Given a stack, S = (a1 , …., an ) then we say that a1 is the bottom most element and element ai is on top of element ai -1, 1 < i ≤ n.Definition: A stack is a collection of similar data, which is having some order, where all the operations like insertion and deletion, carried out, only at one end.Stack is also a linear data structure, the insertion and deletion operations in case of stack is specially termed as PUSH and POP respectively, and the position of the stack where such operations are carried out, is known as the TOP of the stack. An element in a stack is commonly termed as an ITEM. The SIZE of a stack defines the maximum number of elements that a stack can hold at once (figure 3.1).Figure 3.1 Schematic diagram of a stackIn stack, the element, which is inserted the last, is the first to be removed from the stack. For this reason, stacks are also known as Last In First Out(LIFO) lists.One natural example of stacks which arises in Computer Programming is the processing of subroutine calls and their returns. Suppose we have a main procedure and 3 subroutines as follows:Figure 3.2 Sequence of subroutines being calledThe MAIN program calls subroutine X. On completion of X, execution of MAIN will resume at location b. The address b is passed to X, which saves it in some location for later procession. X then invokes Y, which in turn calls Z. In every case the calling procedure passes the return address to the called procedure. If we examine the memory which Z is computing there will be an implicit stack which looks like (a, b, c, d). - eBook - ePub
- Xingni Zhou, Zhiyuan Ren, Yanzhuo Ma, Kai Fan, Xiang Ji(Authors)
- 2020(Publication Date)
- De Gruyter(Publisher)
3 Linear list with restricted operations – stack and queueMain Contents- The logic structure definition of stack
- The storage structure of stack and the implementation of its operations
- The logic structure definition of queue
- The storage structure of queue and the implementation of its operations
Learning Aims- Comprehend the features and operation methods of stacks and queues
- Correctly choose them in actual applications
3.1 Stack – a linear list managed in a first-in–last-out manner
The notion of “stack” in data structures comes from the meaning of “stack” in daily lives, which has the characteristic of “first-in–last-out.” See Fig. 3.1 . For things that can be stacked together, we always put the ones below first, and then the ones above, and the order of retrieval is exactly opposite to the order of storage. We can see the meaning of Stack in data structures via the algorithmic implementations of some actual problems.Fig. 3.1: Stack in daily lives.3.1.1 Introduction to the operation pattern of stack
3.1.1.1 Introduction to stack 1 – erroneous operation in Word
Word is a commonly used word editing software. If there is some erroneous operation during usage, for example, deleting the wrong content, will you be worried? People familiar with Word will usually say “it doesn’t matter,” since you can use “undo” tool to restore the content erroneously deleted. The “undo” tool can undo the previous operation. Actually, editing software nowadays all has “undo” functionality. Note that the order of “undo” operations is exactly the opposite of normal operations, as shown in Fig. 3.2 - eBook - PDF
- Peter Brass(Author)
- 2008(Publication Date)
- Cambridge University Press(Publisher)
1 Elementary Structures Elementary data structures usually treated in the “Programming 2” class are the stack and the queue. They have a common generalization, the double- ended queue, which is also occasionally mentioned, although it has far fewer applications. Stack and queue are very fundamental structures, so they will be discussed in detail and used to illustrate several points in data structure implementation. 1.1 Stack The stack is the simplest of all structures, with an obvious interpretation: putting objects on the stack and taking them off again, with access possible only to the top item. For this reason they are sometimes also described as LIFO storage: last in, first out. Stacks occur in programming wherever we have nested blocks, local variables, recursive definitions, or backtracking. Typical programming exercises that involve a stack are the evaluation of arithmetic expressions with parentheses and operator priorities, or search in a labyrinth with backtracking. The stack should support at least the following operations: { push( obj ): Put obj on the stack, making it the top item. { pop(): Return the top object from the stack and remove it from the stack. { stack empty(): Test whether the stack is empty. Also, the realization of the stack has, of course, to give the right values, so we need to specify the correct behavior of a stack. One method would be an algebraic specification of what correct sequences of operations and return values are. This has been done for simple structures like the stack, but even then the specification is not very helpful in understanding the structure. Instead, we can describe a canonical implementation on an idealized machine, which gives the correct answer for all correct sequences of operations (no pop on an 1 2 1 Elementary Structures empty stack, no memory problems caused by bounded arrays). - eBook - PDF
Fundamentals of Python
Data Structures
- Kenneth Lambert(Author)
- 2018(Publication Date)
- Cengage Learning EMEA(Publisher)
Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 169 Using a Stack Applications of stacks in computer science are numerous. Here are just a few; the first three are discussed in more detail later in this chapter: • Translating infix expressions to postfix form and evaluating postfix expressions. The operator in an infix expression, like 3 + 4, appears between its two operands, whereas the operator in a postfix expression, such as 3 4 +, follows its two operands. • Backtracking algorithms (occurring in problems such as automated theorem proving and game playing). • Managing computer memory in support of function and method calls. • Supporting the undo feature in text editors, word processors, spreadsheet programs, drawing programs, and similar applications. • Maintaining a history of the links visited by a web browser. Using a Stack A stack type is not built into Python. In a pinch, Python programmers can use a Python list to emulate an array-based stack. If you view the end of a list as the top of a stack, the list method append pushes an element onto this stack, whereas the list method pop removes and returns the element at its top. The main drawback of this option is that all the other list operations can manipulate the stack as well. These include the insertion, replacement, and removal of an element at any position. These extra operations violate the spirit of a stack as an abstract data type. This section defines a more restricted interface for any authentic stack implementation and shows how these operations are used in a brief example. The Stack Interface In addition to the push and pop operations, a stack interface provides an operation named peek for examining the element at the top of a stack. Like other collections, the stack type can also include the clear, isEmpty, len, str, in, and + operations, as well as an iterator. - eBook - PDF
- Elliot B. Koffman, Paul A. T. Wolfgang(Authors)
- 2012(Publication Date)
- Wiley(Publisher)
C h a p t e r O b j e c t i v e s ◆ To learn about the stack data type and how to use its four functions: push, pop, top, and empty ◆ To understand how C++ implements a stack ◆ To learn how to implement a stack using an underlying array or a linked list ◆ To see how to use a stack to perform various applications, including finding palin- dromes, testing for balanced (properly nested) parentheses, and evaluating arithmetic expressions 311 Stacks C h a p t e r 5 I n this chapter we illustrate how to use and implement an abstract data type known as a stack. A stack differs from a list in the following way. A client of a list can access any element and can insert elements at any location. However, a client of a stack can access only the element that was most recently inserted in the stack. This may seem like a serious restriction that would make stacks not very use- ful, but it turns out that stacks are actually one of the most commonly used data structures in computer science. For example, during program execution a stack is used to store information about the parameters and return addresses for all the functions that are currently executing (you will see how this is done in Chapter 7, “Recursion”). Compilers also use stacks to store information while evaluating expressions. Part of the reason for the widespread use of stacks is that a stack is rel- atively easy to implement. This was an important consideration for programming in languages that did not provide the capability for implementing ADTs as classes. After describing the stack ADT and providing a header file for this ADT, we will discuss two simple applications of stacks: checking for palindromes and testing for balanced parentheses. We will then show how to implement stacks in two ways: using a standard container and a linked list. Finally, we will study how to use stacks to evaluate arithmetic expressions.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.











