ar
Feedback
inactive

inactive

قناة بسيطة

...

إظهار المزيد
3 849
المشتركون
لا توجد بيانات24 ساعات
-297 أيام
-12730 أيام
أرشيف المشاركات
#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); //awasthi printf("\n"); deleteEven(&stack); printStack(&stack); return 0; }

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.

#include <stdio.h> #define MAX_SIZE 30 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 search(struct Stack *s, int element) { if (!isEmpty(s)) { for (int i = 0; i <= s->top; i++) { if (s->arr[i] == element) { return 1; // Element found } } } return 0; // Element not found } int main() { struct Stack stack; initialize(&stack); int n, s, element; scanf("%d", &n); if (n > MAX_SIZE) { printf("STACK is overflow\n"); return 0; } for (int i = 0; i < n; i++) { scanf("%d", &element); push(&stack, element); } scanf("%d", &s); if (isEmpty(&stack)) { printf("The STACK is empty\n"); } else if (search(&stack, s)) { printf("Element found\n"); } else { printf("Element not found\n"); } return 0; }//awasthi

Ethan and Alya are working together on a simple stack-based program to search for elements in a fixed-size (30) stack implemented using an array. The program has two main operations: pushing elements onto the stack and searching for elements within the stack. They need to implement these operations efficiently. Write a program to accomplish the above. Note: This is a sample question asked during Infosys recruitment. Input format : The first line of input consists of 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. The next line of input consists of an integer s, representing the element to be searched for in the stack. Output format : The output prints one of the following outputs based on the input: If the stack is full: "STACK is overflow" If the stack is empty: "The STACK is empty" If the searched element is found in the stack: "Element found" If the searched element is not found in the stack: "Element not found"

#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; } } void pop(struct Stack *s) { if (!isEmpty(s)) { s->top--; } else { printf("Stack is empty\n"); } } void getMax(struct Stack *s) { if (!isEmpty(s)) { int max = s->arr[0]; for (int i = 1; i <= s->top; i++) { if (s->arr[i] > max) { max = s->arr[i]; } } printf("Maximum element: %d\n", max); } else { printf("Maximum element: -1\n"); } } 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 choice, element; while (1) { scanf("%d", &choice); switch (choice) { case 1: scanf("%d", &element); push(&stack, element); break; case 2: pop(&stack); break; case 3: getMax(&stack); break; case 4: printStack(&stack); break; case 5: return 0; default: printf("Invalid choice\n"); } } return 0; }//awasthi108

Thiru is a software engineer who loves to work with data structures. Recently, he has been learning about stacks and wants to implement a stack that not only allows him to push and pop elements but also quickly find the maximum element in the stack. Write a program that simulates a stack with the following operations: Push: Insert an element into the stack. Pop: Remove the top element from the stack. If stack is empty, It prints "stack is empty". Get Maximum: Find and print the maximum element currently in the stack. If stack is empty, it displays "-1". Print Stack: Display all elements currently in the stack. If the stack is empty, it displays the "stack is empty". Exit: Terminate the program. Input format : The input consists of a single integer. Each integer corresponds to a choice in the menu for interacting with the stack. The choices are as follows: 1: Push an element onto the stack. The next input is the element to push, separated by a space. 2: Pop the top element from the stack. 3: Find and print the maximum element in the stack. 4: Print all elements currently in the stack. 5: Exit the program.

// You are using GCC #include <iostream> #include <list> using namespace std; bool haveSameOrderSequence(list<int>& queue1, list<int>& queue2) { if (queue1.size() != queue2.size()) { return false; } auto it1 = queue1.begin(); auto it2 = queue2.begin(); while (it1 != queue1.end() && it2 != queue2.end()) { if (*it1 != *it2) { return false; } it1++; it2++; } return true; } int main() { int N1, N2; cin >> N1; list<int> queue1; for (int i = 0; i < N1; i++) { int order; cin >> order; queue1.push_back(order); } cin >> N2; list<int> queue2; for (int i = 0; i < N2; i++) { int order; cin >> order; queue2.push_back(order); } if (haveSameOrderSequence(queue1, queue2)) { cout << "The queues have the same elements in the same order." << endl; } else { cout << "The queues do not have the same elements in the same order." << endl; } return 0; } //awasthi108

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. Output format : The output prints whether the queues have the same elements or different elements.

// You are using GCC #include <iostream> using namespace std; class Queue { private: int front, rear, capacity; int* arr; public: Queue(int size) { capacity = size; front = rear = -1; arr = new int[size]; } ~Queue() { delete[] arr; } bool isEmpty() { return front == -1; } bool isFull() { return (rear + 1) % capacity == front; } void enqueue(int value) { if (isFull()) { cout << "Queue is full. Cannot enqueue." << endl; return; } if (isEmpty()) { front = 0; rear = 0; } else { rear = (rear + 1) % capacity; } arr[rear] = value; } int dequeue() { if (isEmpty()) { cout << "Queue is empty." << endl; return -1; } int value = arr[front]; if (front == rear) { front = rear = -1; } else { front = (front + 1) % capacity; } return value; } }; int main() { int N; cin >> N; Queue queue(N); for (int i = 0; i < N; i++) { int value; cin >> value; queue.enqueue(value); } cout << "Dequeuing elements: "; while (!queue.isEmpty()) { int value = queue.dequeue(); cout << value << " "; } cout << endl; return 0; }//awasthi108

Problem Statement You are tasked with implementing a Queue data structure using an array-based approach. Write a program that performs the following: Enqueue Operation: Implement the enqueue operation to insert integers into the queue. Dequeue Operation: After enqueuing all the elements, perform the dequeue operation. Print the dequeued elements in the order they were enqueued, separated by space. The dequeue operation should continue until the queue is empty. Input format : The first line of input consists of an integer N, representing the size. The second line consists of N integers, representing the elements inside the queue. Output format : The output prints the space-separated dequeued elements.

// You are using GCC #include <iostream> #include <queue> using namespace std; void generateBinarySequence(int N) { queue<string> binaryQueue; binaryQueue.push("1"); for (int i = 0; i < N; i++) { string front = binaryQueue.front(); binaryQueue.pop(); cout << front << " "; string next1 = front + "0"; string next2 = front + "1"; binaryQueue.push(next1); binaryQueue.push(next2); } } int main() { int N; //cout << "Enter the number of binary numbers to generate: "; cin >> N; generateBinarySequence(N); return 0; } //awasthi108

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.

#include <iostream> struct Node { int pages; Node* next; }; struct Queue { Node* front; Node* rear; }; void initializeQueue(Queue* q) { q->front = nullptr; q->rear = nullptr; } bool isEmpty(Queue* q) { return q->front == nullptr; } void enqueue(Queue* q, int pages) { Node* newNode = new Node; newNode->pages = pages; newNode->next = nullptr; if (isEmpty(q)) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } bool dequeue(Queue* q, int& pages) { if (isEmpty(q)) { return false; } Node* temp = q->front; pages = temp->pages; q->front = q->front->next; if (q->front == nullptr) { q->rear = nullptr; } delete temp; return true; } void display(Queue* q) { if (isEmpty(q)) { std::cout << "Queue is empty." << std::endl; } else { Node* current = q->front; std::cout << "Print jobs in the queue: "; while (current != nullptr) { std::cout << current->pages << " pages "; current = current->next; } std::cout << std::endl; } } int main() { Queue q; int option; int pages; initializeQueue(&q); while (true) { if (!(std::cin >> option) || option == 4) { break; } switch (option) { case 1: if (!(std::cin >> pages)) { break; } enqueue(&q, pages); std::cout << "Print job with " << pages << " pages is enqueued." << std::endl; break; case 2: if (dequeue(&q, pages)) { std::cout << "Processing print job: " << pages << " pages" << std::endl; } else { std::cout << "Queue is empty." << std::endl; } break; case 3: display(&q); break; default: std::cout << "Invalid option." << std::endl; break; } } return 0; }//awasthi108

Problem Statement You are designing a printer queue for a computer lab. The lab has a printer that can only process one print job at a time. Your task is to implement a queue using a linked list to manage print jobs sent to the printer. Each print job is represented by the number of pages it contains. Implement the following operations: Enqueue Print Job: Add a print job to the end of the queue. Dequeue Print Job: Remove and process the next print job in the queue. Display Queue: Display the print jobs in the queue. Your program should handle these operations efficiently to ensure that print jobs are processed in the order they are received. Input format : The input consists of an integer option representing the action to be performed: Option 1: Enqueue a print job. Option 2: Dequeue and process the next print job. Option 3: Display the print jobs in the queue. Any other integer: Invalid option. If the option is 1, the next line contains an integer representing the number of pages in the print job to be enqueued. Output format : For each operation, the program should provide the appropriate output messages: If option 1 is chosen, display a message indicating that the print job has been enqueued. If option 2 is chosen and a print job is dequeued, display a message indicating that the print job has been processed. If option 2 is chosen and the queue is empty, display a message indicating that the queue is empty. If option 3 is chosen, display the current print jobs in the queue. If any other option other than 1, 2, 3 is given, print "Invalid option".

#include <iostream> #include <stack> #include <string> using namespace std; int precedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; // For other characters (like '(' and ')') } string infixToPostfix(const string& infix) { stack<char> operators; string postfix = ""; for (char ch : infix) { if (isdigit(ch) || ch == '.') { postfix += ch; } else if (ch == '(') { operators.push(ch); } else if (ch == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); operators.pop(); } if (!operators.empty() && operators.top() == '(') operators.pop(); } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { while (!operators.empty() && operators.top() != '(' && precedence(operators.top()) >= precedence(ch)) { postfix += operators.top(); operators.pop(); } operators.push(ch); } } while (!operators.empty()) { postfix += operators.top(); operators.pop(); } return postfix; } int main() { string infix; // cout << "Enter the infix expression: "; getline(cin, infix); string postfix = infixToPostfix(infix); cout << "The RPN is: " << postfix << endl; return 0; } //awasthi108

Puma is working on a project that involves processing mathematical expressions. She needs a program to convert infix expressions into postfix notation, which will make further processing easier. To streamline her work, Puma is seeking your help in developing a program for this purpose. Your task is to create a program that takes an infix expression as input and converts it into postfix notation. Puma is looking for a program that can handle various arithmetic operations, parentheses, and operands in the input expression. Note: This is a sample question asked in an AMCAT interview. Input format : The input consists of a string representing the infix expression. A space separates every value in the expression, and it contains integers, decimal points, arithmetic operators (+, -, *, /), and parentheses (). Output format : The output prints the Reverse Polish Notation of the input expression without any space between the values in the expression. The expression is printed in the format: "The RPN is: <>"

// You are using GCC #include <iostream> #include <stack> #include <string> using namespace std; int precedence(char op) { if (op == '^') return 3; if (op == '*' op == '/') return 2; if (op == '+' op == '-') return 1; return 0; } string infixToPostfix(const string& infix) { string postfix = ""; stack<char> operators; for (char c : infix) { if (isalnum(c)) { postfix += c; } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); operators.pop(); } operators.pop(); // Pop the '(' } else { while (!operators.empty() && 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 an infix arithmetic expression: "; cin >> infix; string postfix = infixToPostfix(infix); cout<< postfix << endl; //awasthi108 return 0; }

Suppose you are designing a compiler for a programming language. As part of the compilation process, you need to convert an infix arithmetic expression to its postfix form. The postfix form is often used in computer algorithms to evaluate arithmetic expressions efficiently. Write a program that takes an infix arithmetic expression as input from the user and converts it to postfix form. The program must be designed to handle input expressions with parentheses and convert them accordingly. Note: This is a sample question asked in an HCL interview. Input format : The input consists of a string representing an infix expression. Output format : The output prints the postfix expression for the given input expression.

// You are using GCC #include <iostream> #include <stack> #include <string> using namespace std; // Function to check if a character is an operator bool isOperator(char c) { return (c == '+' c == '-' c == '*' c == '/'); } // Function to assign precedence to operators int precedence(char c) { if (c == '+' c == '-') return 1; if (c == '*' || c == '/') return 2; return 0; } // Function to convert infix expression to postfix notation string infixToPostfix(string infix) { stack<char> s; string postfix = ""; for (char c : infix) { if (isalpha(c)) { postfix += c; } else if (c == '(') { s.push(c); } else if (c == ')') { while (!s.empty() && s.top() != '(') { postfix += s.top(); s.pop(); } s.pop(); // Remove the '(' } else if (isOperator(c)) { while (!s.empty() && precedence(s.top()) >= precedence(c)) { postfix += s.top(); s.pop(); } s.push(c); } } while (!s.empty()) { postfix += s.top(); s.pop(); } return postfix; } int main() { string infix; // cout << "Enter an infix expression: "; getline(cin, infix); string postfix = infixToPostfix(infix); cout << postfix << endl; return 0; }//awasthi108

Kiruthika is working on a programming project that requires the conversion of infix mathematical expressions into postfix notation. To accomplish this task efficiently, she seeks your assistance in developing a program. Your goal is to create a program that accepts an infix expression as input and converts it into postfix notation. Kiruthika is interested in a program that can handle various arithmetic operations, parentheses, and operands within the input expression. Note: This is a sample question asked in a Deloitte interview. Input format : The input consists of the infix expression and arithmetic operators (+, -, *, /), and parentheses () with a single space. Output format : The output prints the postfix expression of the input expression without any space. Code constraints : The input includes (, ), +, -, * / and upper alphabets.