0% found this document useful (0 votes)
11 views

code

Uploaded by

utshob2137
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

code

Uploaded by

utshob2137
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

} else current->data = key;

CODES
return;
} }
LINKED LISTS void remove_head(Node** head_ref) { Node* tail = head;
if (*head_ref == NULL) { while (tail->next != NULL) {
Single
STACK struct Node {
cout << "Underflow\n"; tail = tail->next;
#include <stack> return; }
int data;
stack<int> stack; } cout << "Head=" << head->data << ",
Node* next;
stack.push(num); Node* temp = *head_ref; Tail=" << tail->data << ", ";
stack.pop() };
*head_ref = (*head_ref)->next; Node* current = head;
stack.empty() void insert_front(Node** head_ref, int
delete temp; while (current != NULL) {
stack.top() key) {
} cout << current->data;
stackname1.swap(stackname2) Node* new_node = new Node();
void remove_element(Node** head_ref, int if (current->next != NULL) {
mystack.emplace(1); new_node->data = key;
key) { cout << " ";
QUEUE new_node->next = *head_ref;
Node* current = *head_ref; }
*head_ref = new_node;
#include <queue> Node* prev = NULL; current = current->next;
queue<int> gquiz; }
}
gquiz.push(10); void insert_back(Node** head_ref, int key)
gquiz.size(); { if (current != NULL && current->data cout << "\n";
gquiz.front(); == key) { }
Node* new_node = new Node();
gquiz.back(); *head_ref = current->next;
new_node->data = key;
gquiz.pop(); delete current;
new_node->next = NULL; int main() {
g.front();
return; Node* head = NULL;
g.empty(); if (*head_ref == NULL) {
queue1.swap(queue2); } int choice, key, v;
*head_ref = new_node;
while (current != NULL && current- cout << "Press 1 to insert at front\n";
PRORITY QUEUE return;
>data != key) {
cout << "Press 2 to insert at back\n";
}
priority_queue<int> pq; prev = current;
Node* current = *head_ref; cout <<"Press 3 to insert after a node\n";
current = current->next;
DEQUE while (current->next != NULL) {
}
cout << "Press 4 to update a node\n";

current = current->next; cout <<"Press 5 to remove thefirstnode\n";


#include <deque> if (current == NULL) {
deque<int>::iterator it; } cout << "Press 6 to remove a node\n";
cout << "Value Not found\n";
for (it = g.begin(); it != g.end(); ++it) cout <<"Press 7 to remove the lastnode\n";
current->next = new_node;
cout << '\t' << *it; return;
} cout << "Press 8 to exit\n";
deque<int> gquiz; }
void insert_after_node(Node* head, int while (1) {
gquiz.push_back(10); prev->next = current->next;
gquiz.push_front(20); key, int v) { cin >> choice;
delete current;
gquiz.size(); Node* current = head; switch (choice) {
gquiz.max_size() ; }
while (current != NULL && current- case 1:
cout << "\ngquiz.at(2) : " << gquiz.at(2); void remove_end(Node** head_ref) {
>data != v) { cin >> key;
gquiz.front(); if (*head_ref == NULL) {
d.empty(); current = current->next; insert_front(&head, key);
} cout << "Underflow\n";
d.resize(5); break;
d.insert(d.begin() + 1, 10); if (current == NULL) { return;
case 2:
d.erase(d.begin() + 1); }
cout << "Value Not found\n"; cin >> key;
d.clear(); if ((*head_ref)->next == NULL) {
gquiz.back(); } else { insert_back(&head, key);
Node* new_node = new Node(); delete *head_ref;
gquiz.pop_front(); break;
gquiz.pop_back(); new_node->data = key; *head_ref = NULL;
case 3:
d1.swap(d2); return;
new_node->next = current->next; cin >> key;
}
HEAP current->next = new_node; cin >> v;
} Node* current = *head_ref;
vector<int> v = {3, 1, 4, 1, 5, 9} insert_after_node(head, key,
} while (current->next->next != NULL) { v);
make_heap(v.begin(), v.end());
std::pop_heap(v.begin(), v.end()); // void update_node(Node* head, int key, int current = current->next; break;
Moves the max element to the end v) { } case 4:
int max_element = v.back(); // Access the Node* current = head; delete current->next; cin >> key;
max element
while (current != NULL && current- current->next = NULL; cin >> v;
v.pop_back(); // Remove the max
>data != v) { }
element from the vector update_node(head, key, v);
sort_heap(v.begin(), v.end()); current = current->next;
break;
bool result = is_heap(v.begin(), v.end()); } void print_list(Node* head) { case 5:
if (current == NULL) { cout << "Value if (head == NULL) { remove_head(&head);
Not found" << endl;
cout << "Head=Null, Tail=Null, Empty\n"; break;
case 6: Node *Temp=Head; }else Temp=Temp->Next; search = true;
cin >> key; while (Temp!=nullptr){ } if (Temp->Next-
remove_element(&head, key); arr[counter]=Temp->data; if (!search) cout << "Value Not Found" >Next==nullptr) Tail=Temp;
break; Temp=Temp->Next; << endl; Node *Temper=Temp->Next;
case 7: counter++; } Temp->Next=Temp->Next-
void Remove_head(){ >Next;
remove_end(&head); }
Node *Temp = Head; Temp->Next->Prev=Temp;
break; cout<<"Main order: ";
if (size<=0) { size--;
case 8: for (int i=0;i<=counter-1;i++){
cout<<"Underflow"<<endl; free(Temper);
return 0; cout<<arr[i]<<" ";
free(Temp); break;
} }
return; }
print_list(head); cout<<endl;
} else if (size==1) { else Temp=Temp->Next;
} cout<<"Reverse Order: ";
Tail=Head=nullptr; }
return 0; for (int i=counter-1;i>=0;i--){
free(Temp); if (!search) cout << "Value Not
} cout<<arr[i]<<" ";
Found" << endl;
size--;
Doubly }
}
} return;
#include <iostream> }
cout << endl; }
#include <algorithm> int main(){
} Head = Head->Next;
using namespace std; int x, key, val;
void Insert_back(int key){ Head->Prev=nullptr;
struct Node{ while (1) {
Node *new_node = create_node(key); size--;
int data; cin >> x;
if (Tail == nullptr) { delete Temp;
Node *Next; if (x == 8)
Head = new_node; }
Node*Prev; break;
Tail = new_node;
}; switch (x){
} else{ void Remove_end(){
Node *Head; case 1:
Tail->Next = new_node; if (size<= 0){
Node *Tail; cin >> key;
new_node->Prev=Tail; cout<<"Underflow"<<endl;
int size = 0; Insert_front(key);
Tail = new_node; return;
Node *create_node(int key){ print();
} } else if (size==1){
Node *new_node = new Node(); break;
} Tail=Head=nullptr;
new_node->data = key; case 2:
void Insert_after_node(int key, int v){ size--;
new_node->Prev = nullptr; cin >> key;
bool search=false; return;
new_node->Next = nullptr; Insert_back(key);
Node *new_node=create_node(key); }
size++; print();
Node *Temp=Head; Node *Temp=Tail;
return new_node; break;
while (Temp!=nullptr){ Tail->Prev->Next=Tail->Next;
} case 3:
if (Temp->data==v) { Tail=Tail->Prev;
void Insert_front(int key) cin >> key;
search=true; size--;
{ cin>> val;
new_node->Next=Temp->Next; delete Temp;
Node *new_node=create_node(key); Insert_after_node(key, val);
new_node->Prev=Temp; }
if (Head==nullptr) { print();
Temp->Next=new_node;
Head=new_node; break;
if (Temp==Tail) Tail=new_node; void Remove_element(int key){
Tail=new_node; case 4:
break; bool search=false;
} else { cin>> key;
} else Temp=Temp->Next; if (size==0){
new_node->Next = Head; cin>> val;
} cout<<"Underflow"<<endl;
Head->Prev=new_node; Update_node(key, val);
if (!search) cout << "Value Not Found" return;
Head = new_node; print();
<< endl; }
} break;
} Node *Temp=Head;
} case 5:
void Update_node(int key, int val) if (Temp->data==key) {
void print(){ Remove_head();
{ if (Temp==Tail) Tail=nullptr;
int arr[1000]; print();
bool search=false; Head = Head->Next;
int counter=0; break;
Node *Temp=Head; Head->Prev=nullptr;
if (size==0) cout << "Head=Null, case 6:
while (Temp!=nullptr) { size--;
Tail=Null, Empty"; cin >> key;
if (Temp->data==val){ delete Temp;
else{ Remove_element(key);
search=true; }else{
cout << "Head="<<Head->data<<", print();
Tail="<<Tail->data<<endl; Temp->data=key; while (Temp->Next!=nullptr) {
break;
break; if (Temp->Next->data==key) {
case 7: std::cout << "After pop_front and } predecessor = temp;
Remove_end(); pop_back: "; }
print(); for (int x : myList) { Node* insert(Node* root, int key) { if (root->right != nullptr) {
break; std::cout << x << " "; Node* new_node = newNode(key); Node* temp = root->right;
case 8: } if (root == nullptr) return new_node; while (temp->left) {
return 0; std::cout << std::endl; Node* current = root; temp = temp->left;
} // 4. Erase an element at a specific Node* parent = nullptr; }
position
} while (current != nullptr) { successor = temp;
it = myList.begin();
} parent = current; }
std::advance(it, 1); // Move iterator
if (key < current->key) current = return;
to the second element
current->left; }
Stl: myList.erase(it); // Erase the second
else if (key > current->key)
element if (key < root->key) {
#include <list> current = current->right;
std::cout << "After erasing second successor = root;
list<int> myList; else return root;
element: "; findBeforeAfter(root->left, key,
myList.push_back(10); } predecessor, successor);
for (int x : myList) {
myList.push_front(5); if (key < parent->key) parent->left = } else {
std::cout << x << " ";
myList.pop_front(); new_node;
} std::cout << std::endl; predecessor = root;
myList.pop_back(); else parent->right = new_node;
// 5. Reverse the list findBeforeAfter(root->right, key,
myList.insert(position, 15); predecessor, successor);
myList.reverse();
myList.erase(pos); return root; }
std::cout << "After reversing: ";
} }
for (int x : myList) {
#include <iostream> void Print_tree(Node* root) { int main() {
std::cout << x << " ";
#include <list> if (root == nullptr) return; int N, choice;
}
int main() { Print_tree(root->left); cin >> N;
std::cout << std::endl;
// 1. Creating a list and inserting cout << root->key << " ";
// 6. Sort the list
elements Print_tree(root->right); Node* root = nullptr;
myList.sort();
std::list<int> myList; } for (int i = 0; i < N; ++i) {
std::cout << "After sorting: ";
myList.push_back(20); // Insert at the Node* search(Node* root, int key, Node*& int value;
back for (int x : myList) { parent) {
cin >> value;
myList.push_front(10); // Insert at std::cout << x << " "; Node* current = root;
root = insert(root, value);
the front } parent = nullptr;
}
myList.push_back(30); std::cout << std::endl; while (current != nullptr && current->key
Print_tree(root);
myList.push_front(5); // 7. Clearing the list != key) {
cout << endl;
std::cout << "Initial list: "; myList.clear(); parent = current;
while (true) {
for (int x : myList) { std::cout << "List cleared. Size: " << if (key < current->key) current =
myList.size() << std::endl; current->left; cin >> choice;
std::cout << x << " ";
else current = current->right; if (choice == -1) break;
}
return 0; } if (choice == 3) {
std::cout << std::endl;
} return current; int searchKey;
// 2. Insert at a specific position using
iterator } cin >> searchKey;
TREE Node* parent = nullptr;
auto it = myList.begin(); int height(Node* node) {
#include <iostream> Node* foundNode = search(root,
std::advance(it, 2); // Move iterator if (node == nullptr) return -1;
to the 3rd position (index 2) using namespace std; searchKey, parent);
int leftHeight = height(node->left);
myList.insert(it, 15); // Insert 15 struct Node { if (foundNode != nullptr) {
int rightHeight = height(node->right);
before the 3rd position int key; cout << "Present\n";
return max(leftHeight, rightHeight) +
std::cout << "After inserting 15: "; Node* left; 1;} Node* predecessor = nullptr;
for (int x : myList) { Node* right; Node* successor = nullptr;
std::cout << x << " "; int height; void findBeforeAfter(Node* root, int key, findBeforeAfter(root,
} }; Node*& predecessor, Node*& successor) { searchKey, predecessor, successor);
std::cout << std::endl; if (root == nullptr) return; cout << "Parent(";
Node* newNode(int key) { if (root->key == key) { if (parent != nullptr)
cout << parent->key;
// 3. Remove elements from the front Node* node = new Node(); if (root->left != nullptr) {
and back else cout << "null";
node->key = key; Node* temp = root->left;
myList.pop_front(); cout << "), ";
node->left = node->right = nullptr; while (temp->right) {
myList.pop_back(); cout << "Left(";
node->height = 0; temp = temp->right;
if (foundNode->left) cout
return node; }
<< foundNode->left->key;
else cout << "null"; if (root == nullptr) return; int N; root = insert(root, key);
cout << "), "; cout << root->key << "("; cin >> N; }
cout << "Right("; if (parent) cout << parent->key; Node* root = nullptr; int q;
if (foundNode->right) cout else cout << "null"; for (int i = 0; i < N; ++i) { cin >> q;
<< foundNode->right->key; cout << ") "; int value; for (int i = 0; i < q; ++i) {
else cout << "null"; preorder(root->left, root); cin >> value; int u, v;
cout << ")\n"; preorder(root->right, root); root = insert(root, value); cin >> u >> v;
} else { cout << "Not Present\n"; } } Node* lca = LCA(root, u, v);
} else if (choice == 4) { void postorder(Node* root, Node* parent) { int choice; if (lca != nullptr) cout << lca-
int searchKey; if (root == nullptr) return; while (true) { >key << endl;
cin >> searchKey; postorder(root->left, root); cin >> choice; }
Node* parent = nullptr; postorder(root->right, root); if (choice == -1) break; return 0;
Node* foundNode = search(root, cout << root->key << "("; if (choice == 1) { }
searchKey, parent);
if (parent) cout << parent->key; cout << "Inorder: ";
if (foundNode != nullptr) {
else cout << "null"; inorder(root, nullptr);
cout << "Height: " <<
cout << ") "; cout << endl;
height(foundNode) << endl;
} } else if (choice == 2) {
} else cout << "Node not
found." << endl; void level_order(Node* root) { cout << "Preorder: ";
} else if (choice == 5) { if (root == nullptr) return; preorder(root, nullptr);
int beforeAfterKey; queue<Node*> nodeQueue; cout << endl;
cin >> beforeAfterKey; queue<Node*> parentQueue; } else if (choice == 3) {
Node* predecessor = nullptr; nodeQueue.push(root); cout << "Postorder: ";
Node* successor = nullptr; parentQueue.push(nullptr); postorder(root, nullptr);
findBeforeAfter(root, beforeAfterKey, int currentLevel = 1; cout << endl;
predecessor, successor); while (!nodeQueue.empty()) { } else if (choice == 4)
if (predecessor != nullptr) cout << int size = nodeQueue.size(); level_order(root);
predecessor->key << " "; cout << "Level " << currentLevel << ": "; }
else cout << "null "; for (int i = 0; i < size; i++) { return 0;
if (successor != nullptr) cout << Node* current = }
successor->key << endl; nodeQueue.front(); LCA:
else cout << "null" << endl; Node* parent =
#include <iostream>
} parentQueue.front();
using namespace std;
} return 0; nodeQueue.pop();
struct Node {
} parentQueue.pop();
int key;
Order traversal: cout << current->key << "(";
Node* left;
#include <iostream> if (parent) cout << parent-
>key; Node* right;
#include <queue>
else cout << "null"; int height;
using namespace std;
cout << ") "; };
struct Node {
if (current->left) { Node* newNode(int key) {}
int key;
nodeQueue.push(current- Node* insert(Node* root, int key) ;
Node* left;
>left); Node* LCA(Node* root, int u, int v) {
Node* right;
parentQueue.push(current); if (root == nullptr) return nullptr;
};
} if (u < root->key && v < root->key)
Node* newNode(int key); return LCA(root->left, u, v);
if (current->right) {
Node* insert(Node* root, int key) ; if (u > root->key && v > root->key)
nodeQueue.push(current-
void inorder(Node* root, Node* parent) { >right); return LCA(root->right, u, v);
if (root == nullptr) return; parentQueue.push(current); return root;
inorder(root->left, root); } }
cout << root->key << "("; } int main() {
if (parent) cout << parent->key; cout << endl; int N;
else cout << "null"; currentLevel++; cin >> N;
cout << ") "; } Node* root = nullptr;
inorder(root->right, root); } for (int i = 0; i < N; ++i) {
} int main() { int key;
void preorder(Node* root, Node* parent) { cin >> key;

You might also like