Data Structures: Stacks and Lists

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Define 'data structure' as described in the text.

A mechanism to store, organize, and access data along with operations that can be efficiently performed on the data.

Name two sequence data types in Python.

String, List, Set, Tuple

Why are different data types used in Python?

For faster accessibility and efficient storage of data.

What are two popular data structures used in programming that are talked about in the chapter?

<p>Stack and Queue</p> Signup and view all the answers

Explain the Last-In-First-Out (LIFO) principle in the context of a stack.

<p>The element which was inserted last will be the first one to be taken out from the stack.</p> Signup and view all the answers

Give one real-life example of a stack.

<p>Pile of clothes in an almirah / Multiple chairs in a vertical pile / Bangles worn on wrist / Pile of boxes of eatables in pantry or on a kitchen shelf</p> Signup and view all the answers

What is the significance of the 'TOP' in a stack data structure?

<p>The end from which elements are added or deleted</p> Signup and view all the answers

Describe the 'PUSH' operation in a stack.

<p>Adds a new element at the TOP of the stack. It is an insertion operation.</p> Signup and view all the answers

What is the 'overflow' condition in a stack?

<p>A stack is full when no more elements can be added to it. Trying to add an element to a full stack results in an exception called 'overflow'.</p> Signup and view all the answers

How does Python use the list data type to implement a stack?

<p>By using the built-in methods <code>append()</code> and <code>pop()</code> of the list for implementation of the stack</p> Signup and view all the answers

In the context of stack implementation using Python's list, why is explicit declaration of 'TOP' not needed?

<p>Because we are using the built-in methods append() and pop() of the list for implementation of the stack. As these built-in methods insert/delete elements at the rightmost end of the list, hence explicit declaration of TOP is not needed.</p> Signup and view all the answers

What is polish notation?

<p>A way of representing arithmetic expression, called polish notation. In such notation, operators are written before their operands.</p> Signup and view all the answers

What is reverse polish notation?

<p>An expression by putting operators after their operands.</p> Signup and view all the answers

What is the primary advantage of prefix/postfix notations over infix notation in terms of computation?

<p>Prefix/postfix expressions do not have to deal with such precedence because the operators are already positioned according to their order of evaluation. Hence a single traversal from left to right is sufficient to evaluate the expression.</p> Signup and view all the answers

In the context of converting an infix expression to postfix notation, what is the role of a stack?

<p>A stack is used to keep track of the operators encountered in the infix expression.</p> Signup and view all the answers

When evaluating any Postfix expression using Stack, what is PUSHed onto it?

<p>operands</p> Signup and view all the answers

Describe a scenario where stacks are used in web browsing.

<p>When browsing the web, we move from one web page to another by accessing links between them. In order to go back to the last visited web page, we may use the back button on the browser. The history of visited pages is maintained as a stack.</p> Signup and view all the answers

Explain how stacks are used in text/image editors.

<p>Used for editing the text/image where we have options to redo/undo the editing done. The system uses a stack to keep track of changes made.</p> Signup and view all the answers

Describe a software engineering situation where stack is used.

<p>While writing any arithmetic expression in a program, we may use parentheses to order the evaluation of operators. While executing the program, the compiler checks for matched parentheses i.e. each opening parenthesis should have a corresponding closing parenthesis and the pairs of parentheses are properly nested. In case of parentheses are mismatched, the compiler needs to throw an error. To handle matching of parentheses, stack is used.</p> Signup and view all the answers

Explain why it is inconvenient to add or remove an object from the middle or bottom of a large pile when thinking about stacks.

<p>In a large pile, it is inconvenient to add or remove an object from in between or bottom. Such an arrangement of elements in a linear order is called a stack.</p> Signup and view all the answers

Explain one way stacks are used in compilers or interpreters.

<p>Handle function calls.</p> Signup and view all the answers

Explain how stacks are used by operating systems.

<p>The operating system in computer or mobile allocates memory to different applications for their execution.</p> Signup and view all the answers

Why doesn't Python's list implementation of Stacks have to worry about 'overflow'?

<p>As there is no limit on the size of list in Python, the implemented stack will never be full unless there is no more space available in memory.</p> Signup and view all the answers

In the context of converting infix to postfix notation, what specific condition triggers the popping of elements from the stack?

<p>If a right parenthesis is encountered or the precedence of the current operator is lower than that of the operator at the top of the stack.</p> Signup and view all the answers

Describe the steps to delete all elements from stack.

<p>While true, set <code>item</code> equal to opPop(glassStack). If <code>item</code> is <code>None</code>, then print <code>Stack is empty now</code> and break. Else print <code>Popped element is</code>, item.</p> Signup and view all the answers

Using Python, explain how to create an empty stack and add the string "glass1" to it.

<p><code>glassStack = list()</code> and then <code>opPush(glassStack, 'glass1')</code>.</p> Signup and view all the answers

Describe how to display all elements in the stack.

<p>Use a for loop to move through the stack using a <code>range()</code> function. The code is <code>for i in range(x-1,-1,-1):</code> and then <code>print(glassStack[i])</code>.</p> Signup and view all the answers

Explain how to check if stack is empty.

<p>Use the <code>isEmpty()</code> function to check the number of elements in the stack. If the number of elements is <code>0</code>, then return <code>True</code>. Otherwise return <code>False</code>.</p> Signup and view all the answers

Using the concept of stacks, devise a method to determine if a given string containing parentheses is balanced (i.e., each opening parenthesis has a corresponding closing parenthesis in the correct order).

<p>Iterate through the string. Push each opening parenthesis onto a stack. When a closing parenthesis is encountered, pop an element from the stack. If the stack is empty at the end, the string is balanced; otherwise, it is not.</p> Signup and view all the answers

Explain in what situation stack elements will have to be POPed while you are pushing?

<p>IF its precedence is lower than that of operator at the top of Stack THEN POP elements from the Stack till an operator with precedence less than the current operator is encountered and append to string postExp before pushing this operator on the postStack.</p> Signup and view all the answers

Given an infix expression that includes exponentiation (^) along with other operators (+, -, *, /), how would you modify the infix-to-postfix conversion algorithm to correctly handle the right-to-left associativity of the exponentiation operator?

<p>When an exponentiation operator is encountered, it should be pushed onto the stack only if the operator at the top of the stack has a lower precedence or if the stack is empty. If the operator at the top of the stack is also an exponentiation operator, the new operator should still be pushed without popping, due to right-to-left associativity.</p> Signup and view all the answers

Describe the implications of using a stack versus a queue for managing function calls in a recursive program.

<p>Using a stack allows the program to follow the correct call and return sequence, enabling proper execution of the algorithm whereas the order of recursive calls would be incorrect if they were managed by a queue.</p> Signup and view all the answers

Explain the difference between Infix, Prefix, and Postfix notations.

<p>Infix notations are when the operator is in the middle of the operands. Prefix notations place the operator before the operands. Postfix notations place the operator after the operands.</p> Signup and view all the answers

Describe how to read the topmost element of the stack.

<p>Use the <code>top()</code> function to read the most recent element (TOP) in the <code>glassStack</code>.</p> Signup and view all the answers

Describe two of three applications of stack beyond web browsing and compilers.

<p>Reversing a string and editing text and images.</p> Signup and view all the answers

Describe how to implement a PUSH function in Python.

<p>def opPush(glassStack,element): glassStack.append(element)</p> Signup and view all the answers

Write a program or function you could use in conjunction with a stack to determine how many elements are in the stack.

<p>def size(glassStack): return len(glassStack)</p> Signup and view all the answers

Flashcards

Data Structure

A mechanism to store, organise, and access data efficiently using operations.

Linear Data Structure

Data structure where elements are arranged in a linear sequence.

Stack

A data structure that follows the Last-In-First-Out (LIFO) principle.

TOP of Stack

The end of a stack where elements are added or removed.

Signup and view all the flashcards

PUSH Operation

Adds a new element to the top of the stack.

Signup and view all the flashcards

POP Operation

Removes the topmost element from the stack.

Signup and view all the flashcards

Stack Overflow

Exception when trying to add to a full stack.

Signup and view all the flashcards

Stack Underflow

Trying to delete from an empty stack

Signup and view all the flashcards

Infix Notation

Operators are in between operands.

Signup and view all the flashcards

Polish Notation

prefix notation where operators are before operands (e.g., +xy).

Signup and view all the flashcards

Postfix Notation

Operators after operands

Signup and view all the flashcards

Study Notes

Introduction to Data Structures

  • Data structures are ways of grouping multiple data elements for faster access and efficient storage.
  • They define mechanisms to store, organize, and access data along with operations for efficient processing.
  • Examples include strings (sequences of characters) and lists (sequences of potentially different data types).
  • An example of an operation that can be performed on a list includes reversal, slicing, counting.
  • Organizes multiple elements in a way that operations can be performed easily, on each element, and the collective data unit.
  • Examples of important data structures in Computer Science: Array, Linked List, Binary Trees, Heaps, Graph, Sparse Matrix, etc.
  • A data structure in which elements are organized in a sequence is a linear data structure.

Stack Data Structure

  • Stack and Queue are two popular data structures used in programming.
  • A stack is a linear arrangement of elements where new elements are added or removed from the same end, called the "top" of the stack.
  • Analogous to piles of books or stacks of plates where items are added or removed from the top, preventing inconvenient access to items in between or at the bottom.
  • Follows the Last-In-First-Out (LIFO) principle, meaning the most recently inserted element is the first one to be taken out.

Applications of Stacks

  • Real-life examples: piles of clothes in an almirah, multiple chairs in a vertical pile, bangles worn on wrist, piles of boxes of eatables in pantry or on a kitchen shelf.
  • Programming Applications include:
    • Reversing a string by traversing it from the last character to the first character and putting characters in a stack.
    • For text/image editors to redo/undo the editing by using stacks to save the recent system editing.
    • Browsing history on the web is as stack. The back button takes you back to the last visited web page i.e a stack order.
    • Compilers use stacks to check for the proper nesting of parentheses in arithmetic expressions.

Stack Operations

  • A stack that implements the LIFO arrangement adds and deletes elements from the stack from one end only.
  • The end from which elements are added or deleted is called the "top" of the stack.
  • The two fundamental operations:
    • PUSH: adds a new element at the top.
    • POP: removes the top element.
  • PUSH is an insertion operation, adding elements until the stack is full.
  • An attempt to add an element to a full stack results in the 'overflow' exception.
  • POP removes the topmost element and is a deletion operation, and can be performed until the stack is empty.
  • Trying to delete an element from an empty stack will result in an exception called ‘underflow'.

Stack operations with Python

  • Implementing a stack in Python can be easily achieved using the list data type.
  • To implement using lists requires implementation of stacks of glasses to insert, delete, check is empty , find # of elements, read out the top most element.
  • The methods append() and pop() are built-in list methods for implementation of a stack.
    • insert/delete elements (glasses).
    • check if the STACK is empty (no glasses in the stack).
    • find the number of elements (glasses) in the STACK.
    • read the value of the topmost element (number on the topmost glass) in the STACK.
  • glassStack = list() creates an empty stack.
  • isEmpty(glassStack) checks if the stack is empty; returns True if empty, otherwise False.
  • Returns True if the list is empty with with len(glassStack)==0
  • 'opPush(glassStack,element)' inserts a new element and uses the built in append() list method, that always adds to the end of the list.
  • 'size(glassStack)' reads the number of elements in the glassStack using len().
  • 'top(glassStack)' reads the top most recent element TOP, in the stack.
  • 'opPop (glassStack)' is used to delete the topmost element.
  • 'display(glassStack)' is used to show contents of the stack.

Arithmetic Expressions and Notations

  • Used for writing arithmetic expressions in a program,
  • Stacks are used to handle matching parenthesis.

Infix Notation

  • Operators are between operands (e.g., x + y, 2 - 3 * y).
  • Use parentheses to order the evaluation.
  • Follow the BODMAS rule.

Polish or Prefix Notation

  • Invented by Jan Lukasiewicz in the 1920s
  • Operators are written before operands (e.g., +xy).
  • Called prefix notation.
  • Order of operations and operands determine result
  • Eliminates the need for parenthesis.

Reverse Polish or Postfix Notation

  • Operators are positioned after their operands (e.g., xy+).

Conversion from Infix to Postfix

  • Stacks are used to keep track of operators encountered in the infix expression during conversion.
  • For the conversion algorithm:
    • Algorithm stores converted postfix expressions in a string
    • It inputs infix and processes each character.

Evaluation of Postfix Expressions

  • Stacks evaluate expressions in postfix notation.
  • Assumes operators are binary.
  • In the evaluation algorithm:
    • Postfix expression is input in a variable.
    • The system looks for an operand or character.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser