Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Khufiya Doc

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Stack ADT

#include <iostream>
#include <string>
using namespace std;
#define max 10

int top = -1;


int stack[max];

void push(int item);


int pop();
void print_items();

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

void push(int item){

if(top >= max - 1 ){


// cout << "Overflow detected\n";
return;
}
top++;
stack[top] = item;
cout << item << " was pushed into the stack\n";
}

int pop(){
if(top == -1){
// cout << "Underflow detected\n";
return 0;
}

int item = stack[top];


top--;
cout << item << " was popped out of the stack\n";
return item;

void print_items(){

if(top == -1){
cout << "No items to display, the stack is empty\n";
return;
}

cout << "The items of the stack are:\n";

for(int i = 0; i <= top; i++){


cout << stack[i] << "\t";
}

Parentheses
#include <iostream>
using namespace std;
#define MAX 100

int top = -1;


string array[MAX];

void push(char brackets)


{
if (top == MAX)
{

cout << "OVERFLOW\n";


return;
}
top++;
array[top] = brackets;
}

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 top = -1;


char stack[max];

void push(char item);


char pop();
void print_items();
void brackets(char ch);

int main()
{

string lmao;
cin >> lmao;

for (int i = 0; i < lmao.length(); i++)


{
push(lmao[i]);
}

for (int i = lmao.length(); i >= 0; i--)


{
cout << pop();
}

return 0;
}

void push(char item)


{

if (top >= max - 1)


{
// cout << "Overflow detected\n";
return;
}
top++;
stack[top] = item;
}

char pop()
{

if (top == -1)
{
// cout << "Underflow detected\n";
return 0;
}

char item = stack[top];


top--;
return item;
}

void print_items()
{

if (top == -1)
{
cout << "No items to display, the stack is empty\n";
return;
}

cout << "The items of the stack are:\n";

for (int i = 0; i <= top; i++)


{
cout << stack[i] << "\t";
}
}

void brackets(char ch)


{

if ((ch == '}' || ch == ')' || ch == ']') && top != -1)


{
pop();
}
else
{
push(ch);
}
}

Queue ADT
#include <iostream>
using namespace std;
#define MAX 10

int queue[MAX];
int front = -1;
int rear = -1;

void enqueue(int data);


int dequeue();
void print_queue();

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

void enqueue(int data){

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;

void enqueue(int data);


int dequeue();
void print_queue();

int main(){

enqueue(1);
enqueue(2);

enqueue(3);

enqueue(4);

print_queue();

return 0;
}

void enqueue(int data){

if(( rear == MAX - 1 && front == 0) || ( front == rear + 1)){


cout << "Overflow\n";
return;
}

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

void insert_at_end(struct Node **head, int data)


{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = NULL;
struct Node *temp = (*head);
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}

void insert_at_sep(struct Node *prev_node, int data)


{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = prev_node->next;
prev_node->next = newnode;
}

void del_at_start(struct Node **head)


{
if (head == NULL)
{
cout << "phela jaine coding sekh , pachi aav";
return;
}
struct Node *temp = (*head);
(*head) = (*head)->next;
delete temp;
}

void del_at_end(struct Node **head)


{
struct Node *temp = (*head);
while (temp->next->next != NULL)
{
temp = temp->next;
}
temp->next = NULL;
delete temp;
}

void del_at_sep(struct Node *head)


{

struct Node *temp = head;


head = head->next;
delete temp;
}

void display(struct Node *head)


{
if (head == NULL)
{
cout << "phela jaine coding sekh , pachi aav";
return;
}
while (head != NULL)
{
cout << head->data << "\t";
head = head->next;
}
}

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

void insert_at_end(struct Node **head, int data)


{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = NULL;
struct Node *temp = (*head);
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}

void insert_at_sep(struct Node *prev_node, int data)


{
struct Node *newnode = new Node;
newnode->data = data;
newnode->next = prev_node->next;
prev_node->next = newnode;
}

void del_at_start(struct Node **head)


{
if (head == NULL)
{
cout << "phela jaine coding sekh , pachi aav";
return;
}
struct Node *temp = (*head);
(*head) = (*head)->next;
delete temp;
}
void del_at_end(struct Node **head)
{

struct Node *temp = (*head);


while (temp->next->next != NULL)
{
temp = temp->next;
}
struct Node *last = temp->next;
temp->next = NULL;

delete last;
}

void del_at_sep(struct Node *prev_node)


{

struct Node *temp = prev_node->next;


prev_node->next = temp->next;
delete temp;
}

void display(struct Node *head)


{
if (head == NULL)
{
cout << "phela jaine coding sekh , pachi aav";
return;
}
while (head != NULL)
{
cout << head->data << "\t";
head = head->next;
}
}

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 smallest(int arr[], int index, int size){


int smallest = arr[index];
int smallest_index = index;
for(int i = index ; i < size; i++){
if(arr[i] < smallest){
smallest = arr[i];
smallest_index = i;
}
}
return smallest_index;
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

void selectionsort(int arr[], int size){

for(int i = 0; i < size ; i++){


int smallest_index = smallest(arr,i,size);
printArray(arr,size);
if(i != smallest_index){
int temp = arr[i];
arr[i] = arr[smallest_index];
arr[smallest_index] = temp;
}

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;

void bubbleSort(int arr[], int n)


{
int i, j;
for (i = 0; i < n - 1; i++){

for (j = 0; j < n - i - 1; j++){


if (arr[j] > arr[j + 1]){
swap(arr[j], arr[j + 1]);
}
}
}
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}

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;

void insertionsort(int arr[], int size){


int i , j, key;
for(i = 1; i < size; i++){
j = i - 1;
key = arr[i];
while(j >= 0 && arr[j] > key){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}

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

You might also like