ch
Feedback
inactive

inactive

关闭频道
3 849
订阅者
无数据24 小时
-297 天
-12730 天
帖子存档
// You are using GCC #include <iostream> #include <stack> #include <string> using namespace std; int getPrecedence(char op) { if (op == '+' op == '-') return 1; if (op == '*' op == '/') return 2; return 0; } bool isOperator(char ch) { return (ch == '+' ch == '-' ch == '*' || ch == '/'); } string infixToPostfix(const string& infix) { string postfix; stack<char> operators; for (char ch : infix) { if (isalnum(ch)) { postfix += ch; // Append operands directly to the output } else if (ch == '(') { operators.push(ch); } else if (ch == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); operators.pop(); } operators.pop(); // Pop '(' } else if (isOperator(ch)) { while (!operators.empty() && getPrecedence(ch) <= getPrecedence(operators.top())) { postfix += operators.top(); operators.pop(); } operators.push(ch); } } while (!operators.empty()) { postfix += operators.top(); operators.pop(); } return postfix; } int main() { string infix; // cout << "Enter an infix expression: "; cin >> infix; string postfix = infixToPostfix(infix); cout << "Postfix expression: " << postfix << endl; return 0; }//awasthi108

Meenu is studying computer science and is currently learning about expressions in infix notation. She needs a program to convert infix expressions to postfix notation to help her with her studies. Can you help her by providing a program that performs this conversion? The program should support the following operations: Check if a character is an operator (+, -, *, or /). Determine the precedence of an operator. Convert an infix expression to postfix notation. Example Input: (3+4)5 Output: 34+5 Note: This is a sample question asked in TCS recruitment. Input format : The input consists of the infix expression to be converted to postfix notation. Output format : The output displays the postfix expression equivalent of the input infix expression.

#include <iostream> #include <stack> #include <string> #include <unordered_map> using namespace std; unordered_map<char, int> precedence = { {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'%', 2}, {'^', 3}, }; bool isOperator(char c) { return precedence.find(c) != precedence.end(); } string infixToPostfix(const string& infix) { string postfix; stack<char> operators; for (char c : infix) { if (c == ' ' || c == ',') { continue; // Ignore spaces and commas } else if (isalnum(c)) { postfix += c; // Append operands directly to postfix } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); // Remove '(' from stack } } else { // Handle operators while (!operators.empty() && operators.top() != '(' && precedence[c] <= precedence[operators.top()]) { postfix += operators.top(); operators.pop(); } operators.push(c); } } while (!operators.empty()) { postfix += operators.top(); operators.pop(); } return postfix; } int main() { string infix; //cout << "Enter the infix mathematical expression: "; getline(cin, infix); string postfix = infixToPostfix(infix); cout << "Postfix expression: " << postfix << endl; return 0; }//awasthi108

Usha is currently studying computer science and is interested in mathematical expressions and their evaluation. She came across an infix expression that includes mathematical operators and functions and wants to convert it into postfix notation. She needs your help to implement a program that can perform this task. Your task is to implement a program that takes an infix mathematical expression with function calls, converts it into postfix notation, and prints the postfix expression. Example Input: sin(3x)+cos(4x) Output: sin3xcos4x+ Note: This is a sample question asked in Capgemini recruitment. Input format : The input consists of a single line containing the infix mathematical expression with function calls. The expression will contain: Arithmetic operators: +, -, *, /, %, ^. Functions: sin, cos, tan, exp, log, sqrt. Parentheses: (and). Numeric values (integers) Spaces may be included in the input, but they should be ignored.

// You are using GCC #include <iostream> using namespace std; // Define a Node structure for the linked list struct Node { int data; Node* next; }; class Stack { private: Node* top; public: // Constructor to initialize an empty stack Stack() { top = nullptr; } // Function to push an element onto the stack void push(int val) { Node* newNode = new Node; newNode->data = val; newNode->next = top; top = newNode; } // Function to pop an element from the stack void pop() { if (isEmpty()) { cout << "Stack is empty." << endl; return; } Node* temp = top; top = top->next; delete temp; } // Function to check if the stack is empty bool isEmpty() { return top == nullptr; } // Function to print the stack elements void printStack() { Node* current = top; while (current != nullptr) { cout << current->data << " "; current = current->next; } cout << endl; } // Function to get the top element of the stack int getTop() { if (isEmpty()) { cout << "Stack is empty." << endl; return -1; // Return a dummy value } return top->data; } }; int main() { int n; cin >> n; Stack stack; for (int i = 0; i < n; i++) { int element; cin >> element; stack.push(element); } // cout << "Stack elements: "; stack.printStack(); cout <<"Top element is "<<stack.getTop() << endl; stack.pop(); // cout << "After pop operation:" << endl; // cout << "Stack elements: "; stack.printStack(); cout << "Top element is "<< stack.getTop() << endl; return 0; }//awasthi108

Jessica is learning about data structures and wants to understand how a stack can be implemented using a linked list. She decides to implement a program that demonstrates the implementation of a stack using a linked list. However, Jessica is not sure how to proceed and seeks guidance from her friend Michael, who is knowledgeable in data structures and algorithms. Write a program that allows Jessica and Michael to implement a stack using a linked list and prints the stack after performing a pop operation. Note: This is a sample question asked in TCS recruitment. Input format : The first line of the input consists of the value of n. The next input is the n stack elements. Output format : The first line of the output prints the stack elements. The next line prints the top element of the stack. The third line prints the result of the pop operation. The next line prints the top element of the stack after performing pop operation.

// You are using GCC #include <iostream> using namespace std; const int MAX_SIZE = 32; class Stack { private: int top; int arr[MAX_SIZE]; public: Stack() { top = -1; } bool isFull() { return top == MAX_SIZE - 1; } bool isEmpty() { return top == -1; } void push(int value) { if (!isFull()) { arr[++top] = value; } } int pop() { if (!isEmpty()) { return arr[top--]; } return -1; } }; void decimalToBinary(int decimal) { Stack stack; while (decimal > 0) { int remainder = decimal % 2; stack.push(remainder); decimal /= 2; } cout << "Binary representation: "; while (!stack.isEmpty()) { cout << stack.pop(); } cout << endl; } int main() { int decimal; cin >> decimal; decimalToBinary(decimal); return 0; } //awasthi

Binu is learning about data structures and is particularly interested in stacks. He wants to practice implementing a stack-based program to convert decimal numbers to binary representation. He has a basic understanding of stacks and has designed a program to achieve this task. Initialize a Stack: Create a stack data structure that can store integers. The stack should be initialized to an empty state. Push Operation: Implement a function push that adds an integer to the top of the stack. The stack should have a maximum size of 32 and should not allow pushing if it is already full. Pop Operation: Implement a function pop that removes and returns the integer from the top of the stack. If the stack is empty, it should return -1. Note: This is a sample question asked in a TCS interview. Input format : The input consists of an integer decimal representing the decimal number to be converted into binary. Output format : The output is a single line containing the binary representation of the input decimal number in the format "Binary representation: [binary]".

// You are using GCC #include <stdio.h> #define MAX_SIZE 100 typedef struct { int stack[MAX_SIZE]; int top; } Stack; void push(Stack *s, int item) { if (s->top >= MAX_SIZE - 1) { printf("Stack Overflow\n"); return; } s->stack[++(s->top)] = item; } int pop(Stack *s) { if (s->top < 0) { printf("Stack Underflow\n"); return -1; } return s->stack[(s->top)--]; } void reverseStack(Stack *A, Stack *B) { while (A->top >= 0) { int item = pop(A); push(B, item); } } int main() { Stack A, B; A.top = -1; B.top = -1; int n, i, item; // printf("Enter the number of elements in stack A: "); scanf("%d", &n); //printf("Enter the elements of stack A separated by spaces: "); for (i = 0; i < n; i++) { scanf("%d", &item); push(&A, item); } printf("Stack A elements: "); for (i = A.top; i >= 0; i--) { printf("%d ", A.stack[i]); } printf("\n"); reverseStack(&A, &B); printf("Elements in Stack B (reversed): "); for (i = B.top; i >= 0; i--) { printf("%d ", B.stack[i]); } printf("\n"); //awasthi return 0; }

You are working on a program that simulates a document editor. The editor uses two stacks, stack A and stack B, to manage the document content. Stack A represents the current document state, while stack B is used for temporary operations. Your task is to implement a function that reverses the elements in stack A and stores the reversed elements in stack B. This operation is needed to support a specific feature in the editor. Note: This is a sample question asked in an Infosys interview. Input format : The first line contains an integer n, representing the number of elements in stack A. The next line of the input is the space-separated values of stack A. Output format : The first line of output prints the Stack A elements, separated by space. The second line of output prints the elements in Stack B after reversing Stack A, separated by space.

// You are using GCC #include <iostream> #include <stack> #include <string> using namespace std; bool isBalanced(const string& expression) { stack<char> s; for (char ch : expression) { if (ch == '(' ch == '{' ch == '[') { s.push(ch); } else if (ch == ')' ch == '}' ch == ']') { if (s.empty()) { return false; // Unmatched closing symbol } char top = s.top(); s.pop(); if ((ch == ')' && top != '(') (ch == '}' && top != '{') (ch == ']' && top != '[')) { return false; // Mismatched opening and closing symbols } } } return s.empty(); // If the stack is empty, all symbols are matched } int main() { string expression; //cout << "Enter an expression: "; getline(cin, expression); if (isBalanced(expression)) { cout << "The expression is balanced." << endl; } else { cout << "The expression is not balanced." << endl; }//awasthi return 0; }

Imagine you are part of a team developing an advanced code editor called CodeCraft that provides real-time syntax checking for programmers. One of the crucial features of CodeCraft is to ensure that the parentheses, braces, and brackets in the code snippets are balanced. This feature plays a significant role in helping programmers catch and fix syntax errors immediately, enhancing their coding experience and productivity. Your task is to implement a program that simulates the syntax-checking functionality of CodeCraft. The program will utilize a stack data structure implemented using arrays and pointers to validate the balance of parentheses, braces, and brackets in an input string. By providing accurate and efficient syntax checking, your program will empower programmers to write clean and error-free code, minimizing the chances of runtime errors and enhancing the overall quality of their software projects. Note: This is a sample question asked in a Capgemini interview. Input format : The input consists of a string representing the expression containing parentheses, braces, and brackets.

#include <iostream> const int MAX_SIZE = 100; // Maximum size of the stack int stack[MAX_SIZE]; // Array to store stack elements int top = -1; // Variable to track the top of the stack // Function to push an element onto the stack void push(int value) { if (top == MAX_SIZE - 1) { return; } stack[++top] = value; std::cout << "Element " << value << " pushed onto the stack." << std::endl; } // Function to pop an element from the stack void pop() { if (top == -1) { std::cout << "Stack Underflow. Cannot perform pop operation." << std::endl; return; } int element = stack[top--]; std::cout << "Element " << element << " popped from the stack." << std::endl; } // Function to display the elements in the stack void displayStack() { if (top == -1) { std::cout << "Stack is empty." << std::endl; return; } std::cout << "Elements in the stack: "; for (int i = top; i >= 0; --i) { std::cout << stack[i] << " "; } std::cout << std::endl; } int main() { int choice, value; do { std::cin >> choice; switch (choice) { case 1: std::cin >> value; push(value); break; case 2: pop(); break; case 3: displayStack(); break; case 4: std::cout << "Exiting the program." << std::endl; break; default: std::cout << "Invalid choice. Please try again." << std::endl; } // std::cout << std::endl; } while (choice != 4); return 0; } //awasthi

Design a program to implement a stack using an array. The program should allow the user to perform stack operations such as push and pop. The stack should store integer values, and the maximum size of the stack should be defined as MAX_SIZE. The program should provide a menu-based interface for the user to choose the desired operation. The menu should include options to push an element onto the stack, pop an element from the stack, display the elements in the stack, and exit the program. The program should handle stack overflow and underflow conditions appropriately. Implement the program using an array without using a class structure. Note: This is a sample question asked in a TCS interview. Input format : The program expects the following inputs in a loop until the user chooses to exit. An integer indicating the choice of operation: 1: Push an element onto the stack 2: Pop an element from the stack 3: Display the elements in the stack 4: Exit the program If the choice is 1, the program expects an integer value to push onto the stack.

#include <stdio.h> #include <stdlib.h> #define MAX_SIZE 50 struct Stack { int arr[MAX_SIZE]; int top; }; void initialize(struct Stack* s) { s->top = -1; } int isFull(struct Stack* s) { return s->top == MAX_SIZE - 1; } int isEmpty(struct Stack* s) { return s->top == -1; } void push(struct Stack* s, int element) { if (!isFull(s)) { s->arr[++s->top] = element; } } int pop(struct Stack* s) { if (!isEmpty(s)) { return s->arr[s->top--]; } else { printf("Stack is empty. Cannot perform pop operation.\n"); return -1; } } void displayStack(struct Stack* s) { if (!isEmpty(s)) { printf("Elements in the stack:"); for (int i = s->top; i >= 0; i--) { printf(" %d", s->arr[i]); } printf("\n"); } else { printf("Stack is empty.\n"); } } int main() { struct Stack stack; initialize(&stack); int choice, element; while (1) { printf(""); scanf("%d", &choice); switch (choice) { case 1: printf(""); scanf("%d", &element); push(&stack, element); break; case 2: pop(&stack); break; case 3: displayStack(&stack); break; case 4: printf("Exiting the program.\n"); return 0; default: printf("Invalid choice.\n"); } } return 0; }//awasthi

Problem Statement Mira wants to create a program that allows her to manipulate a stack, perform push and pop operations, and display the elements in the stack. She would like a user-friendly interface that presents a menu with the following options: Push Operation: Add an integer to the top of the stack. Pop Operation: Remove and discard the integer from the top of the stack. Display Stack: Display all the elements currently in the stack, from top to bottom. Exit the Program: Terminate the program. Note: This is a sample question asked in a Capgemini interview. Input format : The program expects Ganga to enter a choice (an integer) corresponding to the operation she wants to perform: 1: Push the integer value onto the stack. 2: Pop an integer from the stack. 3: Display the elements currently in the stack. 4: Exit the program.

#include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10 struct Stack { int arr[MAX_SIZE]; int top; }; void initialize(struct Stack* s) { s->top = -1; } int isFull(struct Stack* s) { return s->top == MAX_SIZE - 1; } int isEmpty(struct Stack* s) { return s->top == -1; } void push(struct Stack* s, int element) { if (!isFull(s)) { s->arr[++s->top] = element; } } int pop(struct Stack* s) { if (!isEmpty(s)) { return s->arr[s->top--]; } else { printf("Stack is empty.\n"); return -1; } } double calculateAverage(struct Stack* s) { if (!isEmpty(s)) { int sum = 0; for (int i = 0; i <= s->top; i++) { sum += s->arr[i]; } return (double)sum / (s->top + 1); } else { printf("Stack is empty.\n"); return -1.0; } } void printStack(struct Stack* s) { if (!isEmpty(s)) { for (int i = 0; i <= s->top; i++) { printf("%d ", s->arr[i]); } printf("\n"); } else { printf("Stack is empty.\n"); } } int main() { struct Stack stack; initialize(&stack); int n, element; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &element); push(&stack, element); } printf("\n"); printStack(&stack); double average = calculateAverage(&stack); if (average >= 0.0) { printf("Average of the stack values: %.2lf\n", average); } int poppedValue = pop(&stack); if (poppedValue >= 0) { printf("Popped value: %d\n", poppedValue); } average = calculateAverage(&stack); if (average >= 0.0) { printf("Average of the stack values: %.2lf\n", average); } return 0; } //awasthi

You are tasked with implementing a program to perform various operations on a stack and calculate the average of its elements. The stack can hold a maximum of N = 10 elements. Your program should support the following operations: Push: Insert an element onto the stack. Pop: Remove and return the top element from the stack. If the stack is empty when trying to pop, print "Stack is empty." Print: Display the elements of the stack. If the stack is empty, print "Stack is empty." Average: Calculate and display the average of the elements in the stack.If the stack is empty, print "Stack is empty." The program should also handle this case when attempting to calculate the average of an empty stack. Note: This is a sample question asked in Capgemini recruitment. Input format : The first line contains an integer, n, representing the number of elements to be pushed onto the stack. The next line contains n space-separated integers, each representing an element to be pushed onto the stack. Output format : The output displays the following information: After pushing elements onto the stack, it prints the elements on the stack. If the stack is not empty, it prints the average of the stack values to two decimal places. If an element is popped from the stack, it prints the popped value. After popping elements, it prints the updated average of the stack values with two decimal places.

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Stack { struct Node* top; }; void initialize(struct Stack* stack) { stack->top = NULL; } int isEmpty(struct Stack* stack) { return stack->top == NULL; } void push(struct Stack* stack, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); exit(1); } newNode->data = data; newNode->next = stack->top; stack->top = newNode; } void pop(struct Stack* stack) { if (!isEmpty(stack)) { struct Node* temp = stack->top; stack->top = stack->top->next; free(temp); } } void deleteEven(struct Stack* stack) { struct Node* current = stack->top; struct Node* prev = NULL; while (current != NULL) { if (current->data % 2 == 0) { if (prev == NULL) { // If the first node is even struct Node* temp = current; current = current->next; stack->top = current; free(temp); } else { // If it's not the first node prev->next = current->next; free(current); current = prev->next; } } else { prev = current; current = current->next; } } } void printStack(struct Stack* stack) { struct Node* current = stack->top; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct Stack stack; initialize(&stack); int n, element; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &element); push(&stack, element); } printf("\n"); printStack(&stack); printf("\n"); deleteEven(&stack); printStack(&stack); return 0; }//awasthi

Naveen is learning about data structures and wants to implement a stack data structure using linked list. He also needs to perform a specific operation on the stack: delete even numbers from it. Can you help him write a program to create a stack, push elements onto it, delete even numbers, and display the final stack? implements a stack using a linked list and performs the following operations: push (int data): Add an integer element to the top of the stack. pop(): Remove the top element from the stack. deleteEven(): Delete all even numbers from the stack. printStack(): Display the elements in the stack after pushing and deleting even numbers. Note: This is a sample question asked in TCS recruitment. Input format : The first line contains an integer n, representing the number of elements Nandha wants to push onto the stack. The next line contains n space-separated integers, each representing the elements to be pushed onto the stack. Output format : The output displays the following format: After pushing all elements onto the stack, display the elements in the stack separated by a space. After removing even numbers from the stack using the deleteEven function, print the remaining elements in the stack separated by a space.