0% found this document useful (0 votes)
62 views7 pages

Question 1: (Linked List) : Code 1

The document contains code for three questions: 1) A function to insert a node into a linked list at a specified position. It uses two pointers to traverse the list and insert the new node after the desired position. 2) Code to sort numbers in one stack into ascending order using a second temporary stack. It pops elements from the first stack and pushes them in order onto the second stack, then empties the second stack back into the first. 3) A function to find the lowest common ancestor (LCA) of two nodes in a binary search tree. It recursively searches the tree, returning the node that is the farthest shared ancestor of the two input nodes.

Uploaded by

Mahmoud Elmahdy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

Question 1: (Linked List) : Code 1

The document contains code for three questions: 1) A function to insert a node into a linked list at a specified position. It uses two pointers to traverse the list and insert the new node after the desired position. 2) Code to sort numbers in one stack into ascending order using a second temporary stack. It pops elements from the first stack and pushes them in order onto the second stack, then empties the second stack back into the first. 3) A function to find the lowest common ancestor (LCA) of two nodes in a binary search tree. It recursively searches the tree, returning the node that is the farthest shared ancestor of the two input nodes.

Uploaded by

Mahmoud Elmahdy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Code 1 :

Question 1: (Linked List)


Write a function that takes three arguments—a pointer to a linked list of integers and two
integers, n and j—and inserts n after the jth element of the list. If j is 0, nis inserted at the head
of the list. If j is greater than the number of elements in the list, n is inserted after the last one.
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int a;
struct node *next;
};
void insert(struct node **p, int n, int j)
{
struct node *nn=(struct node*)malloc(sizeof(struct node));
nn->a=n;nn->next=NULL;
if(j==0)
{
nn->next=*p;*p=nn;
}
else
{
int i;struct node *t1=*p, *t2=*p;
for(i = 0; t1 != NULL; i++, t2 = t1, t1 = t1 -> next)
{
if(i == j) break;
}
if(t1==NULL)//if no of nodes are less than j
{
t2->next=nn;return;
}
nn->next=t1;
t2->next=nn;
}
}
void display(struct node **p)//displays list
{
for(struct node *temp=*p;temp != NULL;temp=temp->next)
{
cout<<temp->a<<" ";
}
cout<<endl;
}
int main()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->a=1;
p->next=NULL;
while(1)
{
cout<<"enter 0 to break, 1 to continue "<<endl;
int ch;cin>>ch;
if(ch==0) break;
cout<<"enter integer and position"<<endl;
int num, pos;cin>>num>>pos;
insert(&p, num, pos);display(&p);
}
return 0;
}

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

Node* lca = LCARecursive(root, x, y);

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

LCA(root, root->left->left, root->left->right);


return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy