3 849
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-297 روز
-12730 روز
آرشیو پست ها
3 849
#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
3 849
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"
3 849
#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;
}//awasthi
3 849
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.
3 849
#include <iostream>
using namespace std;
struct Node {
int data;
Node* link;
};
typedef Node* NODE;
NODE getnode() {
NODE x = new Node();
return x;
}
NODE insertAtEnd(NODE head, int item) {
NODE temp = getnode();
temp->data = item;
if (head == nullptr) {
head = temp;
head->link = head;
} else {
NODE cur = head;
while (cur->link != head) {
cur = cur->link;
}
cur->link = temp;
temp->link = head;
}
return head;
}
NODE deleteAtPosition(NODE head, int position) {
if (head == nullptr) {
cout << "List is empty." << endl;
return nullptr;
}
NODE cur = head;
NODE prev = nullptr;
int count = 1;
while (cur->link != head && count < position) {
prev = cur;
cur = cur->link;
count++;
}
if (count < position) {
cout << "Invalid position." << endl;
return head;
}
if (prev == nullptr) {
// Deleting the first node
NODE last = head;
while (last->link != head) {
last = last->link;
}
last->link = head->link;
head = head->link;
} else {
prev->link = cur->link;
}
delete cur;
return head;
}
void display(NODE head) {
if (head == nullptr) {
cout << "List is empty." << endl;
return;
}
cout << "Contents of the CLL:" << endl;
NODE cur = head;
do {
cout << "|" << cur->data << "| --> ";
cur = cur->link;
} while (cur != head);
cout << endl;
}
int main() {
NODE head = nullptr;
int item, position;
int n;
cin >> n;
if (n > 0) {
for (int i = 0; i < n; i++) {
cin >> item;
head = insertAtEnd(head, item);
}
display(head);
cin >> position;
if (position <= n) {
head = deleteAtPosition(head, position);
cout << "After deleting at position " << position << ":" << endl;
display(head);
} else {
cout << "Invalid position." << endl;
}
} else {
cout << "List is empty." << endl;
}
return 0;
}
3 849
You are given a circular header linked list implementation. The program should allow the insertion of elements at the end of the list and the deletion of nodes at a given position. The program should display the contents of the circular linked list after each operation.
Note: This is a sample question asked in a CTS interview.
Input format :
The first line contains an integer n representing the number of elements to be inserted into the circular linked list.
The next line contains n space-separated integers representing the elements to be inserted.
The last line contains an integer representing the position for deletion.
Output format :
The output should display the contents of the circular linked list after each operation.
If the list is empty, the output should display "List is empty."
If the position for deletion is invalid, the output should display "Invalid position."
3 849
#include <iostream>
// Node structure for circular header linked list
struct Node {
int data;
Node* next;
};
// Function to create a new node
Node* createNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
return newNode;
}
// Function to insert a node at the end of the linked list
void insertNode(Node*& head, int data) {
Node* newNode = createNode(data);
if (head == nullptr) {
head = newNode;
head->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
// Function to display the linked list
void displayList(Node* head) {
if (head == nullptr) {
return;
}
Node* temp = head;
do {
std::cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
std::cout << std::endl;
}
// Function to perform pairwise swapping of adjacent elements
void pairwiseSwap(Node*& head) {
if (head == nullptr head->next == nullptr) {
return;
}
Node* prev = head;
Node* curr = head->next;
while (true) {
int temp = curr->data;
curr->data = prev->data;
prev->data = temp;
if (curr->next == head curr->next->next == head) {
break;
}
prev = curr->next;
curr = curr->next->next;
}
}
// Function to clean up memory
void cleanup(Node*& head) {
if (head == nullptr) {
return;
}
Node* temp = head;
while (temp->next != head) {
Node* nextNode = temp->next;
delete temp;
temp = nextNode;
}
delete head;
head = nullptr;
}
int main() {
Node* head = nullptr;
// Get input for the linked list
int n;
std::cin >> n;
for (int i = 0; i < n; ++i) {
int element;
std::cin >> element;
insertNode(head, element);
}
// Display the original linked list
std::cout << "Original linked list: ";
displayList(head);
// Perform pairwise swapping of adjacent elements
pairwiseSwap(head);
// Display the modified linked list
std::cout << "Linked list after pairwise swapping: ";
displayList(head);
// Clean up memory
//cleanup(head);
//awasthi
return 0;
}
3 849
In the land of LinkedListia, there was a circular header linked list kingdom ruled by King LinkedList. The linked list was unique, with its last node pointing back to the header node.
A wise advisor named Swapia arrived in the kingdom with a special task. They had a circular header linked list filled with elements. The advisor's mission was to perform pairwise swapping of adjacent elements in the list.
Design a circular header linked list and perform a pairwise swap operation to swap adjacent elements.
Note: This is a sample question asked in Accenture recruitment.
Input format :
The first line represents the number of elements in the linked list n.
The next line represents the n elements in the linked list.
3 849
#include <iostream>
using namespace std;
// Node structure for the linked list
struct Node {
int data;
Node* next;
};
// Function to create a new node
Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert data in a sorted manner
void sortedInsert(Node** head, Node* newNode) {
if (*head == NULL || newNode->data < (*head)->data) {
newNode->next = *head;
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL && current->next->data < newNode->data) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
// Function to display the linked list
void display(Node* head) {
if (head == NULL) {
cout << "Empty linked list" << endl;
return;
}
Node* current = head;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
// Function to check if an element exists in the linked list
bool exists(Node* head, int data) {
Node* current = head;
while (current != NULL) {
if (current->data == data) {
return true;
}
current = current->next;
}
return false;
}
// Function to get the union of two linked lists
Node* makeUnion(Node* head1, Node* head2) {
Node* result = NULL;
// Traverse the first linked list and add distinct elements to the result
Node* temp1 = head1;
while (temp1 != NULL) {
if (!exists(result, temp1->data)) {
Node* newNode = createNode(temp1->data);
sortedInsert(&result, newNode);
}
temp1 = temp1->next;
}
// Traverse the second linked list and add distinct elements to the result
Node* temp2 = head2;
while (temp2 != NULL) {
if (!exists(result, temp2->data)) {
Node* newNode = createNode(temp2->data);
sortedInsert(&result, newNode);
}
temp2 = temp2->next;
}
return result;
}
int main() {
int n1, n2;
cin >> n1;
Node* head1 = NULL;
for (int i = 0; i < n1; i++) {
int data;
cin >> data;
Node* newNode = createNode(data);
sortedInsert(&head1, newNode);
}
cin >> n2;
Node* head2 = NULL;
for (int i = 0; i < n2; i++) {
int data;
cin >> data;
Node* newNode = createNode(data);
sortedInsert(&head2, newNode);
}
cout << "First Linked List: ";
display(head1);
cout << "Second Linked List: ";
display(head2);
Node* unionList = makeUnion(head1, head2);
cout << "Union Linked List: ";
display(unionList);
//awasthi
return 0;
}
3 849
You are tasked with designing a program that operates on two linked lists. Your objective is to create a new linked list that represents the union of the two given linked lists while ensuring that the elements in the union list are distinct and sorted in ascending order.
Note: This is a sample question asked in the Microsoft interview.
Input format :
The first line consists of an integer n, representing the number of nodes in the first linked list.
The second line consists of n space-separated integers, representing the nodes in the first linked list.
The third line consists of an integer m, representing the number of nodes in the second linked list.
The fourth line consists of m space-separated integers, representing the nodes in the second linked list.
Output format :
The first line of output displays the nodes of the first linked list, sorted in ascending order.
The second line displays the nodes of the second linked list, sorted in ascending order.
The third line displays the nodes of the union linked list after merging the distinct elements from the first and second linked lists.
3 849
#include <iostream>
using namespace std;
// Structure for a node in the doubly linked list
struct Node {
int data;
Node* prev;
Node* next;
Node(int data) {
this->data = data;
prev = nullptr;
next = nullptr;
}
};
// Doubly linked list for storing medical events
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
}
// Function to insert a new medical event into the linked list
void insert(int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
// Function to check if the doubly linked list is a palindrome
bool isPalindrome() {
Node* current = head;
Node* previous = tail;
while (current != nullptr && previous != nullptr) {
if (current->data != previous->data) {
return false;
}
current = current->next;
previous = previous->prev;
}
return true;
}
};
int main() {
int n;
cin >> n;
DoublyLinkedList list;
for (int i = 0; i < n; i++) {
int data;
cin >> data;
list.insert(data);
}
bool isPalindrome = list.isPalindrome();
if (isPalindrome) {
cout << "The patient's medical history is a palindrome" << endl;
} else {
cout << "The patient's medical history is not a palindrome" << endl;
}
return 0;
}
3 849
Problem Statement:
Imagine you are a software developer working on a critical project for a medical research institute. The project involves analyzing patient data stored in a doubly linked list. One of the tasks assigned is to develop a program that can determine whether a patient's medical history, represented by a doubly linked list, is a palindrome or not.
A palindrome in the context of this project means that the sequence of medical events recorded in the linked list, when read forward or backward, remains the same. It is crucial to identify palindromes in the medical history as they may indicate recurring patterns or symptoms that require special attention.
Write a program that assists in analyzing the patient data by checking if a given doubly linked list, representing a patient's medical history, is a palindrome or not. The program should provide a reliable tool to help identify potential patterns or recurring symptoms that could aid in diagnosing and treating patients effectively.
Note: This is a sample question asked in a Capgemini interview.
Input format :
The first line contains an integer, 'n', representing the number of medical events recorded in the patient's history.
The second line contains 'n' space-separated integers, denoting the medical events in chronological order.
Output format :
If the doubly linked list is a palindrome, output "The patient's medical history is a palindrome".
If the doubly linked list is not a palindrome, output "The patient's medical history is not a palindrome".
Sample test cases :
Input 1 :
5
1 2 3 2 1
Output 1 :
The patient's medical history is a palindrome
Input 2 :
5
1 2 3 4 5
Output 2 :
The patient's medical history is not a palindrome
3 849
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
struct Node {
string data;
Node* next;
Node* prev;
};
Node* insertAtEnd(Node* head, const string& value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
newNode->prev = nullptr;
return newNode;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
return head;
}
int countUniqueInteractions(Node* head) {
unordered_set<string> uniqueInteractions;
int count = 0;
Node* current = head;
while (current != nullptr) {
if (uniqueInteractions.find(current->data) == uniqueInteractions.end()) {
uniqueInteractions.insert(current->data);
count++;
}
current = current->next;
}
return count;
}
int main() {
int n;
cin >> n;
cin.ignore();
Node* head = nullptr;
for (int i = 0; i < n; i++) {
string sessionID;
getline(cin, sessionID);
head = insertAtEnd(head, sessionID);
}
int uniqueCount = countUniqueInteractions(head);
cout << "Number of unique user interactions: " << uniqueCount << endl;
//awasthi
return 0;
}
3 849
You are developing a web analytics tool that tracks user engagement on a website. The tool requires functionality to determine the number of unique user interactions recorded in a log file. Each user interaction is represented by a unique session ID. Implement a program that allows the user to input a log file containing session IDs representing user interactions. The program should create a doubly linked list using the session IDs from the log file and calculate the total count of unique user interactions in the list.
The program should read the log file, which consists of one session ID per line. It will then create a doubly linked list using the session IDs as the data. Next, the program will traverse the list and count the number of unique session IDs present. Finally, it will display the count as the output.
The web analytics tool will provide valuable insights to website administrators by accurately determining the number of unique user interactions, enabling them to analyze user engagement and make data-driven decisions to enhance the user experience.
Note: This is a sample question asked in an Amazon interview.
Input format :
The input begins with an integer N, representing the number of session IDs in the log file.
This is followed by N lines of strings, each containing a session ID.
