Assn Week-2 Ds
Assn Week-2 Ds
Assn Week-2 Ds
V.SHREERAM
22N251
1) MICROSOFT
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
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Given two trees, return true if they are
structurally identical */
int identicalTrees(struct node* a, struct node* b)
{
/*1. both empty */
if (a == NULL && b == NULL)
return 1;
/* 2. both non-empty -> compare them */
if (a != NULL && b != NULL) {
return (a->data == b->data
&& identicalTrees(a->left, b->left)
&& identicalTrees(a->right, b->right));
}
/* 3. one empty, one not -> false */
return 0;
}
/* Driver code*/
int main()
{
struct node* root1 = newNode(1);
struct node* root2 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(3);
root1->left->left = newNode(4);
root1->left->right = newNode(5);
root2->left = newNode(2);
root2->right = newNode(3);
root2->left->left = newNode(4);
root2->left->right = newNode(5);
// Function call
if (identicalTrees(root1, root2))
printf("Both tree are identical.");
else
printf("Trees are not identical.");
getchar();
return 0;
}
2) MORGAN STANLEY
#include <stdbool.h>
int stack[n];
int expected = 1;
top--;
expected++;
if (perm[i] == expected) {
expected++;
} else {
stack[++top] = perm[i];
top--;
expected++;
int main() {
int permutation[] = {3, 2, 1};
} else {
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
= (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* Change a tree so that the roles of the left and
right pointers are swapped at every node.
So the tree...
4
/\
25
/\
13
is changed to...
4
/\
52
/\
31
*/
void mirror(struct Node* node)
{
if (node == NULL)
return;
else {
struct Node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
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"
" tree is \n");
inOrder(root);
/* Convert tree to its mirror */
mirror(root);
/* Print inorder traversal of the mirror tree */
printf("\nInorder traversal of the mirror tree"
" is \n");
inOrder(root);
return 0;
}
3) CISCO
#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)
return isMirror(root1->left, root2->right)
&& isMirror(root1->right, root2->left);
// if none of above conditions is true then root1
// and root2 are not mirror images
return false;
}
// Returns true if a tree is symmetric i.e. mirror image of
// itself
bool isSymmetric(Node* root)
{
// Check if tree is mirror of itself
return isMirror(root, root);
}
// Driver code
int main()
{
// Let us construct the Tree shown in the above figure
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(4);
root->right->right = newNode(3);
if (isSymmetric(root))
printf("Symmetric");
else
printf("Not symmetric");
return 0;
}
4) ORACLE
#include <stdio.h>
#include <stdlib.h>
struct sNode {
char data;
};
return 1;
return 1;
return 1;
else
return 0;
int i = 0;
while (exp[i]) {
push(&stack, exp[i]);
|| exp[i] == ']') {
if (stack == NULL)
return 0;
return 0;
i++;
if (stack == NULL)
return 1; // balanced
else
int main()
if (areBracketsBalanced(exp))
printf("Balanced \n");
else
return 0;
if (new_node == NULL) {
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
char res;
if (*top_ref == NULL) {
getchar();
exit(0);
else {
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
// 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>
struct node {
int data;
};
if (node == NULL)
return 0;
else {
else
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
getchar();
return 0;
5.WALMART
#include <stdbool.h>
int stack[n];
int expected = 1;
top--;
expected++;
if (perm[i] == expected) {
expected++;
} else {
stack[++top] = perm[i];
top--;
expected++;
}
int main() {
if (isValidStackPermutation(permutation, n)) {
} else {
return 0;
#include <stdio.h>
#include <stdlib.h>
// Node
int data;
int priority;
} Node;
temp->data = d;
temp->priority = p;
temp->next = NULL;
return temp;
return (*head)->data;
(*head) = (*head)->next;
free(temp);
if ((*head)->priority > p) {
temp->next = *head;
(*head) = temp;
else {
// Traverse the list and find a
start->next->priority < p) {
start = start->next;
// or at required position
temp->next = start->next;
start->next = temp;
// Driver code
int main()
// 7->4->5->6
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>
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;
}
GATE QUESTIONS
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.
2.Which of the following operations on a stack is used to remove an item from the
stack?
(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.