You are on page 1of 35

Data StructureS aSSignment:

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;
};

void push(struct sNode** top_ref, int new_data);


int pop(struct sNode** top_ref);
bool isEmpty(struct sNode* top);
void print(struct sNode* top);

void insertAtBottom(struct sNode** top_ref, int item)


{
if (isEmpty(*top_ref))
push(top_ref, item);
else {
int temp = pop(top_ref);
insertAtBottom(top_ref, item);
push(top_ref, temp);
}
}
void reverse(struct sNode** top_ref)
{
if (!isEmpty(*top_ref)) {
int temp = pop(top_ref);
reverse(top_ref);
insertAtBottom(top_ref, temp);
}
}
int main()
{
struct sNode* s = NULL;
push(&s, 4);
push(&s, 3);
push(&s, 2);
push(&s, 1);
printf("\n Original Stack ");
print(s);
reverse(&s);
printf("\n Reversed Stack ");
print(s);
return 0;
}

bool isEmpty(struct sNode* top)


{
return (top == NULL) ? 1 : 0;
}

void push(struct sNode** top_ref, int new_data)


{

struct sNode* new_node


= (struct sNode*)malloc(sizeof(struct sNode));

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;
}
}

DESIGN A QUEUE DATA STRUCTURE TO GET MINIMUM OR


MAXIMUM IN O(1) TIME

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Structure of a node for the doubly ended queue


struct DQueNode {
int data;
struct DQueNode* next;
struct DQueNode* prev;
};

// Structure of the queue


struct MinMaxQueue {
struct DQueNode* front;
struct DQueNode* rear;
struct DQueNode* minElement;
};

// Function to create an empty doubly ended queue


struct DQueNode* createDQueNode(int data) {
struct DQueNode* node = (struct DQueNode*)malloc(sizeof(struct
DQueNode));
node->data = data;
node->next = node->prev = NULL;
return node;
}

// Function to initialize the MinMaxQueue


struct MinMaxQueue* createMinMaxQueue() {
struct MinMaxQueue* q = (struct MinMaxQueue*)malloc(sizeof(struct
MinMaxQueue));
q->front = q->rear = q->minElement = NULL;
return q;
}

// Function to check if the doubly ended queue is empty


bool isDQueEmpty(struct DQueNode* front) {
return (front == NULL);
}
// Function to check if the MinMaxQueue is empty
bool isMinMaxQueueEmpty(struct MinMaxQueue* q) {
return isDQueEmpty(q->front);
}

// Function to push an element into the MinMaxQueue


void enqueue(struct MinMaxQueue* q, int element) {
struct DQueNode* newNode = createDQueNode(element);

if (isMinMaxQueueEmpty(q)) {
q->front = q->rear = newNode;
q->minElement = newNode;
} else {
q->rear->next = newNode;
newNode->prev = q->rear;
q->rear = newNode;

if (element < q->minElement->data) {


q->minElement = newNode;
}
}
}

// Function to remove the front element from the MinMaxQueue


void dequeue(struct MinMaxQueue* q) {
if (isMinMaxQueueEmpty(q)) {
return; // Queue is empty
}

struct DQueNode* front = q->front;

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);
}

// Function to get the minimum element from the MinMaxQueue


int getMin(struct MinMaxQueue* q) {
if (!isMinMaxQueueEmpty(q)) {
return q->minElement->data;
}
return -1; // Queue is empty
}

// Driver code
int main() {
struct MinMaxQueue* k = createMinMaxQueue();
int example[3] = {1, 2, 4};

// Loop to enqueue elements


for (int i = 0; i < 3; i++) {
enqueue(k, example[i]);
}

printf("%d\n", getMin(k));
dequeue(k);
printf("%d\n", getMin(k));

// Free memory
while (!isMinMaxQueueEmpty(k)) {
dequeue(k);
}
free(k);

return 0;
}

PROGRAM TO IDENTIFY IF THE TWO TREES ARE IDENTICAL


OR NOT

#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.microSoft:
CHECK IF THE PERMUTATION IS GIVEN VALID PERMUTATION OR
NOT
#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;
}

CHECK IF A QUEUE CAN BE SORTED INTO ANOTHER QUEUE


USING STACK

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Structure for a stack


struct Stack {
int* data;
int top;
int capacity;
};

// Function to create an empty stack


struct Stack* createStack(int capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->data = (int*)malloc(sizeof(int) * capacity);
return stack;
}

// Function to check if the stack is empty


bool isEmpty(struct Stack* stack) {
return stack->top == -1;
}

// Function to check if the stack is full


bool isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}

// Function to push an element onto the stack


void push(struct Stack* stack, int element) {
if (!isFull(stack)) {
stack->data[++stack->top] = element;
}
}

// Function to pop the top element from the stack


int pop(struct Stack* stack) {
if (!isEmpty(stack)) {
return stack->data[stack->top--];
}
return -1; // Stack is empty
}

// Function to check if the given queue can be sorted using a stack


bool checkSorted(int n, int* queue) {
struct Stack* stack = createStack(n);
int expected = 1;

for (int i = 0; i < n; i++) {


if (queue[i] == expected) {
expected++;
} else {
if (!isEmpty(stack) && stack->data[stack->top] < queue[i]) {
return false;
} else {
push(stack, queue[i]);
}
}

while (!isEmpty(stack) && stack->data[stack->top] == expected) {


pop(stack);
expected++;
}
}

return expected - 1 == n && isEmpty(stack);


}

// 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;
}

CONVERT A BINARY TREE INTO ITS MIRROR TREE

#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.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>

// Structure to represent a node in the queue


struct Node {
int data;
struct Node* next;
};

// Structure for a queue


struct Queue {
struct Node* front;
struct Node* rear;
};

// Function to create an empty queue


struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
// Function to enqueue an element into the queue
void enqueue(struct Queue* q, int x) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}

// Function to dequeue an element from the queue


int dequeue(struct Queue* q) {
if (q->front == NULL) {
return -1; // Queue is empty
}
int data = q->front->data;
struct Node* temp = q->front;
q->front = q->front->next;
free(temp);
return data;
}

// Structure for the stack using two queues


struct Stack {
struct Queue* q1;
struct Queue* q2;
};

// Function to create an empty stack


struct Stack* createStack() {
struct Stack* s = (struct Stack*)malloc(sizeof(struct Stack));
s->q1 = createQueue();
s->q2 = createQueue();
return s;
}

// Function to push an element onto the stack


void push(struct Stack* s, int x) {
// Enqueue x to q2
enqueue(s->q2, x);

// Dequeue all elements from q1 and enqueue them to q2


while (s->q1->front != NULL) {
enqueue(s->q2, dequeue(s->q1));
}

// Swap q1 and q2 to make q2 as the primary queue


struct Queue* temp = s->q1;
s->q1 = s->q2;
s->q2 = temp;
}

// Function to pop the top element from the stack


void pop(struct Stack* s) {
if (s->q1->front == NULL) {
return; // Stack is empty
}
dequeue(s->q1);
}

// Function to get the top element of the stack


int top(struct Stack* s) {
if (s->q1->front == NULL) {
return -1; // Stack is empty
}
return s->q1->front->data;
}

// Function to get the size of the stack


int size(struct Stack* s) {
int count = 0;
struct Node* current = s->q1->front;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}

// Driver code
int main() {
struct Stack* s = createStack();
push(s, 1);
push(s, 2);
push(s, 3);

printf("Current size: %d\n", size(s));


printf("%d\n", top(s));
pop(s);
printf("%d\n", top(s));
pop(s);
printf("%d\n", top(s));

printf("Current size: %d\n", size(s));

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.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;
};

void push(struct sNode** top_ref, int new_data);


int pop(struct sNode** top_ref);

bool isMatchingPair(char character1, char character2)


{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}

bool areBracketsBalanced(char exp[])


{
int i = 0;

struct sNode* stack = NULL;


while (exp[i]) {

if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')


push(&stack, exp[i]);

if (exp[i] == '}' || exp[i] == ')'


|| exp[i] == ']') {
if (stack == NULL)
return 0;

else if (!isMatchingPair(pop(&stack), exp[i]))


return 0;
}
i++;
}

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;
}

void push(struct sNode** top_ref, int new_data)


{

struct sNode* new_node


= (struct sNode*)malloc(sizeof(struct sNode));

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;
}

int pop(struct sNode** top_ref)


{
char res;
struct sNode* top;

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;
}
}

IMPLEMENT QUEUE USING DEQUEUE

#include <stdio.h>
#include <stdlib.h>

// Structure for a node of deque


struct DQueNode {
int value;
struct DQueNode* next;
struct DQueNode* prev;
};

// Structure for a deque


struct Deque {
struct DQueNode* head;
struct DQueNode* tail;
};

// Function to create an empty deque


struct Deque* createDeque() {
struct Deque* dq = (struct Deque*)malloc(sizeof(struct Deque));
dq->head = dq->tail = NULL;
return dq;
}

// Function to check if the deque is empty


int isEmpty(struct Deque* dq) {
return (dq->head == NULL);
}

// Function to count the number of nodes in the deque


int size(struct Deque* dq) {
if (!isEmpty(dq)) {
struct DQueNode* temp = dq->head;
int len = 0;
while (temp != NULL) {
len++;
temp = temp->next;
}
return len;
}
return 0;
}

// Function to insert at the first position of the deque


void insertFirst(struct Deque* dq, int element) {
struct DQueNode* temp = (struct DQueNode*)malloc(sizeof(struct
DQueNode));
temp->value = element;
if (dq->head == NULL) {
dq->head = dq->tail = temp;
temp->next = temp->prev = NULL;
} else {
dq->head->prev = temp;
temp->next = dq->head;
temp->prev = NULL;
dq->head = temp;
}
}

// Function to insert at the last position of the deque


void insertLast(struct Deque* dq, int element) {
struct DQueNode* temp = (struct DQueNode*)malloc(sizeof(struct
DQueNode));
temp->value = element;
if (dq->head == NULL) {
dq->head = dq->tail = temp;
temp->next = temp->prev = NULL;
} else {
dq->tail->next = temp;
temp->next = NULL;
temp->prev = dq->tail;
dq->tail = temp;
}
}

// Function to remove the first element from the deque


void removeFirst(struct Deque* dq) {
if (!isEmpty(dq)) {
struct DQueNode* temp = dq->head;
dq->head = dq->head->next;
if (dq->head) dq->head->prev = NULL;
free(temp);
if (!dq->head) dq->tail = NULL;
}
}

// Function to remove the last element from the deque


void removeLast(struct Deque* dq) {
if (!isEmpty(dq)) {
struct DQueNode* temp = dq->tail;
dq->tail = dq->tail->prev;
if (dq->tail) dq->tail->next = NULL;
free(temp);
if (!dq->tail) dq->head = NULL;
}
}

// Function to display the elements in the deque


void display(struct Deque* dq) {
if (!isEmpty(dq)) {
struct DQueNode* temp = dq->head;
while (temp != NULL) {
printf("%d ", temp->value);
temp = temp->next;
}
printf("\n");
}
}

// Structure for a stack


struct Stack {
struct Deque* dq;
};

// Function to create an empty stack


struct Stack* createStack() {
struct Stack* s = (struct Stack*)malloc(sizeof(struct Stack));
s->dq = createDeque();
return s;
}

// Function to push an element onto the stack


void push(struct Stack* s, int element) {
insertLast(s->dq, element);
}

// Function to pop the top element from the stack


void pop(struct Stack* s) {
removeLast(s->dq);
}
// Structure for a queue
struct Queue {
struct Deque* dq;
};

// Function to create an empty queue


struct Queue* createQueue() {
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->dq = createDeque();
return q;
}

// Function to enqueue an element into the queue


void enqueue(struct Queue* q, int element) {
insertLast(q->dq, element);
}

// Function to dequeue an element from the queue


void dequeue(struct Queue* q) {
removeFirst(q->dq);
}

// Driver code
int main() {
// Create a stack
struct Stack* stk = createStack();

// Push 7 and 8 onto the stack


push(stk, 7);
push(stk, 8);
printf("Stack: ");
display(stk->dq);

// Pop an element from the stack


pop(stk);
printf("Stack: ");
display(stk->dq);

// Create a queue
struct Queue* que = createQueue();

// Enqueue 12 and 13 in the queue


enqueue(que, 12);
enqueue(que, 13);
printf("Queue: ");
display(que->dq);
// Dequeue an element from the queue
dequeue(que);
printf("Queue: ");
display(que->dq);

printf("Size of Stack is %d\n", size(stk->dq));


printf("Size of Queue is %d\n", size(que->dq));

// 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;
}

FIND THE MAXIMUM HEIGHT AND DEPTH OF BINARY TREE

#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 {

/* compute the depth of each subtree */

int lDepth = maxDepth(node->left);

int rDepth = maxDepth(node->right);

/* use the larger one */

if (lDepth > rDepth)

return (lDepth + 1);

else

return (rDepth + 1);


}
}
/* 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);
}
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);
printf("Height of tree is %d", maxDepth(root));
getchar();
return 0;
}

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;
}

PRIORITY QUEUE USING LINKED LIST

#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 {

// Traverse the list and find a

// position to insert new node

while (start->next != NULL &&

start->next->priority < p) {

start = start->next;

// Either at the ends of the list

// 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)) {

printf("%d ", peek(&pq));

pop(&pq);
}
return 0;
}

CHECK IF A TREE IS A BINARY SEARCH TREE

#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>

typedef
struct
Node {
int data;
struct
Node *left;
struct
Node
*right;
} Node;

Node *createNode(int value) {


Node *newNode = (Node
*)malloc(sizeof(Node)); newNode->data =
value;
newNode->left =
NULL;
newNode->right
= NULL; return
newNode;
}

bool isBSTUtil(Node *root, int min,


int max) { if (root == NULL) {
return true;
}

if (root->data < min ||


root->data > max) { return
false;
}

return isBSTUtil(root->left, min, root->data - 1) && isBSTUtil(root->right, root->data + 1,


max);
}

bool isBST(Node *root) {


return isBSTUtil(root, INT_MIN, INT_MAX);
}

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.

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.
3)Application of Queue Data Structure is:-

A. The resource is shared among multiple consumers.


B. Data is transferred asynchronously between two processes.
C. Load Balancing
D. All of the above
Solution: D. All of the above
Explanation:
All a,b, and c options are the application of Queue.

You might also like