3 849
Obunachilar
Ma'lumot yo'q24 soatlar
-297 kunlar
-12730 kunlar
Postlar arxiv
3 849
#include <iostream>
#include <string>
const int MAX_SIZE = 100;
struct Book {
std::string title;
int quantity;
int restockPriority;
};
struct PriorityQueue {
Book arr[MAX_SIZE];
int size;
PriorityQueue() : size(0) {}
bool isEmpty() {
return size == 0;
}
void push(const Book& book) {
if (size == MAX_SIZE) {
std::cout << "Queue is full. Cannot add more books.\n";
return;
}
int index = size;
arr[size++] = book;
while (index > 0) {
int parent = (index - 1) / 2;
if (arr[index].restockPriority < arr[parent].restockPriority) { // Lower values indicate higher priority
std::swap(arr[index], arr[parent]);
index = parent;
} else {
break;
}
}
}
void pop() {
if (isEmpty()) {
std::cout << "No books in the inventory.\n";
return;
}
arr[0] = arr[--size];
int index = 0;
while (true) {
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
int smallest = index;
if (leftChild < size && arr[leftChild].restockPriority < arr[smallest].restockPriority) { // Lower values indicate higher priority
smallest = leftChild;
}
if (rightChild < size && arr[rightChild].restockPriority < arr[smallest].restockPriority) { // Lower values indicate higher priority
smallest = rightChild;
}
if (smallest != index) {
std::swap(arr[index], arr[smallest]);
index = smallest;
} else {
break;
}
}
}
Book top() {
if (isEmpty()) {
std::cout << "No books in the inventory.\n";
return {"", 0, 0};
}
return arr[0];
}
};
//awasthi
int main() {
PriorityQueue inventory;
int choice;
do {
std::cin >> choice;
std::cin.ignore(); // Clear the newline character from the previous input
switch (choice) {
case 1: {
std::string title;
int quantity, priority;
//std::cout << "Enter book title: ";
std::getline(std::cin, title);
//std::cout << "Enter quantity: ";
std::cin >> quantity;
//std::cout << "Enter restock priority (1-5): ";
std::cin >> priority;
if (priority < 1) priority = 1;
if (priority > 5) priority = 5;
inventory.push({title, quantity, priority});
std::cout << "Book added to the inventory.\n";
break;
}
case 2:
if (!inventory.isEmpty()) {
std::cout << "Restocked book: " << inventory.top().title << "\n";
inventory.pop();
} else {
std::cout << "No books in the inventory.\n";
}
break;
case 3:
if (!inventory.isEmpty()) {
std::cout << "Next book to restock: " << inventory.top().title << "\n";
} else {
std::cout << "No books in the inventory.\n";
}
break;
case 4:
std::cout << "Exiting the application.";
break;
default:
std::cout << "Invalid choice. Please enter a valid option.\n";
break;
}
} while (choice != 4);
return 0;
}
3 849
You are tasked with developing a simple inventory management system for a bookstore. The system should allow the bookstore staff to manage the inventory of books, prioritize restocking, and efficiently handle restocking operations.
The program uses a priority queue to manage the inventory. Each book in the inventory is represented by its title, the current quantity available, and a restock priority.
The book is restocked using restock priority. The restock priority is a value between 1 and 5, a low value indicating a higher priority. In the order of priority, 1 has high priority, the level gets reduced as the priority value increases, and 5 has low priority.
Include the following options:
1 - Add book to inventory
2 - Restock book
3 - View the next book to restock
4 - Exit
3 849
#include <stdio.h>
int main() {
int N1, N2;
// Read the input values for the first queue
scanf("%d", &N1);
int queue1[N1];
for (int i = 0; i < N1; i++) {
scanf("%d", &queue1[i]);
}
//awasthi
// Read the input values for the second queue
scanf("%d", &N2);
int queue2[N2];
for (int i = 0; i < N2; i++) {
scanf("%d", &queue2[i]);
}
// Compare the queues
if (N1 != N2) {
printf("The queues do not have the same elements in the same order.\n");
return 0;
}
for (int i = 0; i < N1; i++) {
if (queue1[i] != queue2[i]) {
printf("The queues do not have the same elements in the same order.\n");
return 0;
}
}
printf("The queues have the same elements in the same order.\n");
return 0;
}
3 849
You are developing a food delivery platform that handles orders from two different regions. The system uses two queues implemented using linked lists to manage incoming orders from each region.
Your task is to implement a program that checks if the two queues have the same order sequence.
Input format :
The first line consists of an integer N1, representing the number of orders for the first queue.
The second line consists of N1 space-separated integers, representing the order details for the first queue.
The third line consists of an integer N2, representing the number of orders for the second queue.
The fourth line consists of N2 space-separated integers, representing the order details for the second queue.
3 849
#include <stdio.h>
#define MAX_SIZE 100
// Function to initialize the queue
void initializeQueue(int queue[], int* front, int* rear) {
*front = -1;
*rear = -1;
}
// Function to check if the queue is empty
int isQueueEmpty(int front, int rear) {
return (front == -1 && rear == -1);
}
// Function to check if the queue is full
int isQueueFull(int rear) {
return (rear == MAX_SIZE - 1);
}
// Function to enqueue an element
void enqueue(int queue[], int* front, int* rear, int data) {
if (isQueueFull(*rear)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isQueueEmpty(*front, *rear)) {
*front = 0;
*rear = 0;
} else {
(*rear)++;
}
queue[*rear] = data;
}
// Function to dequeue an element
int dequeue(int queue[], int* front, int* rear) {
if (isQueueEmpty(*front, *rear)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
}
int data = queue[*front];
if (*front == *rear) {
*front = -1;
*rear = -1;
} else {
(*front)++;
}
return data;
}
// Function to count occurrences of a specific student ID in the attendance queue
int countOccurrences(int queue[], int front, int rear, int studentID) {
int count = 0;
for (int i = front; i <= rear; i++) {
if (queue[i] == studentID) {
count++;
}
}
return count;
}
int main() {
int N;
scanf("%d", &N);
int queue[MAX_SIZE];
int front, rear;
initializeQueue(queue, &front, &rear);
for (int i = 0; i < N; i++) {
int studentID;
scanf("%d", &studentID);
enqueue(queue, &front, &rear, studentID);
}
int studentIDToCount;
scanf("%d", &studentIDToCount);
int occurrences = countOccurrences(queue, front, rear, studentIDToCount);
//awasthi
printf("Occurrences of %d in the queue: %d\n", studentIDToCount, occurrences);
return 0;
}
3 849
You are developing a student attendance system for a school. As part of the system, you need to implement a functionality that counts the number of occurrences of a specific student's ID in the attendance queue. The attendance queue is implemented using an array-based queue.
The attendance system should adhere to the following requirements:
Initialize Queue: The system should initialize the attendance queue to an empty state.
Enqueue Student ID: As students enter the classroom, their ID (represented by integers) should be enqueued into the attendance queue.
Count Occurrences: Given a specific student ID, the system should count the number of times the ID appears in the attendance queue.
Input format :
The first line of input consists of an integer N, representing the number of students' IDs to enqueue.
The second line consists of N space-separated integers, each representing a student's ID to enqueue.
The third line consists of an integer representing the student ID for which the occurrences need to be counted.
3 849
#include <stdio.h>
#include <stdlib.h>
// Node structure for each element in the queue
struct Node {
int data;
struct Node* next;
};
// Queue structure
struct Queue {
struct Node* front; // Front of the queue
struct Node* rear; // Rear of the queue
};
// Function to initialize the queue
void initializeQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
// Function to check if the queue is empty
int isQueueEmpty(struct Queue* queue) {
return (queue->front == NULL);
}
// Function to enqueue an element
void enqueue(struct Queue* queue, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (isQueueEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
// Function to dequeue an element
int dequeue(struct Queue* queue) {
if (isQueueEmpty(queue)) {
return -1; // Return -1 if queue is empty
}
int data = queue->front->data;
struct Node* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return data;
}
// Function to generate and print binary sequence
void generateBinarySequence(int N) {
struct Queue queue;
initializeQueue(&queue);
// Enqueue the first binary number
enqueue(&queue, 1);
for (int i = 0; i < N; i++) {
// Dequeue a binary number from the queue
int binaryNumber = dequeue(&queue);
// Print the binary number
printf("%d ", binaryNumber);
// Generate the next binary numbers by appending 0 and 1
enqueue(&queue, binaryNumber * 10);
enqueue(&queue, binaryNumber * 10 + 1);
}
}
int main() {
int N;
scanf("%d", &N);
//awasthi
generateBinarySequence(N);
printf("\n");
return 0;
}
3 849
You are assigned to design and implement a program that generates and prints a binary sequence based on the user's input.
The program should utilize a queue data structure implemented using a linked list to efficiently generate and manage the binary sequence.
Input format :
The input consists of a single integer, N, representing the number of binary numbers to generate.
Output format :
The output prints the generated binary numbers, separated by space.
3 849
#include <stdio.h>
#include <stdlib.h>
// Node structure for each element in the queue
struct Node {
int data;
struct Node* next;
};
// Queue structure
struct Queue {
struct Node* front; // Front of the queue
struct Node* rear; // Rear of the queue
};
// Function to initialize the queue
void initializeQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
// Function to check if the queue is empty
int isQueueEmpty(struct Queue* queue) {
return (queue->front == NULL);
}
// Function to enqueue an element
void enqueue(struct Queue* queue, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (isQueueEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
// Function to dequeue an element
int dequeue(struct Queue* queue) {
if (isQueueEmpty(queue)) {
return -1; // Return -1 if queue is empty
}
int data = queue->front->data;
struct Node* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return data;
}
int main() {
struct Queue queue;
initializeQueue(&queue);
int num;
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
enqueue(&queue, num);
}
//awasthi
printf("Dequeued elements: ");
while (!isQueueEmpty(&queue)) {
printf("%d ", dequeue(&queue));
}
printf("\n");
return 0;
}
3 849
You've been assigned the challenge of developing a queue data structure using a linked list.
The program should allow users to interact with the queue by enqueuing positive integers and subsequently dequeuing and displaying elements.
Input format :
The input consists of a series of integers, one per line.
Enter positive integers into the queue.
Enter -1 to terminate input.
3 849
#include <iostream>
#include <string>
using namespace std;
// Node structure for each task
struct Node {
string description;
Node* next;
};
// Queue class for task management
class TaskQueue {
private:
Node* front; // Front of the queue
Node* rear; // Rear of the queue
public:
// Constructor
TaskQueue() {
front = NULL;
rear = NULL;
}
// Function to enqueue a task
void enqueueTask(string description) {
Node* newNode = new Node;
newNode->description = description;
newNode->next = NULL;
if (rear == NULL) {
front = newNode;
rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
// Function to get the front task
string getFrontTask() {
if (front == NULL) {
return "No tasks in the queue";
} else {
return front->description;
}
}
// Function to get the rear task
string getRearTask() {
if (rear == NULL) {
return "No tasks in the queue";
} else {
return rear->description;
}
}
};
int main() {
int N;
cin >> N;
cin.ignore();
TaskQueue taskQueue;
for (int i = 0; i < N; i++) {
string task;
getline(cin, task);
taskQueue.enqueueTask(task);
}
//awasthi
cout << "Front Task: " << taskQueue.getFrontTask() << endl;
cout << "Rear Task: " << taskQueue.getRearTask() << endl;
return 0;
}
3 849
You have been given the responsibility to create a program for task management in a to-do list using a Queue data structure. This to-do list has a predefined limit for the number of tasks it can hold, and the tasks will be stored in a Queue that is implemented using a linked list.
Your assignment is to develop the Queue data structure along with the required functions that enable the management of tasks within the to-do list.
The main functionalities of the task queue include:
Enqueue Task: Adding a task to the end of the queue.
Get Front Task: Retrieve the description of the first task in the queue.
Get Rear Task: Retrieve the description of the last task in the queue.
Input format :
The first line of input consists of an integer N, representing the number of tasks to enqueue.
The following N lines consist of the descriptions of the tasks, one per line.
Note: Use cin.ignore() to ignore the newline character after reading the value of N.
3 849
#include <iostream>
#include <string>
using namespace std;
const int MAX_SIZE = 100; // Maximum stack size
bool isOperator(char ch) {
if (ch == '+' || ch == '-' ch == '*'|| ch == '/') {
return true;
}
return false;
}
int precedence(char ch) {
if (ch == '+' ch == '-') {
return 1;
} else if (ch == '*' ch == '/') {
return 2;
}
return 0;
}
void infixToPostfix(string infix, string& postfix) {
char stack[MAX_SIZE];
int top = -1; // Stack top pointer
for (int i = 0; i < infix.length(); i++) {
char ch = infix[i];
if (isalnum(ch)) {
postfix += ch;
} else if (isOperator(ch)) {
while (top >= 0 && stack[top] != '(' && precedence(ch) <= precedence(stack[top])) {
postfix += stack[top];
top--;
}
top++;
stack[top] = ch;
} else if (ch == '(') {
top++;
stack[top] = ch;
} else if (ch == ')') {
while (top >= 0 && stack[top] != '(') {
postfix += stack[top];
top--;
}
if (top >= 0 && stack[top] == '(') {
top--;
}
}
}
while (top >= 0) {
postfix += stack[top];
top--;
}
}
int main() {
string infix, postfix;
cin >> infix;
//awasthi
infixToPostfix(infix, postfix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
3 849
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.
3 849
#include <iostream>
#include <cstring>
using namespace std;
bool isOperator(char c) {
return (c == '+'|| c == '-' || c == '*' || c == '/');
}
bool isValidPostfixExpression(const char* postfix) {
int stack[100];
int top = -1;
for (int i = 0; postfix[i]; i++) {
char c = postfix[i];
if (isdigit(c)) {
int operand = 0;
while (isdigit(c)) {
operand = operand * 10 + (c - '0');
i++;
c = postfix[i];
}
stack[++top] = operand;
} else if (isOperator(c)) {
if (top < 1) {
return false;
}
int operand2 = stack[top--];
int operand1 = stack[top--];
switch (c) {
case '+':
stack[++top] = operand1 + operand2;
break;
case '-':
stack[++top] = operand1 - operand2;
break;
case '*':
stack[++top] = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
return false; // Division by zero
}
stack[++top] = operand1 / operand2;
break;
}
} else if (c != ' ') {
return false;
}
}
return (top == 0);
}
int main() {
char postfixExpression[100];
cin.getline(postfixExpression, sizeof(postfixExpression));
//awasthi
if (isValidPostfixExpression(postfixExpression)) {
cout << "Valid postfix expression" << endl;
} else {
cout << "Invalid postfix expression" << endl;
}
return 0;
}
3 849
Venu is currently learning about postfix expressions in his computer science class. He has recently written a program to validate whether a given postfix expression is valid or not. However, he wants you to help him.
A valid postfix expression is one that adheres to the following rules:
It contains only digits, operators (+, -, *, /), and spaces.
It has valid operator placement, meaning that there must be at least two operands for every operator, and no operands should be left unused.
Division by zero is not allowed.
Example
Input 1
23+
Output 1
Valid postfix expression
3 849
#include <iostream>
#include <string>
using namespace std;
const int MAX_EXPR_LEN = 100;
int is_operator(char c) {
if (c == '+' c == '-' c == '*' c == '/' c == '^' c == '(' c == ')') {
return 1;
}
return 0;
}
int precedence(char c) {
if (c == '^') {
return 3;
} else if (c == '*' c == '/') {
return 2;
} else if (c == '+' c == '-') {
return 1;
} else {
return 0;
}
}
struct CharStack {
char data[MAX_EXPR_LEN];
int top;
CharStack() {
top = -1;
}
void push(char c) {
if (top < MAX_EXPR_LEN - 1) {
data[++top] = c;
} else {
cout << "Stack overflow!" << endl;
exit(EXIT_FAILURE);
}
}
char pop() {
if (top >= 0) {
return data[top--];
} else {
cout << "Stack underflow!" << endl;
exit(EXIT_FAILURE);
}
}
char peek() {
if (top >= 0) {
return data[top];
} else {
return '\0'; // Return null character for an empty stack.
}
}
bool empty() {
return top == -1;
}
};
string infix_to_postfix(const string& infix) {
CharStack operators;
string postfix;
char c;
for (int i = 0; i < infix.length(); i++) {
c = infix[i];
if (!is_operator(c)) {
postfix += c;
} else {
if (c == '(') {
operators.push(c);
} else if (c == ')') {
while (operators.peek() != '(') {
postfix += operators.pop();
}
operators.pop(); // Pop the '('
} else {
while (!operators.empty() && operators.peek() != '(' && precedence(c) <= precedence(operators.peek())) {
postfix += operators.pop();
}
operators.push(c);
}
}
}
while (!operators.empty()) {
postfix += operators.pop();
}
return postfix;
}
int main() {
int num_expressions;
string infix, postfix;
cin >> num_expressions;
//awasthi
for (int i = 1; i <= num_expressions; i++) {
cin >> infix;
postfix = infix_to_postfix(infix);
cout << "Postfix expression " << i << ": " << postfix << endl;
}
return 0;
}
3 849
Yogi is learning data structure and he wants to write a program to accept multiple infix expressions from the user and convert them into postfix expressions using a stack-based algorithm. The program should prompt the user to enter the number of expressions they wish to convert, and then accept each expression one by one. The program should validate each expression for proper syntax before converting it to postfix.
After converting each infix expression to a postfix, the program should print the corresponding postfix expression to the console. Finally, the program should ask the user if they want to continue converting more expressions or exit the program.
Note: This is a sample question asked in TCS recruitment.
Input format :
The first line of input contains an integer n denoting the number of infix expressions to be converted.
The next n lines contain the infix expressions to be converted.
Output format :
The output prints the corresponding postfix expression for n inputs on separate lines.
3 849
#include <iostream>
#include <cstring>
using namespace std;
int get_precedence(char op) {
if (op == '*' op == '/')
return 2;
else if (op == '+' op == '-')
return 1;
else
return 0;
}
bool is_operator(char op) {
return op == '+' op == '-' op == '*' || op == '/';
}
void infix_to_postfix(char *infix, char *postfix) {
char stack[100];
int top = -1;
int i = 0, j = 0;
while (infix[i] != '\0') {
if (isdigit(infix[i])) {
postfix[j++] = infix[i++];
} else if (is_operator(infix[i])) {
while (top >= 0 && get_precedence(stack[top]) >= get_precedence(infix[i])) {
postfix[j++] = stack[top--];
}
stack[++top] = infix[i++];
} else if (infix[i] == '(') {
stack[++top] = infix[i++];
} else if (infix[i] == ')') {
while (top >= 0 && stack[top] != '(') {
postfix[j++] = stack[top--];
}
if (top >= 0 && stack[top] == '(') {
top--;
}
i++;
} else {
i++;
}
}
while (top >= 0) {
postfix[j++] = stack[top--];
}
postfix[j] = '\0';
}
int main() {
char infix[100], postfix[100];
cin.getline(infix, 100);
//awasthi
infix_to_postfix(infix, postfix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
3 849
Nithish is studying computer science and is currently learning about expressions and their conversion from infix notation to postfix notation. He has a programming assignment and needs your help to implement an infix-to-postfix conversion algorithm. Can you help him?
Write a program that takes an infix expression as input and converts it into a postfix expression. The program should support the following operators: '+', '-', '*', and '/'. It should also handle parentheses '(' and ')' to indicate the order of operations.
Note: This is a sample question asked in HCL recruitment.
Input format :
The input consists of an infix expression that includes only digits (0–9) and operators (+, -, *, /).
Output format :
The output displays the equivalent postfix expression of the given infix expression.
