mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 02:30:40 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
typedef struct node {
|
||||
int data;
|
||||
int height;
|
||||
struct node *left;
|
||||
@@ -15,8 +14,7 @@ int max(int a, int b) { return a > b ? a : b; }
|
||||
|
||||
// Returns a new Node
|
||||
|
||||
node *createNode(int data)
|
||||
{
|
||||
node *createNode(int data) {
|
||||
node *nn = new node();
|
||||
nn->data = data;
|
||||
nn->height = 0;
|
||||
@@ -27,8 +25,7 @@ node *createNode(int data)
|
||||
|
||||
// Returns height of tree
|
||||
|
||||
int height(node *root)
|
||||
{
|
||||
int height(node *root) {
|
||||
if (root == NULL)
|
||||
return 0;
|
||||
return 1 + max(height(root->left), height(root->right));
|
||||
@@ -40,8 +37,7 @@ int getBalance(node *root) { return height(root->left) - height(root->right); }
|
||||
|
||||
// Returns Node after Right Rotation
|
||||
|
||||
node *rightRotate(node *root)
|
||||
{
|
||||
node *rightRotate(node *root) {
|
||||
node *t = root->left;
|
||||
node *u = t->right;
|
||||
t->right = root;
|
||||
@@ -51,8 +47,7 @@ node *rightRotate(node *root)
|
||||
|
||||
// Returns Node after Left Rotation
|
||||
|
||||
node *leftRotate(node *root)
|
||||
{
|
||||
node *leftRotate(node *root) {
|
||||
node *t = root->right;
|
||||
node *u = t->left;
|
||||
t->left = root;
|
||||
@@ -62,8 +57,7 @@ node *leftRotate(node *root)
|
||||
|
||||
// Returns node with minimum value in the tree
|
||||
|
||||
node *minValue(node *root)
|
||||
{
|
||||
node *minValue(node *root) {
|
||||
if (root->left == NULL)
|
||||
return root;
|
||||
return minValue(root->left);
|
||||
@@ -71,8 +65,7 @@ node *minValue(node *root)
|
||||
|
||||
// Balanced Insertion
|
||||
|
||||
node *insert(node *root, int item)
|
||||
{
|
||||
node *insert(node *root, int item) {
|
||||
node *nn = createNode(item);
|
||||
if (root == NULL)
|
||||
return nn;
|
||||
@@ -81,14 +74,11 @@ node *insert(node *root, int item)
|
||||
else
|
||||
root->right = insert(root->right, item);
|
||||
int b = getBalance(root);
|
||||
if (b > 1)
|
||||
{
|
||||
if (b > 1) {
|
||||
if (getBalance(root->left) < 0)
|
||||
root->left = leftRotate(root->left); // Left-Right Case
|
||||
return rightRotate(root); // Left-Left Case
|
||||
}
|
||||
else if (b < -1)
|
||||
{
|
||||
} else if (b < -1) {
|
||||
if (getBalance(root->right) > 0)
|
||||
root->right = rightRotate(root->right); // Right-Left Case
|
||||
return leftRotate(root); // Right-Right Case
|
||||
@@ -98,8 +88,7 @@ node *insert(node *root, int item)
|
||||
|
||||
// Balanced Deletion
|
||||
|
||||
node *deleteNode(node *root, int key)
|
||||
{
|
||||
node *deleteNode(node *root, int key) {
|
||||
if (root == NULL)
|
||||
return root;
|
||||
if (key < root->data)
|
||||
@@ -107,18 +96,14 @@ node *deleteNode(node *root, int key)
|
||||
else if (key > root->data)
|
||||
root->right = deleteNode(root->right, key);
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Node to be deleted is leaf node or have only one Child
|
||||
if (!root->right)
|
||||
{
|
||||
if (!root->right) {
|
||||
node *temp = root->left;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
return temp;
|
||||
}
|
||||
else if (!root->left)
|
||||
{
|
||||
} else if (!root->left) {
|
||||
node *temp = root->right;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
@@ -135,12 +120,10 @@ node *deleteNode(node *root, int key)
|
||||
|
||||
// LevelOrder (Breadth First Search)
|
||||
|
||||
void levelOrder(node *root)
|
||||
{
|
||||
void levelOrder(node *root) {
|
||||
queue<node *> q;
|
||||
q.push(root);
|
||||
while (!q.empty())
|
||||
{
|
||||
while (!q.empty()) {
|
||||
root = q.front();
|
||||
cout << root->data << " ";
|
||||
q.pop();
|
||||
@@ -151,8 +134,7 @@ void levelOrder(node *root)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// Testing AVL Tree
|
||||
node *root = NULL;
|
||||
int i;
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
struct queue
|
||||
{
|
||||
struct queue {
|
||||
node *t[100];
|
||||
int front;
|
||||
int rear;
|
||||
@@ -21,107 +19,71 @@ void enqueue(node *n) { q.t[q.rear++] = n; }
|
||||
|
||||
node *dequeue() { return (q.t[q.front++]); }
|
||||
|
||||
void Insert(node *n, int x)
|
||||
{
|
||||
if (x < n->val)
|
||||
{
|
||||
if (n->left == NULL)
|
||||
{
|
||||
void Insert(node *n, int x) {
|
||||
if (x < n->val) {
|
||||
if (n->left == NULL) {
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Insert(n->left, x);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->right == NULL)
|
||||
{
|
||||
} else {
|
||||
if (n->right == NULL) {
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Insert(n->right, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int findMaxInLeftST(node *n)
|
||||
{
|
||||
while (n->right != NULL)
|
||||
{
|
||||
int findMaxInLeftST(node *n) {
|
||||
while (n->right != NULL) {
|
||||
n = n->right;
|
||||
}
|
||||
return n->val;
|
||||
}
|
||||
|
||||
void Remove(node *p, node *n, int x)
|
||||
{
|
||||
if (n->val == x)
|
||||
{
|
||||
if (n->right == NULL && n->left == NULL)
|
||||
{
|
||||
if (x < p->val)
|
||||
{
|
||||
void Remove(node *p, node *n, int x) {
|
||||
if (n->val == x) {
|
||||
if (n->right == NULL && n->left == NULL) {
|
||||
if (x < p->val) {
|
||||
p->right = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
p->left = NULL;
|
||||
}
|
||||
}
|
||||
else if (n->right == NULL)
|
||||
{
|
||||
if (x < p->val)
|
||||
{
|
||||
} else if (n->right == NULL) {
|
||||
if (x < p->val) {
|
||||
p->right = n->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
p->left = n->left;
|
||||
}
|
||||
}
|
||||
else if (n->left == NULL)
|
||||
{
|
||||
if (x < p->val)
|
||||
{
|
||||
} else if (n->left == NULL) {
|
||||
if (x < p->val) {
|
||||
p->right = n->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
p->left = n->right;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int y = findMaxInLeftST(n->left);
|
||||
n->val = y;
|
||||
Remove(n, n->right, y);
|
||||
}
|
||||
}
|
||||
else if (x < n->val)
|
||||
{
|
||||
} else if (x < n->val) {
|
||||
Remove(n, n->left, x);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Remove(n, n->right, x);
|
||||
}
|
||||
}
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void BFT(node *n) {
|
||||
if (n != NULL) {
|
||||
cout << n->val << " ";
|
||||
enqueue(n->left);
|
||||
enqueue(n->right);
|
||||
@@ -129,38 +91,31 @@ void BFT(node *n)
|
||||
}
|
||||
}
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void Pre(node *n) {
|
||||
if (n != NULL) {
|
||||
cout << n->val << " ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void In(node *n) {
|
||||
if (n != NULL) {
|
||||
In(n->left);
|
||||
cout << n->val << " ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void Post(node *n) {
|
||||
if (n != NULL) {
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout << n->val << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
q.front = 0;
|
||||
q.rear = 0;
|
||||
int value;
|
||||
@@ -171,8 +126,7 @@ int main()
|
||||
root->val = value;
|
||||
root->left = NULL;
|
||||
root->right = NULL;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Insert";
|
||||
cout << "\n2. Delete";
|
||||
cout << "\n3. Breadth First";
|
||||
@@ -183,8 +137,7 @@ int main()
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
int x;
|
||||
switch (ch)
|
||||
{
|
||||
switch (ch) {
|
||||
case 1:
|
||||
cout << "\nEnter the value to be Inserted : ";
|
||||
cin >> x;
|
||||
|
||||
@@ -7,8 +7,7 @@ using namespace std;
|
||||
void swap(int *x, int *y);
|
||||
|
||||
// A class for Min Heap
|
||||
class MinHeap
|
||||
{
|
||||
class MinHeap {
|
||||
int *harr; // pointer to array of elements in heap
|
||||
int capacity; // maximum possible size of min heap
|
||||
int heap_size; // Current number of elements in min heap
|
||||
@@ -44,18 +43,15 @@ class MinHeap
|
||||
};
|
||||
|
||||
// Constructor: Builds a heap from a given array a[] of given size
|
||||
MinHeap::MinHeap(int cap)
|
||||
{
|
||||
MinHeap::MinHeap(int cap) {
|
||||
heap_size = 0;
|
||||
capacity = cap;
|
||||
harr = new int[cap];
|
||||
}
|
||||
|
||||
// Inserts a new key 'k'
|
||||
void MinHeap::insertKey(int k)
|
||||
{
|
||||
if (heap_size == capacity)
|
||||
{
|
||||
void MinHeap::insertKey(int k) {
|
||||
if (heap_size == capacity) {
|
||||
cout << "\nOverflow: Could not insertKey\n";
|
||||
return;
|
||||
}
|
||||
@@ -66,8 +62,7 @@ void MinHeap::insertKey(int k)
|
||||
harr[i] = k;
|
||||
|
||||
// Fix the min heap property if it is violated
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
while (i != 0 && harr[parent(i)] > harr[i]) {
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
@@ -75,23 +70,19 @@ void MinHeap::insertKey(int k)
|
||||
|
||||
// Decreases value of key at index 'i' to new_val. It is assumed that
|
||||
// new_val is smaller than harr[i].
|
||||
void MinHeap::decreaseKey(int i, int new_val)
|
||||
{
|
||||
void MinHeap::decreaseKey(int i, int new_val) {
|
||||
harr[i] = new_val;
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
while (i != 0 && harr[parent(i)] > harr[i]) {
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Method to remove minimum element (or root) from min heap
|
||||
int MinHeap::extractMin()
|
||||
{
|
||||
int MinHeap::extractMin() {
|
||||
if (heap_size <= 0)
|
||||
return INT_MAX;
|
||||
if (heap_size == 1)
|
||||
{
|
||||
if (heap_size == 1) {
|
||||
heap_size--;
|
||||
return harr[0];
|
||||
}
|
||||
@@ -107,16 +98,14 @@ int MinHeap::extractMin()
|
||||
|
||||
// This function deletes key at index i. It first reduced value to minus
|
||||
// infinite, then calls extractMin()
|
||||
void MinHeap::deleteKey(int i)
|
||||
{
|
||||
void MinHeap::deleteKey(int i) {
|
||||
decreaseKey(i, INT_MIN);
|
||||
extractMin();
|
||||
}
|
||||
|
||||
// A recursive method to heapify a subtree with the root at given index
|
||||
// This method assumes that the subtrees are already heapified
|
||||
void MinHeap::MinHeapify(int i)
|
||||
{
|
||||
void MinHeap::MinHeapify(int i) {
|
||||
int l = left(i);
|
||||
int r = right(i);
|
||||
int smallest = i;
|
||||
@@ -124,24 +113,21 @@ void MinHeap::MinHeapify(int i)
|
||||
smallest = l;
|
||||
if (r < heap_size && harr[r] < harr[smallest])
|
||||
smallest = r;
|
||||
if (smallest != i)
|
||||
{
|
||||
if (smallest != i) {
|
||||
swap(&harr[i], &harr[smallest]);
|
||||
MinHeapify(smallest);
|
||||
}
|
||||
}
|
||||
|
||||
// A utility function to swap two elements
|
||||
void swap(int *x, int *y)
|
||||
{
|
||||
void swap(int *x, int *y) {
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
MinHeap h(11);
|
||||
h.insertKey(3);
|
||||
h.insertKey(2);
|
||||
|
||||
@@ -1,24 +1,20 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
class Queue
|
||||
{
|
||||
class Queue {
|
||||
node *front;
|
||||
node *rear;
|
||||
|
||||
public:
|
||||
Queue()
|
||||
{
|
||||
Queue() {
|
||||
front = NULL;
|
||||
rear = NULL;
|
||||
}
|
||||
void createNode(int val)
|
||||
{
|
||||
void createNode(int val) {
|
||||
node *ptr;
|
||||
node *nn;
|
||||
nn = new node;
|
||||
@@ -28,14 +24,10 @@ class Queue
|
||||
front = nn;
|
||||
rear = nn;
|
||||
}
|
||||
void enqueue(int val)
|
||||
{
|
||||
if (front == NULL || rear == NULL)
|
||||
{
|
||||
void enqueue(int val) {
|
||||
if (front == NULL || rear == NULL) {
|
||||
createNode(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *ptr;
|
||||
node *nn;
|
||||
ptr = front;
|
||||
@@ -46,19 +38,16 @@ class Queue
|
||||
rear = nn;
|
||||
}
|
||||
}
|
||||
void dequeue()
|
||||
{
|
||||
void dequeue() {
|
||||
node *n;
|
||||
n = front;
|
||||
front = front->next;
|
||||
delete (n);
|
||||
}
|
||||
void traverse()
|
||||
{
|
||||
void traverse() {
|
||||
node *ptr;
|
||||
ptr = front;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << ptr->data << " ";
|
||||
ptr = ptr->next;
|
||||
} while (ptr != rear->next);
|
||||
@@ -66,8 +55,7 @@ class Queue
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
Queue q;
|
||||
q.enqueue(10);
|
||||
q.enqueue(20);
|
||||
|
||||
@@ -5,25 +5,22 @@
|
||||
using namespace std;
|
||||
|
||||
/* Constructor */
|
||||
cll::cll()
|
||||
{
|
||||
cll::cll() {
|
||||
head = NULL;
|
||||
total = 0;
|
||||
}
|
||||
|
||||
cll::~cll() { /* Desstructure, no need to fill */ }
|
||||
cll::~cll() { /* Desstructure, no need to fill */
|
||||
}
|
||||
|
||||
/* Display a list. and total element */
|
||||
void cll::display()
|
||||
{
|
||||
void cll::display() {
|
||||
if (head == NULL)
|
||||
cout << "List is empty !" << endl;
|
||||
else
|
||||
{
|
||||
else {
|
||||
cout << "CLL list: ";
|
||||
node *current = head;
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
for (int i = 0; i < total; i++) {
|
||||
cout << current->data << " -> ";
|
||||
current = current->next;
|
||||
}
|
||||
@@ -33,22 +30,17 @@ void cll::display()
|
||||
}
|
||||
|
||||
/* List insert a new value at head in list */
|
||||
void cll::insert_front(int new_data)
|
||||
{
|
||||
void cll::insert_front(int new_data) {
|
||||
node *newNode;
|
||||
newNode = new node;
|
||||
newNode->data = new_data;
|
||||
newNode->next = NULL;
|
||||
if (head == NULL)
|
||||
{
|
||||
if (head == NULL) {
|
||||
head = newNode;
|
||||
head->next = head;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *current = head;
|
||||
while (current->next != head)
|
||||
{
|
||||
while (current->next != head) {
|
||||
current = current->next;
|
||||
}
|
||||
newNode->next = head;
|
||||
@@ -59,22 +51,17 @@ void cll::insert_front(int new_data)
|
||||
}
|
||||
|
||||
/* List insert a new value at head in list */
|
||||
void cll::insert_tail(int new_data)
|
||||
{
|
||||
void cll::insert_tail(int new_data) {
|
||||
node *newNode;
|
||||
newNode = new node;
|
||||
newNode->data = new_data;
|
||||
newNode->next = NULL;
|
||||
if (head == NULL)
|
||||
{
|
||||
if (head == NULL) {
|
||||
head = newNode;
|
||||
head->next = head;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *current = head;
|
||||
while (current->next != head)
|
||||
{
|
||||
while (current->next != head) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = newNode;
|
||||
@@ -88,18 +75,13 @@ int cll::get_size() { return total; }
|
||||
|
||||
/* Return true if the requested item (sent in as an argument)
|
||||
is in the list, otherwise return false */
|
||||
bool cll::find_item(int item_to_find)
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
bool cll::find_item(int item_to_find) {
|
||||
if (head == NULL) {
|
||||
cout << "List is empty !" << endl;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *current = head;
|
||||
while (current->next != head)
|
||||
{
|
||||
while (current->next != head) {
|
||||
if (current->data == item_to_find)
|
||||
return true;
|
||||
current = current->next;
|
||||
@@ -113,17 +95,12 @@ int cll::operator*() { return head->data; }
|
||||
|
||||
/* Overload the pre-increment operator.
|
||||
The iterator is advanced to the next node. */
|
||||
void cll::operator++()
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
void cll::operator++() {
|
||||
if (head == NULL) {
|
||||
cout << "List is empty !" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *current = head;
|
||||
while (current->next != head)
|
||||
{
|
||||
while (current->next != head) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = head->next;
|
||||
|
||||
@@ -9,14 +9,12 @@
|
||||
#ifndef CLL_H
|
||||
#define CLL_H
|
||||
/*The data structure is a linear linked list of integers */
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int data;
|
||||
node* next;
|
||||
};
|
||||
|
||||
class cll
|
||||
{
|
||||
class cll {
|
||||
public:
|
||||
cll(); /* Construct without parameter */
|
||||
~cll();
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include "cll.h"
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
/* Test CLL */
|
||||
cout << "----------- Test construct -----------" << endl;
|
||||
cll list1;
|
||||
|
||||
@@ -7,20 +7,16 @@ using std::vector;
|
||||
|
||||
vector<int> root, rnk;
|
||||
|
||||
void CreateSet(int n)
|
||||
{
|
||||
void CreateSet(int n) {
|
||||
root = vector<int>(n + 1);
|
||||
rnk = vector<int>(n + 1, 1);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
root[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int Find(int x)
|
||||
{
|
||||
if (root[x] == x)
|
||||
{
|
||||
int Find(int x) {
|
||||
if (root[x] == x) {
|
||||
return x;
|
||||
}
|
||||
return root[x] = Find(root[x]);
|
||||
@@ -28,50 +24,38 @@ int Find(int x)
|
||||
|
||||
bool InSameUnion(int x, int y) { return Find(x) == Find(y); }
|
||||
|
||||
void Union(int x, int y)
|
||||
{
|
||||
void Union(int x, int y) {
|
||||
int a = Find(x), b = Find(y);
|
||||
if (a != b)
|
||||
{
|
||||
if (rnk[a] < rnk[b])
|
||||
{
|
||||
if (a != b) {
|
||||
if (rnk[a] < rnk[b]) {
|
||||
root[a] = b;
|
||||
}
|
||||
else if (rnk[a] > rnk[b])
|
||||
{
|
||||
} else if (rnk[a] > rnk[b]) {
|
||||
root[b] = a;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
root[a] = b;
|
||||
++rnk[b];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// tests CreateSet & Find
|
||||
int n = 100;
|
||||
CreateSet(n);
|
||||
for (int i = 1; i <= 100; ++i)
|
||||
{
|
||||
if (root[i] != i)
|
||||
{
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
if (root[i] != i) {
|
||||
cout << "Fail" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tests InSameUnion & Union
|
||||
cout << "1 and 2 are initially not in the same subset" << endl;
|
||||
if (InSameUnion(1, 2))
|
||||
{
|
||||
if (InSameUnion(1, 2)) {
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
Union(1, 2);
|
||||
cout << "1 and 2 are now in the same subset" << endl;
|
||||
if (!InSameUnion(1, 2))
|
||||
{
|
||||
if (!InSameUnion(1, 2)) {
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -2,15 +2,13 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *prev;
|
||||
node *next;
|
||||
} * start;
|
||||
|
||||
class double_linked_list
|
||||
{
|
||||
class double_linked_list {
|
||||
public:
|
||||
double_linked_list() { start = NULL; }
|
||||
void insert(int x);
|
||||
@@ -20,13 +18,10 @@ class double_linked_list
|
||||
void reverseShow();
|
||||
};
|
||||
|
||||
void double_linked_list::insert(int x)
|
||||
{
|
||||
void double_linked_list::insert(int x) {
|
||||
node *t = start;
|
||||
if (start != NULL)
|
||||
{
|
||||
while (t->next != NULL)
|
||||
{
|
||||
if (start != NULL) {
|
||||
while (t->next != NULL) {
|
||||
t = t->next;
|
||||
}
|
||||
node *n = new node;
|
||||
@@ -34,9 +29,7 @@ void double_linked_list::insert(int x)
|
||||
n->prev = t;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->prev = NULL;
|
||||
@@ -45,91 +38,69 @@ void double_linked_list::insert(int x)
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::remove(int x)
|
||||
{
|
||||
void double_linked_list::remove(int x) {
|
||||
node *t = start;
|
||||
while (t != NULL && t->val != x)
|
||||
{
|
||||
while (t != NULL && t->val != x) {
|
||||
t = t->next;
|
||||
}
|
||||
if (t == NULL)
|
||||
{
|
||||
if (t == NULL) {
|
||||
return;
|
||||
}
|
||||
if (t->prev == NULL)
|
||||
{
|
||||
if (t->next == NULL)
|
||||
{
|
||||
if (t->prev == NULL) {
|
||||
if (t->next == NULL) {
|
||||
start = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
start = t->next;
|
||||
start->prev = NULL;
|
||||
}
|
||||
}
|
||||
else if (t->next == NULL)
|
||||
{
|
||||
} else if (t->next == NULL) {
|
||||
t->prev->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
t->prev->next = t->next;
|
||||
t->next->prev = t->prev;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
void double_linked_list::search(int x)
|
||||
{
|
||||
void double_linked_list::search(int x) {
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if (t->val == x)
|
||||
{
|
||||
while (t != NULL) {
|
||||
if (t->val == x) {
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
if (found == 0)
|
||||
{
|
||||
if (found == 0) {
|
||||
std::cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::show()
|
||||
{
|
||||
void double_linked_list::show() {
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::reverseShow()
|
||||
{
|
||||
void double_linked_list::reverseShow() {
|
||||
node *t = start;
|
||||
while (t != NULL && t->next != NULL)
|
||||
{
|
||||
while (t != NULL && t->next != NULL) {
|
||||
t = t->next;
|
||||
}
|
||||
while (t != NULL)
|
||||
{
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int choice, x;
|
||||
double_linked_list ob;
|
||||
do
|
||||
{
|
||||
do {
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
@@ -137,8 +108,7 @@ int main()
|
||||
std::cout << "\n5. Reverse print";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
switch (choice) {
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
|
||||
@@ -1,42 +1,32 @@
|
||||
#include <iostream>
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
void insert(int x) {
|
||||
node *t = start;
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
if (start != NULL)
|
||||
{
|
||||
while (t->next != NULL)
|
||||
{
|
||||
if (start != NULL) {
|
||||
while (t->next != NULL) {
|
||||
t = t->next;
|
||||
}
|
||||
t->next = n;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
start = n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
void remove(int x) {
|
||||
if (start == NULL) {
|
||||
std::cout << "\nLinked List is empty\n";
|
||||
return;
|
||||
}
|
||||
else if (start->val == x)
|
||||
{
|
||||
} else if (start->val == x) {
|
||||
node *temp = start;
|
||||
start = start->next;
|
||||
delete temp;
|
||||
@@ -45,14 +35,12 @@ void remove(int x)
|
||||
|
||||
node *temp = start, *parent = start;
|
||||
|
||||
while (temp != NULL && temp->val != x)
|
||||
{
|
||||
while (temp != NULL && temp->val != x) {
|
||||
parent = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (temp == NULL)
|
||||
{
|
||||
if (temp == NULL) {
|
||||
std::cout << std::endl << x << " not found in list\n";
|
||||
return;
|
||||
}
|
||||
@@ -61,44 +49,35 @@ void remove(int x)
|
||||
delete temp;
|
||||
}
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
void search(int x) {
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if (t->val == x)
|
||||
{
|
||||
while (t != NULL) {
|
||||
if (t->val == x) {
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
if (found == 0)
|
||||
{
|
||||
if (found == 0) {
|
||||
std::cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
void show() {
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void reverse()
|
||||
{
|
||||
void reverse() {
|
||||
node *first = start;
|
||||
if (first != NULL)
|
||||
{
|
||||
if (first != NULL) {
|
||||
node *second = first->next;
|
||||
while (second != NULL)
|
||||
{
|
||||
while (second != NULL) {
|
||||
node *tem = second->next;
|
||||
second->next = first;
|
||||
first = second;
|
||||
@@ -106,18 +85,14 @@ void reverse()
|
||||
}
|
||||
start->next = NULL;
|
||||
start = first;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "\nEmpty list";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
@@ -126,8 +101,7 @@ int main()
|
||||
std::cout << "\n0. Exit";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
switch (choice) {
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
|
||||
@@ -7,18 +7,15 @@
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct Node
|
||||
{
|
||||
struct Node {
|
||||
int data;
|
||||
int next;
|
||||
};
|
||||
Node AvailArray[100]; // array that will act as nodes of a linked list.
|
||||
int head = -1;
|
||||
int avail = 0;
|
||||
void initialise_list()
|
||||
{
|
||||
for (int i = 0; i <= 98; i++)
|
||||
{
|
||||
void initialise_list() {
|
||||
for (int i = 0; i <= 98; i++) {
|
||||
AvailArray[i].next = i + 1;
|
||||
}
|
||||
AvailArray[99].next = -1; // indicating the end of the linked list.
|
||||
@@ -50,12 +47,10 @@ void insertAtTheBeginning(int data) // The function will insert the given data
|
||||
head = newNode;
|
||||
}
|
||||
|
||||
void insertAtTheEnd(int data)
|
||||
{
|
||||
void insertAtTheEnd(int data) {
|
||||
int newNode = getnode();
|
||||
int temp = head;
|
||||
while (AvailArray[temp].next != -1)
|
||||
{
|
||||
while (AvailArray[temp].next != -1) {
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
// temp is now pointing to the end node.
|
||||
@@ -64,11 +59,9 @@ void insertAtTheEnd(int data)
|
||||
AvailArray[temp].next = newNode;
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
void display() {
|
||||
int temp = head;
|
||||
while (temp != -1)
|
||||
{
|
||||
while (temp != -1) {
|
||||
cout << AvailArray[temp].data << "->";
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
@@ -76,20 +69,17 @@ void display()
|
||||
;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
initialise_list();
|
||||
int x, y, z;
|
||||
for (;;)
|
||||
{
|
||||
for (;;) {
|
||||
cout << "1. Insert At The Beginning" << endl;
|
||||
cout << "2. Insert At The End" << endl;
|
||||
cout << "3. Display" << endl;
|
||||
cout << "4.Exit" << endl;
|
||||
cout << "Enter Your choice" << endl;
|
||||
cin >> z;
|
||||
switch (z)
|
||||
{
|
||||
switch (z) {
|
||||
case 1:
|
||||
cout << "Enter the number you want to enter" << endl;
|
||||
cin >> x;
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct list
|
||||
{
|
||||
struct list {
|
||||
int data[50];
|
||||
int top = 0;
|
||||
bool isSorted = false;
|
||||
|
||||
int BinarySearch(int *array, int first, int last, int x)
|
||||
{
|
||||
if (last < first)
|
||||
{
|
||||
int BinarySearch(int *array, int first, int last, int x) {
|
||||
if (last < first) {
|
||||
return -1;
|
||||
}
|
||||
int mid = (first + last) / 2;
|
||||
@@ -22,12 +19,9 @@ struct list
|
||||
return (BinarySearch(array, mid + 1, last, x));
|
||||
}
|
||||
|
||||
int LinarSearch(int *array, int x)
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
if (array[i] == x)
|
||||
{
|
||||
int LinarSearch(int *array, int x) {
|
||||
for (int i = 0; i < top; i++) {
|
||||
if (array[i] == x) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -35,41 +29,31 @@ struct list
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Search(int x)
|
||||
{
|
||||
int Search(int x) {
|
||||
int pos = -1;
|
||||
|
||||
if (isSorted)
|
||||
{
|
||||
if (isSorted) {
|
||||
pos = BinarySearch(data, 0, top - 1, x);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
pos = LinarSearch(data, x);
|
||||
}
|
||||
|
||||
if (pos != -1)
|
||||
{
|
||||
if (pos != -1) {
|
||||
cout << "\nElement found at position : " << pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "\nElement not found";
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Sort()
|
||||
{
|
||||
void Sort() {
|
||||
int i, j, pos;
|
||||
for (i = 0; i < top; i++)
|
||||
{
|
||||
for (i = 0; i < top; i++) {
|
||||
int min = data[i];
|
||||
for (j = i + 1; j < top; j++)
|
||||
{
|
||||
if (data[j] < min)
|
||||
{
|
||||
for (j = i + 1; j < top; j++) {
|
||||
if (data[j] < min) {
|
||||
pos = j;
|
||||
min = data[pos];
|
||||
}
|
||||
@@ -82,40 +66,30 @@ struct list
|
||||
isSorted = true;
|
||||
}
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
if (!isSorted)
|
||||
{
|
||||
if (top == 49)
|
||||
{
|
||||
void insert(int x) {
|
||||
if (!isSorted) {
|
||||
if (top == 49) {
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
data[top] = x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < top - 1; i++)
|
||||
{
|
||||
if (data[i] <= x && x <= data[i + 1])
|
||||
{
|
||||
for (int i = 0; i < top - 1; i++) {
|
||||
if (data[i] <= x && x <= data[i + 1]) {
|
||||
pos = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos == 0)
|
||||
{
|
||||
if (pos == 0) {
|
||||
pos = top - 1;
|
||||
}
|
||||
|
||||
for (int i = top; i > pos; i--)
|
||||
{
|
||||
for (int i = top; i > pos; i--) {
|
||||
data[i] = data[i - 1];
|
||||
}
|
||||
top++;
|
||||
@@ -123,33 +97,27 @@ struct list
|
||||
}
|
||||
}
|
||||
|
||||
void Remove(int x)
|
||||
{
|
||||
void Remove(int x) {
|
||||
int pos = Search(x);
|
||||
cout << "\n" << data[pos] << " deleted";
|
||||
for (int i = pos; i < top; i++)
|
||||
{
|
||||
for (int i = pos; i < top; i++) {
|
||||
data[i] = data[i + 1];
|
||||
}
|
||||
top--;
|
||||
}
|
||||
|
||||
void Show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
void Show() {
|
||||
for (int i = 0; i < top; i++) {
|
||||
cout << data[i] << "\t";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
list L;
|
||||
int choice;
|
||||
int x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1.Insert";
|
||||
cout << "\n2.Delete";
|
||||
cout << "\n3.Search";
|
||||
@@ -157,8 +125,7 @@ int main()
|
||||
cout << "\n5.Print";
|
||||
cout << "\n\nEnter Your Choice : ";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
switch (choice) {
|
||||
case 1:
|
||||
cout << "\nEnter the element to be inserted : ";
|
||||
cin >> x;
|
||||
|
||||
@@ -7,39 +7,32 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Btree
|
||||
{
|
||||
struct Btree {
|
||||
int data;
|
||||
struct Btree *left; // Pointer to left subtree
|
||||
struct Btree *right; // Pointer to right subtree
|
||||
};
|
||||
|
||||
void insert(Btree **root, int d)
|
||||
{
|
||||
void insert(Btree **root, int d) {
|
||||
Btree *nn = new Btree(); // Creating new node
|
||||
nn->data = d;
|
||||
nn->left = NULL;
|
||||
nn->right = NULL;
|
||||
if (*root == NULL)
|
||||
{
|
||||
if (*root == NULL) {
|
||||
*root = nn;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
queue<Btree *> q;
|
||||
// Adding root node to queue
|
||||
q.push(*root);
|
||||
while (!q.empty())
|
||||
{
|
||||
while (!q.empty()) {
|
||||
Btree *node = q.front();
|
||||
// Removing parent node from queue
|
||||
q.pop();
|
||||
if (node->left)
|
||||
// Adding left child of removed node to queue
|
||||
q.push(node->left);
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Adding new node if no left child is present
|
||||
node->left = nn;
|
||||
return;
|
||||
@@ -47,8 +40,7 @@ void insert(Btree **root, int d)
|
||||
if (node->right)
|
||||
// Adding right child of removed node to queue
|
||||
q.push(node->right);
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Adding new node if no right child is present
|
||||
node->right = nn;
|
||||
return;
|
||||
@@ -57,36 +49,29 @@ void insert(Btree **root, int d)
|
||||
}
|
||||
}
|
||||
|
||||
void morrisInorder(Btree *root)
|
||||
{
|
||||
void morrisInorder(Btree *root) {
|
||||
Btree *curr = root;
|
||||
Btree *temp;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->left == NULL)
|
||||
{
|
||||
while (curr) {
|
||||
if (curr->left == NULL) {
|
||||
cout << curr->data << " ";
|
||||
// If left of current node is NULL then curr is shifted to right
|
||||
curr = curr->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Left of current node is stored in temp
|
||||
temp = curr->left;
|
||||
// Moving to extreme right of temp
|
||||
while (temp->right && temp->right != curr) temp = temp->right;
|
||||
// If extreme right is null it is made to point to currrent node
|
||||
// (will be used for backtracking)
|
||||
if (temp->right == NULL)
|
||||
{
|
||||
if (temp->right == NULL) {
|
||||
temp->right = curr;
|
||||
// current node is made to point its left subtree
|
||||
curr = curr->left;
|
||||
}
|
||||
// If extreme right already points to currrent node it it set to
|
||||
// null
|
||||
else if (temp->right == curr)
|
||||
{
|
||||
else if (temp->right == curr) {
|
||||
cout << curr->data << " ";
|
||||
temp->right = NULL;
|
||||
// current node is made to point its right subtree
|
||||
@@ -96,8 +81,7 @@ void morrisInorder(Btree *root)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// Testing morrisInorder funtion
|
||||
Btree *root = NULL;
|
||||
int i;
|
||||
|
||||
@@ -6,8 +6,7 @@ using namespace std;
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Kind>
|
||||
queue<Kind>::queue()
|
||||
{
|
||||
queue<Kind>::queue() {
|
||||
queueFront = NULL;
|
||||
queueRear = NULL;
|
||||
size = 0;
|
||||
@@ -15,18 +14,14 @@ queue<Kind>::queue()
|
||||
|
||||
/* Destructor */
|
||||
template <class Kind>
|
||||
queue<Kind>::~queue()
|
||||
{
|
||||
}
|
||||
queue<Kind>::~queue() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Kind>
|
||||
void queue<Kind>::display()
|
||||
{
|
||||
void queue<Kind>::display() {
|
||||
node<Kind> *current = queueFront;
|
||||
cout << "Front --> ";
|
||||
while (current != NULL)
|
||||
{
|
||||
while (current != NULL) {
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
@@ -36,33 +31,27 @@ void queue<Kind>::display()
|
||||
|
||||
/* Determine whether the queue is empty */
|
||||
template <class Kind>
|
||||
bool queue<Kind>::isEmptyQueue()
|
||||
{
|
||||
bool queue<Kind>::isEmptyQueue() {
|
||||
return (queueFront == NULL);
|
||||
}
|
||||
|
||||
/* Clear queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::clear()
|
||||
{
|
||||
void queue<Kind>::clear() {
|
||||
queueFront = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::enQueue(Kind item)
|
||||
{
|
||||
void queue<Kind>::enQueue(Kind item) {
|
||||
node<Kind> *newNode;
|
||||
newNode = new node<Kind>;
|
||||
newNode->data = item;
|
||||
newNode->next = NULL;
|
||||
if (queueFront == NULL)
|
||||
{
|
||||
if (queueFront == NULL) {
|
||||
queueFront = newNode;
|
||||
queueRear = newNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
queueRear->next = newNode;
|
||||
queueRear = queueRear->next;
|
||||
}
|
||||
@@ -71,26 +60,21 @@ void queue<Kind>::enQueue(Kind item)
|
||||
|
||||
/* Return the top element of the queue */
|
||||
template <class Kind>
|
||||
Kind queue<Kind>::front()
|
||||
{
|
||||
Kind queue<Kind>::front() {
|
||||
assert(queueFront != NULL);
|
||||
return queueFront->data;
|
||||
}
|
||||
|
||||
/* Remove the element of the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::deQueue()
|
||||
{
|
||||
void queue<Kind>::deQueue() {
|
||||
node<Kind> *temp;
|
||||
if (!isEmptyQueue())
|
||||
{
|
||||
if (!isEmptyQueue()) {
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "Queue is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,16 +4,14 @@
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Kind>
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
Kind data;
|
||||
node<Kind> *next;
|
||||
};
|
||||
|
||||
/* Definition of the queue class */
|
||||
template <class Kind>
|
||||
class queue
|
||||
{
|
||||
class queue {
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
queue<string> q;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
|
||||
@@ -10,15 +10,13 @@
|
||||
|
||||
#define MAXSIZE 10
|
||||
|
||||
class Queue_Array
|
||||
{
|
||||
class Queue_Array {
|
||||
int front;
|
||||
int rear;
|
||||
int size;
|
||||
|
||||
public:
|
||||
Queue_Array()
|
||||
{
|
||||
Queue_Array() {
|
||||
front = -1;
|
||||
rear = -1;
|
||||
size = MAXSIZE;
|
||||
@@ -29,59 +27,42 @@ class Queue_Array
|
||||
void display();
|
||||
};
|
||||
|
||||
void Queue_Array::enqueue(int ele)
|
||||
{
|
||||
if (rear == size - 1)
|
||||
{
|
||||
void Queue_Array::enqueue(int ele) {
|
||||
if (rear == size - 1) {
|
||||
std::cout << "\nStack is full";
|
||||
}
|
||||
else if (front == -1 && rear == -1)
|
||||
{
|
||||
} else if (front == -1 && rear == -1) {
|
||||
front = rear = 0;
|
||||
arr[rear] = ele;
|
||||
}
|
||||
else if (rear < size)
|
||||
{
|
||||
} else if (rear < size) {
|
||||
rear++;
|
||||
arr[rear] = ele;
|
||||
}
|
||||
}
|
||||
|
||||
int Queue_Array::dequeue()
|
||||
{
|
||||
int Queue_Array::dequeue() {
|
||||
int d;
|
||||
if (front == -1)
|
||||
{
|
||||
if (front == -1) {
|
||||
std::cout << "\nstack is empty ";
|
||||
return 0;
|
||||
}
|
||||
else if (front == rear)
|
||||
{
|
||||
} else if (front == rear) {
|
||||
d = arr[front];
|
||||
front = rear = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
d = arr[front++];
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
void Queue_Array::display()
|
||||
{
|
||||
if (front == -1)
|
||||
{
|
||||
void Queue_Array::display() {
|
||||
if (front == -1) {
|
||||
std::cout << "\nStack is empty";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
for (int i = front; i <= rear; i++) std::cout << arr[i] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int op, data;
|
||||
|
||||
Queue_Array ob;
|
||||
@@ -90,31 +71,21 @@ int main()
|
||||
std::cout << "\n2. dequeue(Deletion)";
|
||||
std::cout << "\n3. Display";
|
||||
std::cout << "\n4. Exit";
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
std::cout << "\nEnter your choice ";
|
||||
std::cin >> op;
|
||||
if (op == 1)
|
||||
{
|
||||
if (op == 1) {
|
||||
std::cout << "Enter data ";
|
||||
std::cin >> data;
|
||||
ob.enqueue(data);
|
||||
}
|
||||
else if (op == 2)
|
||||
{
|
||||
} else if (op == 2) {
|
||||
data = ob.dequeue();
|
||||
std::cout << "\ndequeue element is:\t" << data;
|
||||
}
|
||||
else if (op == 3)
|
||||
{
|
||||
} else if (op == 3) {
|
||||
ob.display();
|
||||
}
|
||||
else if (op == 4)
|
||||
{
|
||||
} else if (op == 4) {
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "\nWrong choice ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,30 +5,22 @@ int queue[10];
|
||||
int front = 0;
|
||||
int rear = 0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if (rear == 10)
|
||||
{
|
||||
void Enque(int x) {
|
||||
if (rear == 10) {
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
queue[rear++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front == rear)
|
||||
{
|
||||
void Deque() {
|
||||
if (front == rear) {
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
cout << "\n" << queue[front++] << " deleted";
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
for (int i = front; i < rear; i++) {
|
||||
queue[i - front] = queue[i];
|
||||
}
|
||||
rear = rear - front;
|
||||
@@ -36,36 +28,27 @@ void Deque()
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
void show() {
|
||||
for (int i = front; i < rear; i++) {
|
||||
cout << queue[i] << "\t";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Enque";
|
||||
cout << "\n2. Deque";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
if (ch == 1) {
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch == 2)
|
||||
{
|
||||
} else if (ch == 2) {
|
||||
Deque();
|
||||
}
|
||||
else if (ch == 3)
|
||||
{
|
||||
} else if (ch == 3) {
|
||||
show();
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *front, *rear;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if (rear == NULL)
|
||||
{
|
||||
void Enque(int x) {
|
||||
if (rear == NULL) {
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
@@ -20,8 +17,7 @@ void Enque(int x)
|
||||
front = n;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
@@ -30,14 +26,10 @@ void Enque(int x)
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (rear == NULL && front == NULL)
|
||||
{
|
||||
void Deque() {
|
||||
if (rear == NULL && front == NULL) {
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *t = front;
|
||||
cout << "\n" << t->val << " deleted";
|
||||
front = front->next;
|
||||
@@ -47,38 +39,29 @@ void Deque()
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
void show() {
|
||||
node *t = front;
|
||||
while (t != NULL)
|
||||
{
|
||||
while (t != NULL) {
|
||||
cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Enque";
|
||||
cout << "\n2. Deque";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
if (ch == 1) {
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch == 2)
|
||||
{
|
||||
} else if (ch == 2) {
|
||||
Deque();
|
||||
}
|
||||
else if (ch == 3)
|
||||
{
|
||||
} else if (ch == 3) {
|
||||
show();
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
@@ -3,13 +3,11 @@
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
struct linkedlist
|
||||
{
|
||||
struct linkedlist {
|
||||
int data;
|
||||
linkedlist *next;
|
||||
};
|
||||
class stack_linkedList
|
||||
{
|
||||
class stack_linkedList {
|
||||
public:
|
||||
linkedlist *front;
|
||||
linkedlist *rear;
|
||||
@@ -19,28 +17,24 @@ class stack_linkedList
|
||||
int dequeue();
|
||||
void display();
|
||||
};
|
||||
void stack_linkedList::enqueue(int ele)
|
||||
{
|
||||
void stack_linkedList::enqueue(int ele) {
|
||||
linkedlist *temp = new linkedlist();
|
||||
temp->data = ele;
|
||||
temp->next = NULL;
|
||||
|
||||
if (front == NULL)
|
||||
front = rear = temp;
|
||||
else
|
||||
{
|
||||
else {
|
||||
rear->next = temp;
|
||||
rear = temp;
|
||||
}
|
||||
}
|
||||
int stack_linkedList::dequeue()
|
||||
{
|
||||
int stack_linkedList::dequeue() {
|
||||
linkedlist *temp;
|
||||
int ele;
|
||||
if (front == NULL)
|
||||
std::cout << "\nStack is empty";
|
||||
else
|
||||
{
|
||||
else {
|
||||
temp = front;
|
||||
ele = temp->data;
|
||||
if (front == rear) // if length of queue is 1;
|
||||
@@ -50,25 +44,21 @@ int stack_linkedList::dequeue()
|
||||
}
|
||||
return ele;
|
||||
}
|
||||
void stack_linkedList::display()
|
||||
{
|
||||
void stack_linkedList::display() {
|
||||
if (front == NULL)
|
||||
std::cout << "\nStack is empty";
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
linkedlist *temp;
|
||||
temp = front;
|
||||
while (temp != NULL)
|
||||
{
|
||||
while (temp != NULL) {
|
||||
std::cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int op, data;
|
||||
stack_linkedList ob;
|
||||
std::cout << "\n1. enqueue(Insertion) ";
|
||||
@@ -76,17 +66,14 @@ int main()
|
||||
std::cout << "\n3. Display";
|
||||
std::cout << "\n4. Exit";
|
||||
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
std::cout << "\nEnter your choice ";
|
||||
std::cin >> op;
|
||||
if (op == 1)
|
||||
{
|
||||
if (op == 1) {
|
||||
std::cout << "Enter data ";
|
||||
std::cin >> data;
|
||||
ob.enqueue(data);
|
||||
}
|
||||
else if (op == 2)
|
||||
} else if (op == 2)
|
||||
data = ob.dequeue();
|
||||
else if (op == 3)
|
||||
ob.display();
|
||||
|
||||
@@ -4,69 +4,50 @@ using namespace std;
|
||||
int *stack;
|
||||
int top = 0, size;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if (top == size)
|
||||
{
|
||||
void push(int x) {
|
||||
if (top == size) {
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
stack[top++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top == 0)
|
||||
{
|
||||
void pop() {
|
||||
if (top == 0) {
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "\n" << stack[--top] << " deleted";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
void show() {
|
||||
for (int i = 0; i < top; i++) {
|
||||
cout << stack[i] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void topmost() { cout << "\nTopmost element: " << stack[top - 1]; }
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
cout << "\nEnter Size of stack : ";
|
||||
cin >> size;
|
||||
stack = new int[size];
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Push";
|
||||
cout << "\n2. Pop";
|
||||
cout << "\n3. Print";
|
||||
cout << "\n4. Print topmost element:";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
if (ch == 1) {
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch == 2)
|
||||
{
|
||||
} else if (ch == 2) {
|
||||
pop();
|
||||
}
|
||||
else if (ch == 3)
|
||||
{
|
||||
} else if (ch == 3) {
|
||||
show();
|
||||
}
|
||||
else if (ch == 4)
|
||||
{
|
||||
} else if (ch == 4) {
|
||||
topmost();
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *top;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
void push(int x) {
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = top;
|
||||
top = n;
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top == NULL)
|
||||
{
|
||||
void pop() {
|
||||
if (top == NULL) {
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *t = top;
|
||||
cout << "\n" << t->val << " deleted";
|
||||
top = top->next;
|
||||
@@ -32,38 +26,29 @@ void pop()
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
void show() {
|
||||
node *t = top;
|
||||
while (t != NULL)
|
||||
{
|
||||
while (t != NULL) {
|
||||
cout << t->val << "\n";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Push";
|
||||
cout << "\n2. Pop";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
if (ch == 1) {
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch == 2)
|
||||
{
|
||||
} else if (ch == 2) {
|
||||
pop();
|
||||
}
|
||||
else if (ch == 3)
|
||||
{
|
||||
} else if (ch == 3) {
|
||||
show();
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int main(int argc, char* argv[]) {
|
||||
double GPA;
|
||||
double highestGPA;
|
||||
string name;
|
||||
@@ -35,24 +34,19 @@ int main(int argc, char* argv[])
|
||||
infile >> GPA >> name;
|
||||
highestGPA = GPA;
|
||||
|
||||
while (infile)
|
||||
{
|
||||
if (GPA > highestGPA)
|
||||
{
|
||||
while (infile) {
|
||||
if (GPA > highestGPA) {
|
||||
stk.clear();
|
||||
stk.push(name);
|
||||
highestGPA = GPA;
|
||||
}
|
||||
else if (GPA == highestGPA)
|
||||
{
|
||||
} else if (GPA == highestGPA) {
|
||||
stk.push(name);
|
||||
}
|
||||
infile >> GPA >> name;
|
||||
}
|
||||
cout << "Highest GPA: " << highestGPA << endl;
|
||||
cout << "Students the highest GPA are: " << endl;
|
||||
while (!stk.isEmptyStack())
|
||||
{
|
||||
while (!stk.isEmptyStack()) {
|
||||
cout << stk.top() << endl;
|
||||
stk.pop();
|
||||
}
|
||||
|
||||
@@ -6,26 +6,21 @@ using namespace std;
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Type>
|
||||
stack<Type>::stack()
|
||||
{
|
||||
stack<Type>::stack() {
|
||||
stackTop = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
template <class Type>
|
||||
stack<Type>::~stack()
|
||||
{
|
||||
}
|
||||
stack<Type>::~stack() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Type>
|
||||
void stack<Type>::display()
|
||||
{
|
||||
void stack<Type>::display() {
|
||||
node<Type> *current = stackTop;
|
||||
cout << "Top --> ";
|
||||
while (current != NULL)
|
||||
{
|
||||
while (current != NULL) {
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
@@ -35,22 +30,19 @@ void stack<Type>::display()
|
||||
|
||||
/* Determine whether the stack is empty */
|
||||
template <class Type>
|
||||
bool stack<Type>::isEmptyStack()
|
||||
{
|
||||
bool stack<Type>::isEmptyStack() {
|
||||
return (stackTop == NULL);
|
||||
}
|
||||
|
||||
/* Clear stack */
|
||||
template <class Type>
|
||||
void stack<Type>::clear()
|
||||
{
|
||||
void stack<Type>::clear() {
|
||||
stackTop = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::push(Type item)
|
||||
{
|
||||
void stack<Type>::push(Type item) {
|
||||
node<Type> *newNode;
|
||||
newNode = new node<Type>;
|
||||
newNode->data = item;
|
||||
@@ -61,42 +53,35 @@ void stack<Type>::push(Type item)
|
||||
|
||||
/* Return the top element of the stack */
|
||||
template <class Type>
|
||||
Type stack<Type>::top()
|
||||
{
|
||||
Type stack<Type>::top() {
|
||||
assert(stackTop != NULL);
|
||||
return stackTop->data;
|
||||
}
|
||||
|
||||
/* Remove the top element of the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::pop()
|
||||
{
|
||||
void stack<Type>::pop() {
|
||||
node<Type> *temp;
|
||||
if (!isEmptyStack())
|
||||
{
|
||||
if (!isEmptyStack()) {
|
||||
temp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
delete temp;
|
||||
size--;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "Stack is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* Operator "=" */
|
||||
template <class Type>
|
||||
stack<Type> stack<Type>::operator=(stack<Type> &otherStack)
|
||||
{
|
||||
stack<Type> stack<Type>::operator=(stack<Type> &otherStack) {
|
||||
node<Type> *newNode, *current, *last;
|
||||
|
||||
if (stackTop != NULL) /* If stack is no empty, make it empty */
|
||||
stackTop = NULL;
|
||||
if (otherStack.stackTop == NULL)
|
||||
stackTop = NULL;
|
||||
else
|
||||
{
|
||||
else {
|
||||
current = otherStack.stackTop;
|
||||
stackTop = new node<Type>;
|
||||
stackTop->data = current->data;
|
||||
@@ -104,8 +89,7 @@ stack<Type> stack<Type>::operator=(stack<Type> &otherStack)
|
||||
last = stackTop;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL)
|
||||
{
|
||||
while (current != NULL) {
|
||||
newNode = new node<Type>;
|
||||
newNode->data = current->data;
|
||||
newNode->next = NULL;
|
||||
|
||||
@@ -4,16 +4,14 @@
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
class stack
|
||||
{
|
||||
class stack {
|
||||
public:
|
||||
void display(); /* Show stack */
|
||||
stack(); /* Default constructor*/
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
stack<int> stk;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
|
||||
@@ -2,17 +2,14 @@
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
struct node {
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
void CreateTree(node *curr, node *n, int x, char pos)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void CreateTree(node *curr, node *n, int x, char pos) {
|
||||
if (n != NULL) {
|
||||
char ch;
|
||||
cout << "\nLeft or Right of " << n->val << " : ";
|
||||
cin >> ch;
|
||||
@@ -20,32 +17,25 @@ void CreateTree(node *curr, node *n, int x, char pos)
|
||||
CreateTree(n, n->left, x, ch);
|
||||
else if (ch == 'r')
|
||||
CreateTree(n, n->right, x, ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
node *t = new node;
|
||||
t->val = x;
|
||||
t->left = NULL;
|
||||
t->right = NULL;
|
||||
if (pos == 'l')
|
||||
{
|
||||
if (pos == 'l') {
|
||||
curr->left = t;
|
||||
}
|
||||
else if (pos == 'r')
|
||||
{
|
||||
} else if (pos == 'r') {
|
||||
curr->right = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
void BFT(node *n) {
|
||||
list<node *> queue;
|
||||
|
||||
queue.push_back(n);
|
||||
|
||||
while (!queue.empty())
|
||||
{
|
||||
while (!queue.empty()) {
|
||||
n = queue.front();
|
||||
cout << n->val << " ";
|
||||
queue.pop_front();
|
||||
@@ -57,38 +47,31 @@ void BFT(node *n)
|
||||
}
|
||||
}
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void Pre(node *n) {
|
||||
if (n != NULL) {
|
||||
cout << n->val << " ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void In(node *n) {
|
||||
if (n != NULL) {
|
||||
In(n->left);
|
||||
cout << n->val << " ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n != NULL)
|
||||
{
|
||||
void Post(node *n) {
|
||||
if (n != NULL) {
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout << n->val << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int value;
|
||||
int ch;
|
||||
node *root = new node;
|
||||
@@ -97,8 +80,7 @@ int main()
|
||||
root->val = value;
|
||||
root->left = NULL;
|
||||
root->right = NULL;
|
||||
do
|
||||
{
|
||||
do {
|
||||
cout << "\n1. Insert";
|
||||
cout << "\n2. Breadth First";
|
||||
cout << "\n3. Preorder Depth First";
|
||||
@@ -107,8 +89,7 @@ int main()
|
||||
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
switch (ch)
|
||||
{
|
||||
switch (ch) {
|
||||
case 1:
|
||||
int x;
|
||||
char pos;
|
||||
|
||||
@@ -5,15 +5,13 @@
|
||||
#include <string>
|
||||
|
||||
// structure definition
|
||||
typedef struct trie
|
||||
{
|
||||
typedef struct trie {
|
||||
struct trie* arr[26];
|
||||
bool isEndofWord;
|
||||
} trie;
|
||||
|
||||
// create a new node for trie
|
||||
trie* createNode()
|
||||
{
|
||||
trie* createNode() {
|
||||
trie* nn = new trie();
|
||||
for (int i = 0; i < 26; i++) nn->arr[i] = NULL;
|
||||
nn->isEndofWord = false;
|
||||
@@ -21,17 +19,12 @@ trie* createNode()
|
||||
}
|
||||
|
||||
// insert string into the trie
|
||||
void insert(trie* root, std::string str)
|
||||
{
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
void insert(trie* root, std::string str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
int j = str[i] - 'a';
|
||||
if (root->arr[j])
|
||||
{
|
||||
if (root->arr[j]) {
|
||||
root = root->arr[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
root->arr[j] = createNode();
|
||||
root = root->arr[j];
|
||||
}
|
||||
@@ -40,10 +33,8 @@ void insert(trie* root, std::string str)
|
||||
}
|
||||
|
||||
// search a string exists inside the trie
|
||||
bool search(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
bool search(trie* root, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
return true;
|
||||
@@ -59,10 +50,8 @@ removes the string if it is not a prefix of any other
|
||||
string, if it is then just sets the endofword to false, else
|
||||
removes the given string
|
||||
*/
|
||||
bool deleteString(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
bool deleteString(trie* root, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
root->isEndofWord = false;
|
||||
@@ -73,15 +62,11 @@ bool deleteString(trie* root, std::string str, int index)
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
bool var = deleteString(root, str, index + 1);
|
||||
if (var)
|
||||
{
|
||||
if (var) {
|
||||
root->arr[j] = NULL;
|
||||
if (root->isEndofWord)
|
||||
{
|
||||
if (root->isEndofWord) {
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int i;
|
||||
for (i = 0; i < 26; i++)
|
||||
if (root->arr[i])
|
||||
@@ -91,8 +76,7 @@ bool deleteString(trie* root, std::string str, int index)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
trie* root = createNode();
|
||||
insert(root, "hello");
|
||||
insert(root, "world");
|
||||
|
||||
Reference in New Issue
Block a user