Advertisement
Coder_22

Untitled

Mar 8th, 2023 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.07 KB | None | 0 0
  1. String Palindrome:
  2. bool palindrome(string s) {
  3.     string r=s;
  4.     reverse(s.begin(),s.end());
  5.     if(s==r) return true;
  6.     else return false;
  7. }
  8.  
  9. Binary Search:
  10. bool bsearch(int ar[], int n, int e) {
  11.     int l = 0, h = n - 1;
  12.     while (h - l > 1) {
  13.         int mid = (h + l) / 2;
  14.         if (ar[mid] < e) l = mid + 1;
  15.         else h = mid;
  16.     }
  17.     return (ar[l] == e || ar[h] == e);
  18. }
  19.  
  20. Bubble Sort:
  21. void bubblesort(int ar[], int n) {
  22.     for (int i = 0; i < n - 1; ++i) {
  23.         for (int j = 0; j < n - i - 1; ++j) {
  24.             if (ar[j] > ar[j + 1])
  25.                 swap(ar[j], ar[j + 1]);
  26.         }
  27.     }
  28. }
  29.  
  30. Radix Sort:
  31. void radixsort(int ar[], int n) {
  32.     int mx = *max_element(ar, ar + n);
  33.     for (int exp = 1; mx / exp > 0; exp *= 10) {
  34.         int op[n], cnt[10]{};
  35.         for (int i = 0; i < n; ++i) ++cnt[(ar[i] / exp) % 10];
  36.         for (int i = 1; i < 10; ++i) cnt[i] += cnt[i - 1];
  37.         for (int i = n - 1; i >= 0; --i) op[--cnt[(ar[i] / exp) % 10]] = ar[i];
  38.         copy(op, op + n, ar);
  39.     }
  40. }
  41.  
  42. Bucket Sort:
  43. void bubblesort(int ar[], int n) {
  44.     for (int i = 0; i < n - 1; ++i) {
  45.         for (int j = 0; j < n - i - 1; ++j) {
  46.             if (ar[j] > ar[j + 1])
  47.                 swap(ar[j], ar[j + 1]);
  48.         }
  49.     }
  50. }
  51. void bucketsort(int ar[], int n) {
  52.     int buck = (*max_element(ar, ar + n) / 100) + 1;
  53.     vector<int> bucket[buck];
  54.     for (int i = 0; i < n; ++i) bucket[ar[i] / 100].push_back(ar[i]);
  55.     for (int i = 0; i < buck; ++i) bubblesort(bucket[i]);
  56.     int k = 0;
  57.     for (auto& i : bucket) for (auto& j : i) ar[k++] = j;
  58. }
  59.  
  60. Stack using array:
  61. #include <bits/stdc++.h>
  62. using namespace std;
  63. int stk[100], top = -1;
  64. void push(int val) {
  65.     if (top >= 100 - 1) cout << "Stack Overflow" << '\n';
  66.     else stk[++top] = val;
  67. }
  68. void pop() {
  69.     if (top <= -1) cout << "Stack Underflow" << '\n';
  70.     else cout << "The popped element is " << stk[top--] << '\n';
  71. }
  72. void display() {
  73.     if (top >= 0) {
  74.         cout << "Stack elements are: ";
  75.         for (int i = top; i >= 0; --i) cout << stk[i] << ' ';
  76.     }
  77.     else cout << "Stack is empty!!!";
  78.     cout << '\n';
  79. }
  80. int main() {
  81.     cout << "1) Push in stack" << '\n';
  82.     cout << "2) Pop from stack" << '\n';
  83.     cout << "3) Display stack" << '\n';
  84.     cout << "4) Exit" << '\n';
  85.     int ch;
  86.     do {
  87.         cout << "Enter choice: "; cin >> ch;
  88.         switch (ch) {
  89.         case 1: {
  90.             cout << "Enter value to be pushed: " << '\n';
  91.             int val; cin >> val; push(val); break;
  92.         }
  93.         case 2: {
  94.             pop(); break;
  95.         }
  96.         case 3: {
  97.             display(); break;
  98.         }
  99.         case 4: {
  100.             cout << "Exit" << '\n'; break;
  101.         }
  102.         default:
  103.             cout << "Invalid Choice" << '\n';
  104.         }
  105.     } while (ch != 4);
  106.     return 0;
  107. }
  108.  
  109. Queue using array:
  110. #include <bits/stdc++.h>
  111. using namespace std;
  112. int q[100], front = -1, rear = -1;
  113. void insert() {
  114.     int val;
  115.     if (rear == 100 - 1) cout << "Queue Overflow" << '\n';
  116.     else {
  117.         if (front == -1) front = 0;
  118.         cout << "Insert the element in queue: " << '\n';
  119.         cin >> val; q[++rear] = val;
  120.     }
  121. }
  122. void remove() {
  123.     if (front == -1 || front > rear) cout << "Queue Underflow ";
  124.     else cout << "Element removed from queue is: " << q[front++] << '\n';
  125. }
  126. void display() {
  127.     if (front == -1) cout << "Queue is empty" << '\n';
  128.     else {
  129.         cout << "Queue elements are : ";
  130.         for (int i = front; i <= rear; i++) cout << q[i] << " ";
  131.         cout << '\n';
  132.     }
  133. }
  134. int main() {
  135.     cout << "1) Insert element to queue" << '\n';
  136.     cout << "2) remove element from queue" << '\n';
  137.     cout << "3) Display all the elements of queue" << '\n';
  138.     cout << "4) Exit" << '\n';
  139.     int ch;
  140.     do {
  141.         cout << "Enter your choice : " << '\n';
  142.         cin >> ch;
  143.         switch (ch) {
  144.         case 1: insert(); break;
  145.         case 2: remove(); break;
  146.         case 3: display(); break;
  147.         case 4: cout << "Exit" << '\n'; break;
  148.         default: cout << "Invalid choice" << '\n';
  149.         }
  150.     } while (ch != 4);
  151.     return 0;
  152. }
  153.  
  154. Linked list:
  155. #include <bits/stdc++.h>
  156. using namespace std;
  157. class Node {
  158. public:
  159.     int data; Node* next;
  160.     Node() {
  161.         data = 0; next = NULL;
  162.     }
  163.     Node(int data) {
  164.         this->data = data; this->next = NULL;
  165.     }
  166. };
  167. class Linkedlist {
  168.     Node* head;
  169. public:
  170.     Linkedlist() { head = NULL; }
  171.     void insert();
  172.     void display();
  173.     void remove();
  174. };
  175. void Linkedlist::remove() {
  176.     cout << "Enter the element to remove: ";
  177.     int ele; cin >> ele;
  178.     Node* tmp1 = head, * tmp2 = NULL;
  179.     int len = 0;
  180.     if (head == NULL) {
  181.         cout << "List is empty." << '\n'; return;
  182.     }
  183.     while (tmp1 != NULL) {
  184.         tmp1 = tmp1->next; ++len;
  185.     }
  186.     if (len < ele) {
  187.         cout << "Index out of range" << '\n'; return;
  188.     }
  189.     tmp1 = head;
  190.     if (ele == 1) {
  191.         head = head->next;
  192.         delete tmp1; return;
  193.     }
  194.     while (--ele) {
  195.         tmp2 = tmp1; tmp1 = tmp1->next;
  196.     }
  197.     tmp2->next = tmp1->next;
  198.     delete tmp1;
  199. }
  200. void Linkedlist::insert() {
  201.     cout << "Enter the element to insert: ";
  202.     int data; cin >> data;
  203.     Node* newNode = new Node(data);
  204.     if (head == NULL) {
  205.         head = newNode; return;
  206.     }
  207.     Node* tmp = head;
  208.     while (tmp->next != NULL) tmp = tmp->next;
  209.     tmp->next = newNode;
  210. }
  211. void Linkedlist::display() {
  212.     cout << "Elements of the list are: ";
  213.     Node* tmp = head;
  214.     if (head == NULL) {
  215.         cout << "List is empty\n"; return;
  216.     }
  217.     while (tmp != NULL) {
  218.         cout << tmp->data << ' '; tmp = tmp->next;
  219.     }
  220.     cout << '\n';
  221. }
  222. int main() {
  223.     Linkedlist list;
  224.     cout << "1) Insert element to list" << '\n';
  225.     cout << "2) Remove element from list" << '\n';
  226.     cout << "3) Display all the elements of list" << '\n';
  227.     cout << "4) Exit" << '\n';
  228.     int ch;
  229.     do {
  230.         cout << "Enter your choice : " << '\n';
  231.         cin >> ch;
  232.         switch (ch) {
  233.         case 1: list.insert(); break;
  234.         case 2: list.remove(); break;
  235.         case 3: list.display(); break;
  236.         case 4: cout << "Exit" << '\n'; break;
  237.         default: cout << "Invalid choice" << '\n';
  238.         }
  239.     } while (ch != 4);
  240.     return 0;
  241. }
  242.  
  243. BST:
  244. #include <bits/stdc++.h>
  245. using namespace std;
  246. class Node {
  247. public:
  248.     int val; Node* left, * right;
  249.     Node(int val): val(val), left(NULL), right(NULL) { }
  250. };
  251. void insert(Node*& root) {
  252.     cout << "Enter the element to insert: ";
  253.     int key; cin >> key;
  254.     Node* node = new Node(key);
  255.     if (!root) {
  256.         root = node; return;
  257.     }
  258.     Node* prev = NULL, * temp = root;
  259.     while (temp) {
  260.         if (temp->val > key) {
  261.             prev = temp; temp = temp->left;
  262.         }
  263.         else if (temp->val < key) {
  264.             prev = temp; temp = temp->right;
  265.         }
  266.     }
  267.     if (prev->val > key) prev->left = node;
  268.     else prev->right = node;
  269. }
  270. void inorder(Node* node) {
  271.     if (node == NULL) return;
  272.     inorder(node->left);
  273.     cout << node->val << ' ';
  274.     inorder(node->right);
  275. }
  276. void preorder(Node* node) {
  277.     if (node == NULL) return;
  278.     cout << node->val << ' ';
  279.     preorder(node->left);
  280.     preorder(node->right);
  281. }
  282. void postorder(Node* node) {
  283.     if (node == NULL) return;
  284.     postorder(node->left);
  285.     postorder(node->right);
  286.     cout << node->val << ' ';
  287. }
  288. int main() {
  289.     Node* root = NULL;
  290.     cout << "1) Insert element to BST" << '\n';
  291.     cout << "2) Display inorder traversal" << '\n';
  292.     cout << "3) Display preorder traversal" << '\n';
  293.     cout << "4) Display postorder traversal" << '\n';
  294.     cout << "5) Exit" << '\n';
  295.     int ch;
  296.     do {
  297.         cout << "Enter your choice : " << '\n';
  298.         cin >> ch;
  299.         switch (ch) {
  300.         case 1: insert(root); break;
  301.         case 2: inorder(root); break;
  302.         case 3: preorder(root); break;
  303.         case 4: postorder(root); break;
  304.         case 5: cout << "Exit" << '\n'; break;
  305.         default: cout << "Invalid choice" << '\n';
  306.         }
  307.     } while (ch != 5);
  308.     return 0;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement