Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String Palindrome:
- bool palindrome(string s) {
- string r=s;
- reverse(s.begin(),s.end());
- if(s==r) return true;
- else return false;
- }
- Binary Search:
- bool bsearch(int ar[], int n, int e) {
- int l = 0, h = n - 1;
- while (h - l > 1) {
- int mid = (h + l) / 2;
- if (ar[mid] < e) l = mid + 1;
- else h = mid;
- }
- return (ar[l] == e || ar[h] == e);
- }
- Bubble Sort:
- void bubblesort(int ar[], int n) {
- for (int i = 0; i < n - 1; ++i) {
- for (int j = 0; j < n - i - 1; ++j) {
- if (ar[j] > ar[j + 1])
- swap(ar[j], ar[j + 1]);
- }
- }
- }
- Radix Sort:
- void radixsort(int ar[], int n) {
- int mx = *max_element(ar, ar + n);
- for (int exp = 1; mx / exp > 0; exp *= 10) {
- int op[n], cnt[10]{};
- for (int i = 0; i < n; ++i) ++cnt[(ar[i] / exp) % 10];
- for (int i = 1; i < 10; ++i) cnt[i] += cnt[i - 1];
- for (int i = n - 1; i >= 0; --i) op[--cnt[(ar[i] / exp) % 10]] = ar[i];
- copy(op, op + n, ar);
- }
- }
- Bucket Sort:
- void bubblesort(int ar[], int n) {
- for (int i = 0; i < n - 1; ++i) {
- for (int j = 0; j < n - i - 1; ++j) {
- if (ar[j] > ar[j + 1])
- swap(ar[j], ar[j + 1]);
- }
- }
- }
- void bucketsort(int ar[], int n) {
- int buck = (*max_element(ar, ar + n) / 100) + 1;
- vector<int> bucket[buck];
- for (int i = 0; i < n; ++i) bucket[ar[i] / 100].push_back(ar[i]);
- for (int i = 0; i < buck; ++i) bubblesort(bucket[i]);
- int k = 0;
- for (auto& i : bucket) for (auto& j : i) ar[k++] = j;
- }
- Stack using array:
- #include <bits/stdc++.h>
- using namespace std;
- int stk[100], top = -1;
- void push(int val) {
- if (top >= 100 - 1) cout << "Stack Overflow" << '\n';
- else stk[++top] = val;
- }
- void pop() {
- if (top <= -1) cout << "Stack Underflow" << '\n';
- else cout << "The popped element is " << stk[top--] << '\n';
- }
- void display() {
- if (top >= 0) {
- cout << "Stack elements are: ";
- for (int i = top; i >= 0; --i) cout << stk[i] << ' ';
- }
- else cout << "Stack is empty!!!";
- cout << '\n';
- }
- int main() {
- cout << "1) Push in stack" << '\n';
- cout << "2) Pop from stack" << '\n';
- cout << "3) Display stack" << '\n';
- cout << "4) Exit" << '\n';
- int ch;
- do {
- cout << "Enter choice: "; cin >> ch;
- switch (ch) {
- case 1: {
- cout << "Enter value to be pushed: " << '\n';
- int val; cin >> val; push(val); break;
- }
- case 2: {
- pop(); break;
- }
- case 3: {
- display(); break;
- }
- case 4: {
- cout << "Exit" << '\n'; break;
- }
- default:
- cout << "Invalid Choice" << '\n';
- }
- } while (ch != 4);
- return 0;
- }
- Queue using array:
- #include <bits/stdc++.h>
- using namespace std;
- int q[100], front = -1, rear = -1;
- void insert() {
- int val;
- if (rear == 100 - 1) cout << "Queue Overflow" << '\n';
- else {
- if (front == -1) front = 0;
- cout << "Insert the element in queue: " << '\n';
- cin >> val; q[++rear] = val;
- }
- }
- void remove() {
- if (front == -1 || front > rear) cout << "Queue Underflow ";
- else cout << "Element removed from queue is: " << q[front++] << '\n';
- }
- void display() {
- if (front == -1) cout << "Queue is empty" << '\n';
- else {
- cout << "Queue elements are : ";
- for (int i = front; i <= rear; i++) cout << q[i] << " ";
- cout << '\n';
- }
- }
- int main() {
- cout << "1) Insert element to queue" << '\n';
- cout << "2) remove element from queue" << '\n';
- cout << "3) Display all the elements of queue" << '\n';
- cout << "4) Exit" << '\n';
- int ch;
- do {
- cout << "Enter your choice : " << '\n';
- cin >> ch;
- switch (ch) {
- case 1: insert(); break;
- case 2: remove(); break;
- case 3: display(); break;
- case 4: cout << "Exit" << '\n'; break;
- default: cout << "Invalid choice" << '\n';
- }
- } while (ch != 4);
- return 0;
- }
- Linked list:
- #include <bits/stdc++.h>
- using namespace std;
- class Node {
- public:
- int data; Node* next;
- Node() {
- data = 0; next = NULL;
- }
- Node(int data) {
- this->data = data; this->next = NULL;
- }
- };
- class Linkedlist {
- Node* head;
- public:
- Linkedlist() { head = NULL; }
- void insert();
- void display();
- void remove();
- };
- void Linkedlist::remove() {
- cout << "Enter the element to remove: ";
- int ele; cin >> ele;
- Node* tmp1 = head, * tmp2 = NULL;
- int len = 0;
- if (head == NULL) {
- cout << "List is empty." << '\n'; return;
- }
- while (tmp1 != NULL) {
- tmp1 = tmp1->next; ++len;
- }
- if (len < ele) {
- cout << "Index out of range" << '\n'; return;
- }
- tmp1 = head;
- if (ele == 1) {
- head = head->next;
- delete tmp1; return;
- }
- while (--ele) {
- tmp2 = tmp1; tmp1 = tmp1->next;
- }
- tmp2->next = tmp1->next;
- delete tmp1;
- }
- void Linkedlist::insert() {
- cout << "Enter the element to insert: ";
- int data; cin >> data;
- Node* newNode = new Node(data);
- if (head == NULL) {
- head = newNode; return;
- }
- Node* tmp = head;
- while (tmp->next != NULL) tmp = tmp->next;
- tmp->next = newNode;
- }
- void Linkedlist::display() {
- cout << "Elements of the list are: ";
- Node* tmp = head;
- if (head == NULL) {
- cout << "List is empty\n"; return;
- }
- while (tmp != NULL) {
- cout << tmp->data << ' '; tmp = tmp->next;
- }
- cout << '\n';
- }
- int main() {
- Linkedlist list;
- cout << "1) Insert element to list" << '\n';
- cout << "2) Remove element from list" << '\n';
- cout << "3) Display all the elements of list" << '\n';
- cout << "4) Exit" << '\n';
- int ch;
- do {
- cout << "Enter your choice : " << '\n';
- cin >> ch;
- switch (ch) {
- case 1: list.insert(); break;
- case 2: list.remove(); break;
- case 3: list.display(); break;
- case 4: cout << "Exit" << '\n'; break;
- default: cout << "Invalid choice" << '\n';
- }
- } while (ch != 4);
- return 0;
- }
- BST:
- #include <bits/stdc++.h>
- using namespace std;
- class Node {
- public:
- int val; Node* left, * right;
- Node(int val): val(val), left(NULL), right(NULL) { }
- };
- void insert(Node*& root) {
- cout << "Enter the element to insert: ";
- int key; cin >> key;
- Node* node = new Node(key);
- if (!root) {
- root = node; return;
- }
- Node* prev = NULL, * temp = root;
- while (temp) {
- if (temp->val > key) {
- prev = temp; temp = temp->left;
- }
- else if (temp->val < key) {
- prev = temp; temp = temp->right;
- }
- }
- if (prev->val > key) prev->left = node;
- else prev->right = node;
- }
- void inorder(Node* node) {
- if (node == NULL) return;
- inorder(node->left);
- cout << node->val << ' ';
- inorder(node->right);
- }
- void preorder(Node* node) {
- if (node == NULL) return;
- cout << node->val << ' ';
- preorder(node->left);
- preorder(node->right);
- }
- void postorder(Node* node) {
- if (node == NULL) return;
- postorder(node->left);
- postorder(node->right);
- cout << node->val << ' ';
- }
- int main() {
- Node* root = NULL;
- cout << "1) Insert element to BST" << '\n';
- cout << "2) Display inorder traversal" << '\n';
- cout << "3) Display preorder traversal" << '\n';
- cout << "4) Display postorder traversal" << '\n';
- cout << "5) Exit" << '\n';
- int ch;
- do {
- cout << "Enter your choice : " << '\n';
- cin >> ch;
- switch (ch) {
- case 1: insert(root); break;
- case 2: inorder(root); break;
- case 3: preorder(root); break;
- case 4: postorder(root); break;
- case 5: cout << "Exit" << '\n'; break;
- default: cout << "Invalid choice" << '\n';
- }
- } while (ch != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement