Sharon is developing a programming challenge for a coding competition. The challenge revolves around implementing a character-based stack data structure using an array.Sharon's project involves a stack that can perform the following operations:Push a Character: Users can push a character onto the stack.Pop a Character: Users can pop a character from the stack.Display Stack: Users can view the current elements in the stack.Exit: Users can exit the stack operations application.Write a program to help Sharon to implement a program that performs the given operations.Input format :The input consists of integers corresponding to the operation that needs to be performed:Choice 1: Push the character onto the stack. If the choice is 1, the following input is a space-separated character, representing the character to be pushed onto the stack.Choice 2: Pop the character from the stack.Choice 3: Display the characters in the stack.Choice 4: Exit the program.Output format :The output displays messages according to the choice and the status of the stack:If the choice is 1, push the given character to the stack and display the pushed character.If the choice is 2, pop the character from the stack and display the character that is popped.If the choice is 2, and if the stack is empty without any characters, print "Stack is empty. Nothing to pop."If the choice is 3, print the elements in the stack.If the choice is 3, and there are no characters in the stack, print "Stack is empty."If the choice is 4, exit the program.If any other choice is entered, print "Invalid choice"Refer to the sample output for the exact format.Code constraints :The maximum size of the stack is defined as 100.Choice: 1, 2, 3, or 4Sample test cases :Input 1 :24Output 1 :Stack is empty. Nothing to pop.Input 2 :1 A1 B234Output 2 :Pushed: APushed: BPopped: BStack elements: A Input 3 :94Output 3 :Invalid choiceInput 4 :1 A234Output 4 :Pushed: APopped: AStack is empty.Input 5 :1 X1 Y1 Z322224Output 5 :Pushed: XPushed: YPushed: ZStack elements: Z Y X Popped: ZPopped: YPopped: XStack is empty. Nothing to pop.
Question
Sharon is developing a programming challenge for a coding competition. The challenge revolves around implementing a character-based stack data structure using an array.Sharon's project involves a stack that can perform the following operations:Push a Character: Users can push a character onto the stack.Pop a Character: Users can pop a character from the stack.Display Stack: Users can view the current elements in the stack.Exit: Users can exit the stack operations application.Write a program to help Sharon to implement a program that performs the given operations.Input format :The input consists of integers corresponding to the operation that needs to be performed:Choice 1: Push the character onto the stack. If the choice is 1, the following input is a space-separated character, representing the character to be pushed onto the stack.Choice 2: Pop the character from the stack.Choice 3: Display the characters in the stack.Choice 4: Exit the program.Output format :The output displays messages according to the choice and the status of the stack:If the choice is 1, push the given character to the stack and display the pushed character.If the choice is 2, pop the character from the stack and display the character that is popped.If the choice is 2, and if the stack is empty without any characters, print "Stack is empty. Nothing to pop."If the choice is 3, print the elements in the stack.If the choice is 3, and there are no characters in the stack, print "Stack is empty."If the choice is 4, exit the program.If any other choice is entered, print "Invalid choice"Refer to the sample output for the exact format.Code constraints :The maximum size of the stack is defined as 100.Choice: 1, 2, 3, or 4Sample test cases :Input 1 :24Output 1 :Stack is empty. Nothing to pop.Input 2 :1 A1 B234Output 2 :Pushed: APushed: BPopped: BStack elements: A Input 3 :94Output 3 :Invalid choiceInput 4 :1 A234Output 4 :Pushed: APopped: AStack is empty.Input 5 :1 X1 Y1 Z322224Output 5 :Pushed: XPushed: YPushed: ZStack elements: Z Y X Popped: ZPopped: YPopped: XStack is empty. Nothing to pop.
Solution
Here is a Python program that implements the stack operations as described:
class Stack:
def __init__(self):
self.stack = []
self.max_size = 100
def push(self, char):
if len(self.stack) < self.max_size:
self.stack.append(char)
print(f"Pushed: {char}")
else:
print("Stack is full. Cannot push.")
def pop(self):
if len(self.stack) > 0:
popped_char = self.stack.pop()
print(f"Popped: {popped_char}")
else:
print("Stack is empty. Nothing to pop.")
def display(self):
if len(self.stack) > 0:
print("Stack elements: ", end="")
for i in reversed(self.stack):
print(i, end=" ")
print()
else:
print("Stack is empty.")
def exit(self):
print("Exiting the program.")
exit(0)
stack = Stack()
while True:
choice = int(input())
if choice == 1:
char = input()
stack.push(char)
elif choice == 2:
stack.pop()
elif choice == 3:
stack.display()
elif choice == 4:
stack.exit()
else:
print("Invalid choice")
This program creates a stack object and performs operations based on the user's input. If the user enters 1, it pushes the next input character onto the stack. If the user enters 2, it pops a character from the stack. If the user enters 3, it displays the current stack elements. If the user enters 4, it exits the program. If the user enters any other number, it prints "Invalid choice".
Similar Questions
Problem Statement:Write a program that implements the Stack Data structure using ArraysThe operations performed on the Stack arePushPopPeekInput format:The first line of input contains the number of operations to be performed on the stackThe next lines contain the integers separated by space in which the first integer indicates the operation to be performed and the second integer contains the element to be pushed.1 ---> indicates the Push2 ---> indicates the Pop3 ---> indicates the PeekOutput format:Display every element after performing the peek operationDisplay the stack after performing all operations at the end.Sample Test Case:Input:61 21 31 4231 5Output:35 3 2Explanation:1 2 ---> 2 will be pushed1 3 ---> 3 will be pushed1 4 ---> 4 will be pushed2 ---> Last element(4) is popped3 ---> peek operation is performed which results in printing the top element of the stack i.e 31 5 ---> 5 will be pushed6 Operations are completedTherefore one peek operation is performed so the output is 3 and the final stack 5 3 2Note:If the stack is empty and when pop and peek are performed first, proceed to the next operation without displaying and modifying anything in the stack.If the stack is empty and nothing is pushed to the stack, Print Empty at the end.Sample Test CasesTest Case 1:Expected Output:61 21 31 4231 535·3·2
Objectives - Learn stacksUsing ArrayCreate a program to implement a stack by using array1) You have to get the user choice using keyboard as follows in the main functionChoices1. Push2. Pop3. Print all the elements of the stack4. Print the top element of the stack5. Exit2) Implement functions one by one according to each choice3) Using a switch case complete the code.Using Linked-listDo the above exercise using singly linked list
Sanjeev is in charge of managing a library's book storage, and he wants to create a program that simplifies this task. His goal is to implement a program that simulates a stack using an array.Help him in writing a program that provides the following functionality:Add Book ID to the Stack (Push): You can add a book ID to the top of the book stack. Remove Book ID from the Stack (Pop): You can remove the top book ID from the stack and display its details. If the stack is empty, you cannot remove any more book IDs.Display Books ID in the Stack (Display): You can view the books ID currently on the stack.Exit the Library: You can choose to exit the program.Input format :The input consists of integers corresponding to the operation that needs to be performed:Choice 1: Push the book onto the stack. If the choice is 1, the following input is a space-separated integer, representing the ID of the book to be pushed onto the stack.Choice 2: Pop the book ID from the stack.Choice 3: Display the book ID in the stack.Choice 4: Exit the program.Output format :The output displays messages according to the choice and the status of the stack:If the choice is 1, push the given book ID to the stack and display the corresponding message.If the choice is 2, pop the book ID from the stack and display the corresponding message.If the choice is 2, and if the stack is empty without any book ID, print "Stack Underflow"If the choice is 3, print the book IDs in the stack.If the choice is 3, and there are book IDs in the stack, print "Stack is empty"If the choice is 4, exit the program and display the corresponding message.If any other choice is entered, print "Invalid choice"
What will be the output of the following program?main() { char str[]="san foundry"; int len = strlen(str); int i; for(i=0;i push(str[i]); // pushes an element into stack for(i=0;i pop(); //pops an element from the stack}
In a prestigious educational institute, Professor Smith designs a programming challenge for Computer Science students. As part of an assessment on Data Structures and Algorithms, students are presented with a postfix expression that involves mathematical operations.The challenge requires students to develop a robust algorithm to evaluate these expressions accurately and efficiently. This exercise not only hones their coding skills but also emphasizes the importance of understanding stack-based computations, enhancing their problem-solving capabilities for real-world software development scenarios.Write a program to evaluate the given postfix expression and display the result.Note: This is a sample question asked in Infosys recruitment.Input format :The input consists of a postfix mathematical expression.The expression will contain real numbers and mathematical operators (+, -, *, /), without any space.Output format :The output prints the result of evaluating the given postfix expression.Refer to the sample output for formatting specifications.Code constraints :The arithmetic operators to be included in the expression are +, -, *, and /.Sample test cases :Input 1 :82/Output 1 :4Input 2 :545*+5/Output 2 :5Input 3 :82-4+Output 3 :10
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.