Khufiya Doc
Khufiya Doc
Khufiya Doc
#include <iostream>
#include <string>
using namespace std;
#define max 10
int main(){
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
pop();
push(7);
push(8);
push(9);
push(8);
print_items();
return 0;
}
int pop(){
if(top == -1){
// cout << "Underflow detected\n";
return 0;
}
void print_items(){
if(top == -1){
cout << "No items to display, the stack is empty\n";
return;
}
Parentheses
#include <iostream>
using namespace std;
#define MAX 100
int pop()
{
if (top == -1)
{
cout << "Underflow\n";
return -1;
}
top--;
return 0;
}
void print()
{
if (top == -1)
{
cout << "Empty array\n";
return;
}
cout << "The elements in stack are: ";
for (int i = 0; i <= top; i++)
{
cout << array[i] << "\t";
}
cout << endl;
}
int main()
{
bool isEmpty = true;
string a = "{{}{}{}}";
for (char b : a)
{
if (b == '{' || b == '[' || b == '(')
{
push(b);
}
else if (b == '}' || b == ']' || b == ')')
{
int temp = pop();
if (temp == -1)
{
isEmpty = false;
break;
}
}
}
if (isEmpty == false)
{
cout << "Unbalanced\n";
}
else
{
cout << "Balanced\n";
}
return 0;
}
Rev String
#include <iostream>
#include <string>
using namespace std;
#define max 10
int main()
{
string lmao;
cin >> lmao;
return 0;
}
char pop()
{
if (top == -1)
{
// cout << "Underflow detected\n";
return 0;
}
void print_items()
{
if (top == -1)
{
cout << "No items to display, the stack is empty\n";
return;
}
Queue ADT
#include <iostream>
using namespace std;
#define MAX 10
int queue[MAX];
int front = -1;
int rear = -1;
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
enqueue(6);
dequeue();
dequeue();
dequeue();
enqueue(7);
enqueue(8);
enqueue(9);
enqueue(10);
enqueue(11);
enqueue(12);
print_queue();
return 0;
}
if(rear == MAX-1){
cout << "Queue overflow\n";
return;
}
if(front == -1){
front = 0;
rear = 0;
}else{
rear++;
}
queue[rear] = data;
return;
int dequeue(){
int data = 0;
if(front == -1 || front > rear){
cout << "Queue underflow\n";
return data;
}
if(front == rear){
front = -1;
rear = -1;
}else{
data = queue[front];
front++;
}
return data;
void print_queue(){
cout << "The elements of the queue are: \n";
for(int i = front ; i <= rear; i++){
cout << queue[i] << endl;
}
Circular Queue
#include <iostream>
using namespace std;
#define MAX 10
int queue[MAX];
int front = -1;
int rear = -1;
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
print_queue();
return 0;
}
if(front == -1){
front = 0;
rear = 0;
}else if (rear == MAX - 1 || front != 0){
rear = 0;
}else{
rear++;
}
queue[rear] = data;
return;
}
int dequeue(){
int data = 0;
if(front == -1){
cout << "Underflow\n";
return data;
}
data = queue[front];
if(front == rear){
front = -1;
rear = -1;
}else if(front == MAX - 1){
front = 1;
}else{
front++;
}
return data;
void print_queue(){
cout << "The elements of the queue are: \n";
for(int i = 0 ; i <= MAX; i++){
cout << queue[i] << endl;
}
Linked List
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void insert_at_beg(struct Node **head, int data)
{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = (*head);
(*head) = newnode;
}
int main()
{
struct Node *head = NULL;
insert_at_beg(&head, 10);
insert_at_sep(head, 15);
insert_at_end(&head, 20);
// del_at_start(&head);
// del_at_end(&head);
// insert_at_end(&head, 30);
// insert_at_end(&head, 40);
display(head);
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void insert_at_beg(struct Node **head, int data)
{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = (*head);
(*head) = newnode;
}
delete last;
}
int main()
{
struct Node *head = NULL;
insert_at_beg(&head, 10);
insert_at_sep(head, 15);
insert_at_end(&head, 20);
del_at_start(&head);
del_at_end(&head);
insert_at_end(&head, 30);
insert_at_end(&head, 40);
del_at_sep(head->next);
display(head);
return 0;
}
Binary Search
int binary_search(int arr[], int x , int low, int high){
int mid;
if(low > high){
return -1;
}else{
mid = (low+high)/2;
if(x == arr[mid]){
return mid;
}else if(x > arr[mid]){
// right side
return binary_search(arr,x,mid+1,high);
}else{
// left side
return binary_search(arr,x,low,mid-1);
}
}
}
Selection Sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {3,12,-5,6,142,21,-17,45};
int n = sizeof(arr)/sizeof(arr[0]);
selectionsort(arr,n);
return 0;
}
Bubble Sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 5, 1, 4, 2, 8};
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
Insertion Sort
#include <iostream>
using namespace std;
int main()
{
int arr[] = {12,123,34,36,5346,235,234,12,757};
int n = sizeof(arr)/sizeof(arr[0]);
insertionsort(arr,n);
for(int i : arr){
cout << i << "\t";
}
return 0;
}
Numerical
Infix to Prefix
Infix to postfix
Row & Column Major
Normal