Question 1: (Linked List) : Code 1
Question 1: (Linked List) : Code 1
Question 1: (Linked List) : Code 1
-----------------------------------------------------------------------------------------------------------------------------
Code 2:
A stack, S1, contains some numbers in arbitrary order. Using another stack, S2, for temporary
storage, show how to sort the numbers in S1 such that the smallest is at the top of S1 and the
largest is at the bottom.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
cout<<"Enter stack size :";
cin>>n
do
{
cout<<"\n Enter the Choice:";
cin>>choice;
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}case 4:
{
cout<<"\n\t EXIT "<<endl;
break;
}
default:
{
cout<<"\n\t Please select from(1-2-3-4)";
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
cout<<"\n\tSTACK is over flow";
}
else
{
cout<<" Enter a value to be pushed:";
cin>>x
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
cout<<"\n\t Stack is under flow\n";
}
else
{
cout<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"\n The elements in STACK \n";
for(i=top; i>=0; i--)
cout<<"\n%d",stack[i];
cout<<"\n Press Next Choice";
}
else
{
cout<<"\n The STACK is empty";
}case 4:
{
cout<<"\n\t EXIT ";
break;
}
default:
{
cout<<"\n\t Please Enter a Valid Choice(1-2-3-4)";
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
cout<<"\n\tSTACK is over flow"<<endl;
}
else
{
cout<<" Enter a value to be pushed:";
cin>>x;
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
cout<<"\n\t Stack is under flow";
}
else
{
cout<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"\n The elements in STACK \n";
for(i=top; i>=0; i--)
cin>>stack[i];
cout<<"\n Press Next Choice :";
}
else
{
cout<<"\n The STACK is empty"<<endl;
}}
Code 3 :
The lowest common ancestor (LCA) of two nodes x and y in the BST is the lowest (i.e.,
deepest) node that has both x and y as descendants, where each node can be a descendant of
itself (so if x is reachable from w, w is the LCA). In other words, the LCA of x and y is the
shared ancestor of x and y that is located farthest from the root.
Given a BST and two nodes x and y in it, write a function that returns the lowest common
ancestor (LCA) of x and y
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left = nullptr, *right = nullptr;
Node() {}
Node(int data): data(data) {}
};
void inorder(Node* root)
{
if (root == nullptr) {
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
Node* insert(Node* root, int key)
{
// if the root is null, create a new node and return it
if (root == nullptr) {
return new Node(key);
}
if (key < root->data) {
root->left = insert(root->left, key);
}
else {
root->right = insert(root->right, key);
}
return root;
}
bool search(Node* root, Node* key)
{
while (root)
{
if (key->data < root->data) {
root = root->left;
}
else if (key->data > root->data) {
root = root->right;
}
else if (key == root) {
return true;
}
else {
return false;
}
}
return false;
}
Node* LCARecursive(Node* root, Node* x, Node* y)
{
// base case: empty tree
if (root == nullptr) {
return nullptr;
}
if (root->data > max(x->data, y->data)) {
return LCARecursive(root->left, x, y);
}
else if (root->data < min(x->data, y->data)) {
return LCARecursive(root->right, x, y);
}
return root;
}
void LCA(Node* root, Node* x, Node* y)
{
if (root == nullptr || !search(root, x) || !search(root, y)) {
return;
}
if (lca != nullptr) {
cout << "LCA is " << lca->data;
}
else {
cout << "LCA does not exist";
}
}
int main()
{
int keys[] = { 15, 10, 20, 8, 12, 16, 25 };
Node* root = nullptr;
for (int key: keys) {
root = insert(root, key);
}