3 849
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-297 روز
-12730 روز
آرشیو پست ها
3 849
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
//awasthi
// Function to perform bubble sort in descending order
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
// Taking input from the user
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sorting the elements in descending order
bubbleSort(arr, n);
// Printing the sorted elements
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
3 849
John is a software engineer working on an application that requires sorting elements of an array in a dequeue format.
Write a program to help John implement the functionality to sort the elements in an array in descending order.
Input format :
The first line of input consists of an integer N, representing the number of elements.
The second line consists of N space-separated elements.
Output format :
The output prints the elements sorted in descending order.
3 849
You are tasked with implementing a double-ended queue, a data structure that allows elements to be added or removed from both ends.
You need to create a program that provides the following functionality:
Initialize an empty deque.
Insert elements at the front of the deque.
Insert elements at the back of the deque.
Print the elements of the deque.
Input format :
The first line of input consists of the first integer to insert at the front.
The second line consists of the second integer to insert at the front.
The third line consists of the first integer to insert at the back.
The fourth line consists of the second integer to insert at the back.
The fifth line consists of the third integer to insert at the front.
The sixth line consists of the third integer to insert at the back.
3 849
#include <stdio.h>
#include <stdlib.h>
// Structure for a node in the doubly linked list
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to insert an element at the front of the deque
void insertFront(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
// Function to insert an element at the back of the deque
void insertBack(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
// Function to print the elements of the deque
void printDeque(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
// Initializing an empty deque
struct Node* head = NULL;
// Taking input from the user
int num1, num2, num3, num4, num5, num6;
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &num3);
scanf("%d", &num4);
scanf("%d", &num5);
scanf("%d", &num6);
//awasthi
// Inserting elements at the front and back of the deque
insertFront(&head, num1);
insertFront(&head, num2);
insertBack(&head, num3);
insertBack(&head, num4);
insertFront(&head, num5);
insertBack(&head, num6);
// Printing the elements of the deque
printDeque(head);
return 0;
}
3 849
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for deque node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Structure for deque
struct Deque {
struct Node* front;
struct Node* rear;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to create an empty deque
struct Deque* createDeque() {
struct Deque* deque = (struct Deque*)malloc(sizeof(struct Deque));
deque->front = NULL;
deque->rear = NULL;
return deque;
}
// Function to insert an element at the front of the deque
void insertFront(struct Deque* deque, int data) {
struct Node* newNode = createNode(data);
if (deque->front == NULL) {
deque->front = newNode;
deque->rear = newNode;
} else {
newNode->next = deque->front;
deque->front->prev = newNode;
deque->front = newNode;
}
}
// Function to delete an element from the front of the deque
void deleteFront(struct Deque* deque) {
if (deque->front == NULL) {
return;
} else {
struct Node* temp = deque->front;
deque->front = deque->front->next;
if (deque->front == NULL) {
deque->rear = NULL;
} else {
deque->front->prev = NULL;
}
free(temp);
}
}
// Function to insert an element at the rear of the deque
void insertRear(struct Deque* deque, int data) {
struct Node* newNode = createNode(data);
if (deque->rear == NULL) {
deque->front = newNode;
deque->rear = newNode;
} else {
newNode->prev = deque->rear;
deque->rear->next = newNode;
deque->rear = newNode;
}
}
// Function to delete an element from the rear of the deque
void deleteRear(struct Deque* deque) {
if (deque->rear == NULL) {
return;
} else {
struct Node* temp = deque->rear;
deque->rear = deque->rear->prev;
if (deque->rear == NULL) {
deque->front = NULL;
} else {
deque->rear->next = NULL;
}
free(temp);
}
}
// Function to check if the deque is empty
int isEmpty(struct Deque* deque) {
return (deque->front == NULL);
}
// Function to find all occurrences of a pattern in a text document using sliding window algorithm
void findPatternOccurrences(char* text, char* pattern) {
int textLength = strlen(text);
int patternLength = strlen(pattern);
if (patternLength > textLength) {
printf("Pattern is longer than the string\n");
return;
}
struct Deque* deque = createDeque();
int i, j;
for (i = 0; i <= textLength - patternLength; i++) {
for (j = 0; j < patternLength; j++) {
if (text[i + j] != pattern[j]) {
break;
}
}
if (j == patternLength) {
insertRear(deque, i);
}
}//awasthi
if (isEmpty(deque)) {
printf("Pattern not found\n");
} else {
while (!isEmpty(deque)) {
printf("Pattern found at index %d\n", deque->front->data);
deleteFront(deque);
}
}
}
int main() {
char text[1000];
char pattern[100];
fgets(text, sizeof(text), stdin);
fgets(pattern, sizeof(pattern), stdin);
text[strcspn(text, "\n")] = '\0';
pattern[strcspn(pattern, "\n")] = '\0';
findPatternOccurrences(text, pattern);
return 0;
}
3 849
You are working on a text processing system for a search engine. As part of the system, you need to implement a pattern-matching algorithm using a sliding window approach.
Given a large text document and a pattern, you are required to find all occurrences of the pattern in the text document efficiently.
Your task is to write a program that performs the following operations:
Read the large text document and store it in memory.
Read the pattern from the user.
Implement a sliding window algorithm using a linked list-based deque to find all occurrences of the pattern in the text document.
Display the positions (starting indices) of all occurrences of the pattern in the text document.
Write a program to implement the above operations and display the positions of all occurrences of the pattern in the text document.
Your program should prompt the user to enter the required inputs and then output the positions of all occurrences of the pattern.
3 849
#include <iostream>
using namespace std;
#define MAX 100
class Deque {
int arr[MAX];
int front;
int rear;
int size;
public:
Deque(int size)
{
front = -1;
rear = 0;
this->size = size;
}
// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};
bool Deque::isFull()
{
return ((front == 0 && rear == size - 1)
|| front == rear + 1);
}
bool Deque::isEmpty() {
return (front == -1);
}
void Deque::insertfront(int key)
{
if (isFull()) {
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else if (front == 0)
front = size - 1;
else
front = front - 1;
arr[front] = key;
}
void Deque ::deletefront()
{
if (isEmpty()) {
return;
}
if (front == rear) {
front = -1;
rear = -1;
}
else
if (front == size - 1)
front = 0;
else
front = front + 1;
}
int Deque::getFront()
{
if (isEmpty()) {
return -1;
}
return arr[front];
}
int main()
{
int len;
cin>>len;
Deque dq(len);
int val;
for(int i=0;i<len;i++)
{
cin>>val;
dq.insertfront(val);
}
cout << "The front element is " << dq.getFront() <<"."<< endl;
int del;
cin>>del;
for(int i=0;i<del;i++)
{
dq.deletefront();
}//awasthi
cout << "After deletion, the front element becomes " << dq.getFront() <<"."<< endl;
return 0;
}
3 849
Write a program to implement a double-ended queue and perform the following operations:
Insert at front
Delete at front
Example
Input:
5
1 2 3 4 5
2
Output:
The front element is 5.
After deletion, the front element becomes 3.
Explanation:
The first line of input contains the length of the deque which is 5 in this case.
The second line contains the 5 elements to be inserted at the front of the deque.
The third line contains the number of elements to be deleted from the front of the deque which is 2 in this case.
After inserting the 5 elements at the front of the deque, the current front element is 5. Then we delete the first two elements from the front of the Deque. After deletion, the front element becomes 3. Hence the output shows "The front element is 5." and "After deletion, the front element becomes 3.".
3 849
#include <iostream>
#include <deque>
int main() {
int N;
std::cin >> N;
//awasthi
std::deque<int> myDeque;
for (int i = 0; i < N; i++) {
int element;
std::cin >> element;
myDeque.push_back(element);
}
while (!myDeque.empty()) {
std::cout << myDeque.back() << " ";
myDeque.pop_back();
}
return 0;
}
3 849
You are working on a program that manages a deque implemented using an array. The deque initially contains a set of integer elements.
Write a program to implement the reversal operation on the deque. Your program should prompt the user to enter the elements of the deque and then output the reversed elements.
Note: This kind of question will be helpful in clearing Infosys recruitment.
Input format :
The first line of input consists of the number of elements N in the deque.
The second line consists of the N deque elements, separated by space.
3 849
#include <iostream>
#include <algorithm>
struct Packet {
int priority;
std::string data;
};
struct PacketComparator {
bool operator()(const Packet& p1, const Packet& p2) {
// Higher priority packets have lower priority values
return p1.priority > p2.priority;
}
};
struct PriorityQueue {
Packet* packets;
int capacity;
int size;
PriorityQueue(int maxCapacity) {
capacity = maxCapacity;
packets = new Packet[capacity];
size = 0;
}
void push(const Packet& packet) {
if (size >= capacity) {
std::cerr << "Queue is full" << std::endl;
return;
}
packets[size++] = packet;
std::push_heap(packets, packets + size, PacketComparator());
}
Packet pop() {
if (size <= 0) {
std::cerr << "Queue is empty" << std::endl;
return Packet();
}
std::pop_heap(packets, packets + size, PacketComparator());
return packets[--size];
}
~PriorityQueue() {
delete[] packets;
}
};
int main() {
int maxCapacity;
std::cin >> maxCapacity;
//awasthi
PriorityQueue priorityQueue(maxCapacity);
int numPackets;
std::cin >> numPackets;
std::cin.ignore(); // Ignore the newline character after reading numPackets
for (int i = 0; i < numPackets; i++) {
Packet packet;
std::cin >> packet.priority;
std::cin.ignore(); // Ignore the newline character
std::getline(std::cin, packet.data);
priorityQueue.push(packet);
}
std::cout << "Processing packets based on priority:" << std::endl;
while (priorityQueue.size > 0) {
Packet packet = priorityQueue.pop();
std::cout << "Processing packet with priority " << packet.priority << " and data: " << packet.data << std::endl;
}
return 0;
}
3 849
Naveena was tasked with developing a program to process network packets based on their priority levels. Each packet arrives with a priority value, and the program needs to handle the packets in the order of their priority, processing higher-priority packets first.
Note: This kind of question will be helpful in clearing Deloitte recruitment.
Input format :
The first line of input consists of an integer, representing the maximum capacity of the priority queue.
The second line consists of an integer, representing the number of packets to be processed.
For each packet, the program should prompt for the priority value and the data associated with the packet.
A lower priority value indicates a higher priority packet.
3 849
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct Queue {
float elements[MAX_SIZE];
int front;
int rear;
};
void enqueue(struct Queue* queue, float element) {
if (queue->rear == MAX_SIZE - 1) {
printf("Queue is full\n");
return;
}
queue->rear++;
queue->elements[queue->rear] = element;
}
void dequeue(struct Queue* queue) {
if (queue->front > queue->rear) {
printf("Queue is empty\n");
return;
}
int minIndex = queue->front;
for (int i = queue->front + 1; i <= queue->rear; i++) {
if (queue->elements[i] < queue->elements[minIndex]) {
minIndex = i;
}
}
for (int i = minIndex; i < queue->rear; i++) {
queue->elements[i] = queue->elements[i + 1];
}
queue->rear--;
}
void display(struct Queue* queue) {
if (queue->front > queue->rear) {
printf("Queue is empty\n");
return;
}
for (int i = queue->front; i <= queue->rear; i++) {
printf("%.1f ", queue->elements[i]);
}
printf("\n");
}
int main() {
struct Queue queue;
queue.front = 0;
queue.rear = -1;
int n;
scanf("%d", &n);
if (n == 0) {
printf("Queue is empty\n");
return 0;
}
for (int i = 0; i < n; i++) {
float element;
scanf("%f", &element);
enqueue(&queue, element);
}
//awasthi
dequeue(&queue);
display(&queue);
return 0;
}
3 849
Write a program to implement a specialized queue data structure capable of deleting the smallest element within its contents. The program should also handle standard enqueue and dequeue operations.
Input format :
The first line of input consists of an integer N, representing the number of elements in the queue.
The following N lines consist of N floating-point numbers, separated by space, representing the elements of the queue.
3 849
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
// Structure for each element in the priority queue
typedef struct Element {
int value;
int priority;
} Element;
// Structure for the priority queue
typedef struct PriorityQueue {
Element elements[MAX_SIZE];
int size;
} PriorityQueue;
// Initialize the priority queue
void initializePriorityQueue(PriorityQueue* pq) {
pq->size = 0;
}
// Check if the priority queue is empty
bool isEmpty(PriorityQueue* pq) {
return pq->size == 0;
}
// Check if the priority queue is full
bool isFull(PriorityQueue* pq) {
return pq->size == MAX_SIZE;
}
// Enqueue an element into the priority queue
void enqueue(PriorityQueue* pq, int value, int priority) {
if (isFull(pq)) {
printf("Priority Queue is full. Cannot enqueue.\n");
return;
}
Element newElement;
newElement.value = value;
newElement.priority = priority;
int i = pq->size - 1;
while (i >= 0 && pq->elements[i].priority > priority) {
pq->elements[i + 1] = pq->elements[i];
i--;
}
pq->elements[i + 1] = newElement;
pq->size++;
}
// Dequeue the element with the highest priority from the priority queue
void dequeue(PriorityQueue* pq) {
if (isEmpty(pq)) {
printf("Priority Queue is empty. Cannot dequeue.\n");
return;
}
for (int i = 0; i < pq->size - 1; i++) {
pq->elements[i] = pq->elements[i + 1];
}
pq->size--;
}
// Print the elements of the priority queue
void printPriorityQueue(PriorityQueue* pq) {
for (int i = 0; i < pq->size; i++) {
printf("%d ", pq->elements[i].value);
}
printf("\n");
}
int main() {
int N;
scanf("%d", &N);
PriorityQueue pq;
initializePriorityQueue(&pq);
for (int i = 0; i < N; i++) {
int value, priority;
scanf("%d %d", &value, &priority);
enqueue(&pq, value, priority);
}
printf("Priority Queue: ");
printPriorityQueue(&pq);
//awasthi
dequeue(&pq);
printf("Priority Queue: ");
printPriorityQueue(&pq);
return 0;
}
3 849
Write a program to implement a priority queue using an array-based approach.
The priority queue should support the following operations:
Enqueue: Insert an element into the priority queue with a specified priority.
Dequeue: Remove the element with the highest priority from the priority queue.
isEmpty: Check if the priority queue is empty.
isFull: Check if the priority queue is full.
printPriorityQueue: Print the elements of the priority queue in the order of their priorities.
Note: This kind of question will be helpful in clearing Wipro recruitment.
Input format :
The first line of input consists of an integer N, representing the number of elements to be inserted into the priority queue.
This is followed by N lines, each containing two space-separated integers: element and priority.
The element represents the value to be inserted, and priority represents its priority. The priorities are non-negative integers.
3 849
#include <stdio.h>
#include <stdlib.h>
// Structure for each customer
typedef struct Customer {
int item;
struct Customer* next;
} Customer;
// Structure for each queue
typedef struct Queue {
struct Customer* front;
struct Customer* rear;
} Queue;
// Initialize a queue
void initializeQueue(Queue* q) {
q->front = NULL;
q->rear = NULL;
}
// Enqueue a customer into a queue
void enqueue(Queue* q, int item) {
Customer* newCustomer = (Customer*)malloc(sizeof(Customer));
newCustomer->item = item;
newCustomer->next = NULL;
if (q->front == NULL) {
q->front = newCustomer;
q->rear = newCustomer;
} else {
q->rear->next = newCustomer;
q->rear = newCustomer;
}
}
// Dequeue a customer from a queue
int dequeue(Queue* q) {
if (q->front == NULL) {
return -1; // Queue is empty
}
int item = q->front->item;
Customer* temp = q->front;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return item;
}
// Print the contents of a queue
void printQueue(Queue* q) {
Customer* current = q->front;
while (current != NULL) {
printf("%d ", current->item);
current = current->next;
}
printf("\n");
}
int main() {
int k, n;
scanf("%d %d", &k, &n);
Queue* queues = (Queue*)malloc(k * sizeof(Queue));
for (int i = 0; i < k; i++) {
initializeQueue(&queues[i]);
}
for (int i = 0; i < n; i++) {
int item, qn;
scanf("%d %d", &item, &qn);
enqueue(&queues[qn], item);
}
//awasthi
for (int i = 0; i < k; i++) {
printf("Queue %d: ", i);
printQueue(&queues[i]);
}
return 0;
}
3 849
You are tasked with implementing a program to simulate multiple queues using linked lists. The program should allow enqueue and dequeue operations on the queues and display the contents of each queue.
The program should prompt the user for the number of queues (k) and the total number of customers (n). Then, for each customer, the program should prompt for the item and the queue number the customer wants to join.
After enqueuing all the customers, the program should print the contents of each queue in order, indicating the queue number and the items in each queue.
