Professional Documents
Culture Documents
1.google:
Problem solving:
REVERSE A STACK USING RECURSION
#include <stdio.h>
#include <stdlib.h>
#define bool int
struct sNode {
char data;
struct sNode* next;
};
if (new_node == NULL) {
printf("Stack overflow \n");
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
char res;
struct sNode* top;
if (*top_ref == NULL) {
printf("Stack overflow \n");
exit(0);
}
else {
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
void print(struct sNode* top)
{
printf("\n");
while (top != NULL) {
printf(" %d ", top->data);
top = top->next;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
if (isMinMaxQueueEmpty(q)) {
q->front = q->rear = newNode;
q->minElement = newNode;
} else {
q->rear->next = newNode;
newNode->prev = q->rear;
q->rear = newNode;
if (front == q->minElement) {
if (front->next != NULL) {
q->minElement = front->next;
} else {
q->minElement = NULL;
}
}
if (front->next != NULL) {
front->next->prev = NULL;
q->front = front->next;
} else {
q->front = q->rear = NULL;
}
free(front);
}
// Driver code
int main() {
struct MinMaxQueue* k = createMinMaxQueue();
int example[3] = {1, 2, 4};
printf("%d\n", getMin(k));
dequeue(k);
printf("%d\n", getMin(k));
// Free memory
while (!isMinMaxQueueEmpty(k)) {
dequeue(k);
}
free(k);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node
return 1;
/* 2. both non-empty -> compare them */
if (a != NULL && b != NULL) {
int main() {
int permutation[] = {3, 2, 1};
int n = sizeof(permutation) / sizeof(permutation[0]);
if (isValidStackPermutation(permutation, n)) {
printf("Valid stack permutation\n");
} else {
printf("Not a valid stack permutation\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Driver code
int main() {
int queue[] = {5, 1, 2, 3, 4};
int n = sizeof(queue) / sizeof(queue[0]);
if (checkSorted(n, queue)) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer
to left child and a pointer to right child */
struct Node {
int data;
struct Node* left;
struct Node* right;
};
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
struct Node* node
/\
31
*/
void mirror(struct Node* node)
{
if (node == NULL)
return;
else {
/* do the subtrees */
mirror(node->left);
mirror(node->right);
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
/* Helper function to print Inorder traversal.*/
void inOrder(struct Node* node)
{
if (node == NULL)
return;
inOrder(node->left);
printf("%d ", node->data);
inOrder(node->right);
}
/* Driver program to test mirror() */
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
/* Print inorder traversal of the input tree */
printf("Inorder traversal of the constructed"
" is \n");
inOrder(root);
return 0;
}
3.infoSyS
IMPLEMENT TWO STACXKS IN AN ARRAY
#include <stdio.h>
#include <stdlib.h>
struct TwoStacks {
int *arr;
int size;
int top1, top2;
};
struct TwoStacks *createTwoStacks(int n) {
struct TwoStacks *ts = (struct TwoStacks *)malloc(sizeof(struct TwoStacks));
ts->size = n;
ts->arr = (int *)malloc(n * sizeof(int));
ts->top1 = n / 2 + 1;
ts->top2 = n / 2;
return ts;
}
void push1(struct TwoStacks *ts, int x) {
if (ts->top1 > 0) {
ts->top1--;
ts->arr[ts->top1] = x;
} else {
printf("Stack Overflow By element: %d\n", x);
}
}
void push2(struct TwoStacks *ts, int x) {
if (ts->top2 < ts->size - 1) {
ts->top2++;
ts->arr[ts->top2] = x;
} else {
printf("Stack Overflow By element: %d\n", x);
}
}
int pop1(struct TwoStacks *ts) {
if (ts->top1 <= ts->size / 2) {
int x = ts->arr[ts->top1];
ts->top1++;
return x;
} else {
printf("Stack UnderFlow\n");
exit(1);
}
return 0;
}
int pop2(struct TwoStacks *ts) {
if (ts->top2 >= ts->size / 2 + 1) {
int x = ts->arr[ts->top2];
ts->top2--;
return x;
} else {
printf("Stack UnderFlow\n");
exit(1);
}
return 1;
}
int main() {
struct TwoStacks *ts = createTwoStacks(5);
push1(ts, 5);
push2(ts, 10);
push2(ts, 15);
push1(ts, 11);
push2(ts, 7);
printf("Popped element from stack1 is: %d\n", pop1(ts));
push2(ts, 40);
printf("Popped element from stack2 is: %d\n", pop2(ts));
free(ts->arr);
free(ts);
return 0;
}
IMPLEMENT STACK USING QUEUE
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
struct Stack* s = createStack();
push(s, 1);
push(s, 2);
push(s, 3);
return 0;
SYMMETRIC TREE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// A Binary Tree Node
typedef struct Node {
int key;
struct Node *left, *right;
} Node;
// Utility function to create new Node
Node* newNode(int key)
{
Node* temp = (Node *)malloc(sizeof(Node));
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Returns true if trees with roots as root1 and root2 are
// mirror
bool isMirror(Node* root1, Node* root2)
{
// If both trees are empty, then they are mirror images
if (root1 == NULL && root2 == NULL)
return true;
// For two trees to be mirror images, the following
// three conditions must be true
// 1.) Their root node's key must be same
// 2.) left subtree of left tree and right subtree of
// right tree have to be mirror images
// 3.) right subtree of left tree and left subtree of
// right tree have to be mirror images
if (root1 && root2 && root1->key == root2->key)
printf("Symmetric");
else
printf("Not symmetric");
return 0;
}
4.intel
CHECK FOR BALANCED BRACKETS IN AN EXPRESSION
(WELL-FORMEDNESS)
#include <stdio.h>
#include <stdlib.h>
#define bool int
struct sNode {
char data;
struct sNode* next;
};
if (stack == NULL)
return 1; // balanced
else
return 0; // not balanced
}
int main()
{
char exp[100] = "{()}[]";
if (areBracketsBalanced(exp))
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
if (new_node == NULL) {
printf("Stack overflow n");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
if (*top_ref == NULL) {
printf("Stack overflow n");
getchar();
exit(0);
}
else {
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
// Create a stack
struct Stack* stk = createStack();
// Create a queue
struct Queue* que = createQueue();
// Free memory
while (!isEmpty(stk->dq)) {
pop(stk);
}
free(stk->dq);
free(stk);
while (!isEmpty(que->dq)) {
dequeue(que);
}
free(que->dq);
free(que);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
int data;
struct node* left;
struct node* right;
};
/* Compute the "maxDepth" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int maxDepth(struct node* node)
{
if (node == NULL)
return 0;
else {
else
5.zoho
CHECK IF TWO EXPRESSIONS WITH BRACKETS ARE
SAME
#include <stdio.h>
#include <stdbool.h>
bool isValidStackPermutation(int perm[], int n) {
int stack[n];
int top = -1;
int expected = 1;
for (int i = 0; i < n; i++) {
while (top >= 0 && stack[top] == expected) {
top--;
expected++;
}
if (perm[i] == expected) {
expected++;
} else {
stack[++top] = perm[i];
}
}
while (top >= 0 && stack[top] == expected) {
top--;
expected++;
}
return (top == -1);
}
int main() {
int permutation[] = {3, 2, 1};
int n = sizeof(permutation) / sizeof(permutation[0]);
if (isValidStackPermutation(permutation, n)) {
printf("Valid stack permutation\n");
} else {
printf("Not a valid stack permutation\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Node
typedef struct node {
int data;
// Lower values indicate higher priority
int priority;
struct node* next;
} Node;
// Function to Create A New Node
Node* newNode(int d, int p)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = d;
temp->priority = p;
temp->next = NULL;
return temp;
}
// Return the value at head
int peek(Node** head)
{
return (*head)->data;
}
// Removes the element with the
// highest priority from the list
void pop(Node** head)
{
Node* temp = *head;
(*head) = (*head)->next;
free(temp);
}
// Function to push according to priority
void push(Node** head, int d, int p)
{
Node* start = (*head);
// Create new Node
Node* temp = newNode(d, p);
// Special Case: The head of list has lesser
// priority than new node. So insert new
// node before head node and change head node.
if ((*head)->priority > p) {
// Insert New Node before head
temp->next = *head;
(*head) = temp;
}
else {
start->next->priority < p) {
start = start->next;
// or at required position
temp->next = start->next;
start->next = temp;
}
}
// Function to check is list is empty
int isEmpty(Node** head)
{
return (*head) == NULL;
}
// Driver code
int main()
{
// Create a Priority Queue
// 7->4->5->6
Node* pq = newNode(4, 1);
push(&pq, 5, 2);
push(&pq, 6, 3);
push(&pq, 7, 0);
while (!isEmpty(&pq)) {
pop(&pq);
}
return 0;
}
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
typedef
struct
Node {
int data;
struct
Node *left;
struct
Node
*right;
} Node;
int main() {
Node *root =
createNode(2);
root->left =
createNode(1);
root->right = createNode(3);
if (isBST(root)) {
printf("The binary tree is a Binary Search Tree.\n");
} else {
printf("The binary tree is not a Binary Search Tree.\n");
}
return 0;
}
mcq:
1. Given a binary tree, which traversal is used to construct an
expression tree?
A. Preorder
B. Inorder
C. Postorder
D. Level order
Solution:
(B) Inorder
Explanation:
To construct an expression tree from a binary tree, you should use the inorder
traversal (option B). In an expression tree, the inorder traversal preserves the
original infix order of the expression, which is important for constructing the
correct expression tree. Other traversals like preorder and postorder do not
maintain the infix order and are not suitable for this purpose.
A. Push
B. Pop
C. Top
D. Size
Solution:(B) Pop
Explanation:
● The "Push" operation is used to insert (push) an item onto the stack.
● The "Pop" operation is used to remove and return the top item from the
stack.
● The "Top" operation is used to retrieve the top item from the stack without
removing it.
● The "Size" operation is used to determine the number of items currently in
the stack. In the context of a stack, "Pop" is specifically used for removing
items from the stack.
3)Application of Queue Data Structure is:-