Sheet
Sheet
#include <stack>
using namespace std;
bool isBalanced(const string& expression) {
stack<char> s;
for (char c : expression) {
switch (c) {
case '(':
case '{':
case '[':
s.push(c);
break;
case ')':
if (!s.empty() && s.top() == '(') {
s.pop();
} else {
return false; // unbalanced
}
break;
case '}':
if (!s.empty() && s.top() == '{') {
s.pop();
} else {
return false; // unbalanced
}
break;
case ']':
if (!s.empty() && s.top() == '[') {
s.pop();
} else {
return false; // unbalanced
}
break;
}
}
return s.empty();
}
int main() {
string expression;
cout << "Enter An Expression: ";
getline(cin, expression);
if (isBalanced(expression)) {
cout << "This Expression Is Balanced" << endl;
} else {
cout << "This Expression Is Unbalanced" << endl;
}
return 0;
}
#include <iostream>
#include <stack>
#include <string>
using namespace std; for (char c : postfix) {
bool isOperator(char c) { if (isdigit(c)) {
return c == '+' || c == '-' || c == '*' || c == '/'; operands.push(c - '0');
} } else if (isOperator(c)) {
int precedence(char c) { double operand2 = operands.top();
if (c == '+' || c == '-') { operands.pop();
return 1; double operand1 = operands.top();
} else if (c == '*' || c == '/') { operands.pop();
return 2; double result;
} else { switch (c) {
return 0; case '+':
} result = operand1 + operand2;
} break;
string infixToPostfix(string infix) { case '-':
stack<char> operators; result = operand1 - operand2;
string postfix; break;
for (char c : infix) { case '*':
if (isalnum(c)) { result = operand1 * operand2;
postfix += c; break;
} else if (isOperator(c)) { case '/':
while (!operators.empty() && precedence(operators.top()) result = operand1 / operand2;
>= precedence(c)) { break;
postfix += operators.top(); }
operators.pop(); operands.push(result);
} }
operators.push(c); }
} else if (c == '(') { if (operands.size() != 1) {
operators.push(c); throw invalid_argument("Invalid expression");
} else if (c == ')') { }
while (!operators.empty() && operators.top() != '(') { return operands.top();
postfix += operators.top(); }
operators.pop(); int main() {
} string infixExpression;
operators.pop(); cout << "Enter an infix expression: ";
} else { getline(cin, infixExpression);
throw invalid_argument("Invalid character in expression"); string postfixExpression =
} infixToPostfix(infixExpression);
} cout << "Postfix expression: " <<
while (!operators.empty()) { postfixExpression << endl;
postfix += operators.top(); double result = evaluatePostfix(postfixExpression);
operators.pop(); cout << "Result: " << result << endl;
} return 0;
return postfix; }
}
double evaluatePostfix(string postfix) {
stack<double> operands;
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr) {
Node* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}
void printList(Node* head) {
while (head) {
cout << head->data << " -> ";
head = head->next;
}
cout << "NULL" << endl;
}
int main() {
Node* head = new Node{1};
head->next = new Node{2};
head->next->next = new Node{3};
head->next->next->next = new Node{4};
cout << "The Original List Is: ";
printList(head);
head = reverseList(head);
cout << "The Reversed List Is: ";
printList(head);
return 0;
}
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<string> undoStack;
stack<string> redoStack;
string currentText = "";
void displayText() {
cout << "Current text: " << currentText << endl;
}
void typeText(string text) {
undoStack.push(currentText);
currentText += text;
redoStack = stack<string>(); // Clear redo stack on new actions
displayText();
}
void deleteText() {
if (currentText.length() > 0) {
undoStack.push(currentText);
currentText.pop_back();
redoStack = stack<string>();
displayText();
} else {
cout << "Cannot delete: Text is empty." << endl;
}
}
void undo() {
if (!undoStack.empty()) {
redoStack.push(currentText);
currentText = undoStack.top();
undoStack.pop();
displayText();
} else {
cout << "Cannot undo: No more actions to undo." << endl;
}
}
void redo() {
if (!redoStack.empty()) {
undoStack.push(currentText);
currentText = redoStack.top();
redoStack.pop();
displayText();
} else {
cout << "Cannot redo: No more actions to redo." << endl;
}
}
int main() {
int choice;
do {
cout << "\nText Editor Menu:" << endl;
cout << "1. Type Text" << endl;
cout << "2. Delete Text" << endl;
cout << "3. Undo" << endl;
cout << "4. Redo" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
string text;
cout << "Enter text to type: ";
cin >> text;
getline(cin, text);
typeText(text);
break;
}
case 2:
deleteText();
break;
case 3:
undo();
break;
case 4:
redo();
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice." << endl;
}
} while (choice != 5);
return 0;
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
class TaskScheduler {
public:
void addTask(const std::string& task);
void scheduleTasks();
void executeTask();
private:
queue<string> taskQueue;
vector<string> completedTasks;
};
void TaskScheduler::scheduleTasks() {
cout << "Scheduled tasks: ";
while (!taskQueue.empty()) {
cout << taskQueue.front() << ", ";
completedTasks.push_back(taskQueue.front());
taskQueue.pop();
}
cout << "\n";
}
void TaskScheduler::executeTask() {
cout << "Execution order: ";
for (const auto& task : completedTasks) {
cout << task << " -> ";
}
cout << "\n";
}
int main() {
TaskScheduler scheduler;
scheduler.addTask("Task A");
scheduler.addTask("Task B");
scheduler.addTask("Task C");
scheduler.scheduleTasks();
scheduler.executeTask();
return 0;
}